1
1
import collections .abc
2
2
import types
3
3
import unittest
4
- from test .support import C_RECURSION_LIMIT
4
+ from test .support import get_c_recursion_limit
5
5
6
6
class TestExceptionGroupTypeHierarchy (unittest .TestCase ):
7
7
def test_exception_group_types (self ):
@@ -300,17 +300,33 @@ def assertMatchesTemplate(self, exc, exc_type, template):
300
300
self .assertEqual (type (exc ), type (template ))
301
301
self .assertEqual (exc .args , template .args )
302
302
303
+ class Predicate :
304
+ def __init__ (self , func ):
305
+ self .func = func
306
+
307
+ def __call__ (self , e ):
308
+ return self .func (e )
309
+
310
+ def method (self , e ):
311
+ return self .func (e )
303
312
304
313
class ExceptionGroupSubgroupTests (ExceptionGroupTestBase ):
305
314
def setUp (self ):
306
315
self .eg = create_simple_eg ()
307
316
self .eg_template = [ValueError (1 ), TypeError (int ), ValueError (2 )]
308
317
318
+ # TODO: RUSTPYTHON
319
+ @unittest .expectedFailure
309
320
def test_basics_subgroup_split__bad_arg_type (self ):
321
+ class C :
322
+ pass
323
+
310
324
bad_args = ["bad arg" ,
325
+ C ,
311
326
OSError ('instance not type' ),
312
327
[OSError , TypeError ],
313
- (OSError , 42 )]
328
+ (OSError , 42 ),
329
+ ]
314
330
for arg in bad_args :
315
331
with self .assertRaises (TypeError ):
316
332
self .eg .subgroup (arg )
@@ -342,10 +358,14 @@ def test_basics_subgroup_by_type__match(self):
342
358
self .assertMatchesTemplate (subeg , ExceptionGroup , template )
343
359
344
360
def test_basics_subgroup_by_predicate__passthrough (self ):
345
- self .assertIs (self .eg , self .eg .subgroup (lambda e : True ))
361
+ f = lambda e : True
362
+ for callable in [f , Predicate (f ), Predicate (f ).method ]:
363
+ self .assertIs (self .eg , self .eg .subgroup (callable ))
346
364
347
365
def test_basics_subgroup_by_predicate__no_match (self ):
348
- self .assertIsNone (self .eg .subgroup (lambda e : False ))
366
+ f = lambda e : False
367
+ for callable in [f , Predicate (f ), Predicate (f ).method ]:
368
+ self .assertIsNone (self .eg .subgroup (callable ))
349
369
350
370
def test_basics_subgroup_by_predicate__match (self ):
351
371
eg = self .eg
@@ -356,9 +376,12 @@ def test_basics_subgroup_by_predicate__match(self):
356
376
((ValueError , TypeError ), self .eg_template )]
357
377
358
378
for match_type , template in testcases :
359
- subeg = eg .subgroup (lambda e : isinstance (e , match_type ))
360
- self .assertEqual (subeg .message , eg .message )
361
- self .assertMatchesTemplate (subeg , ExceptionGroup , template )
379
+ f = lambda e : isinstance (e , match_type )
380
+ for callable in [f , Predicate (f ), Predicate (f ).method ]:
381
+ with self .subTest (callable = callable ):
382
+ subeg = eg .subgroup (f )
383
+ self .assertEqual (subeg .message , eg .message )
384
+ self .assertMatchesTemplate (subeg , ExceptionGroup , template )
362
385
363
386
364
387
class ExceptionGroupSplitTests (ExceptionGroupTestBase ):
@@ -405,14 +428,18 @@ def test_basics_split_by_type__match(self):
405
428
self .assertIsNone (rest )
406
429
407
430
def test_basics_split_by_predicate__passthrough (self ):
408
- match , rest = self .eg .split (lambda e : True )
409
- self .assertMatchesTemplate (match , ExceptionGroup , self .eg_template )
410
- self .assertIsNone (rest )
431
+ f = lambda e : True
432
+ for callable in [f , Predicate (f ), Predicate (f ).method ]:
433
+ match , rest = self .eg .split (callable )
434
+ self .assertMatchesTemplate (match , ExceptionGroup , self .eg_template )
435
+ self .assertIsNone (rest )
411
436
412
437
def test_basics_split_by_predicate__no_match (self ):
413
- match , rest = self .eg .split (lambda e : False )
414
- self .assertIsNone (match )
415
- self .assertMatchesTemplate (rest , ExceptionGroup , self .eg_template )
438
+ f = lambda e : False
439
+ for callable in [f , Predicate (f ), Predicate (f ).method ]:
440
+ match , rest = self .eg .split (callable )
441
+ self .assertIsNone (match )
442
+ self .assertMatchesTemplate (rest , ExceptionGroup , self .eg_template )
416
443
417
444
def test_basics_split_by_predicate__match (self ):
418
445
eg = self .eg
@@ -426,20 +453,22 @@ def test_basics_split_by_predicate__match(self):
426
453
]
427
454
428
455
for match_type , match_template , rest_template in testcases :
429
- match , rest = eg .split (lambda e : isinstance (e , match_type ))
430
- self .assertEqual (match .message , eg .message )
431
- self .assertMatchesTemplate (
432
- match , ExceptionGroup , match_template )
433
- if rest_template is not None :
434
- self .assertEqual (rest .message , eg .message )
456
+ f = lambda e : isinstance (e , match_type )
457
+ for callable in [f , Predicate (f ), Predicate (f ).method ]:
458
+ match , rest = eg .split (callable )
459
+ self .assertEqual (match .message , eg .message )
435
460
self .assertMatchesTemplate (
436
- rest , ExceptionGroup , rest_template )
461
+ match , ExceptionGroup , match_template )
462
+ if rest_template is not None :
463
+ self .assertEqual (rest .message , eg .message )
464
+ self .assertMatchesTemplate (
465
+ rest , ExceptionGroup , rest_template )
437
466
438
467
439
468
class DeepRecursionInSplitAndSubgroup (unittest .TestCase ):
440
469
def make_deep_eg (self ):
441
470
e = TypeError (1 )
442
- for i in range (C_RECURSION_LIMIT + 1 ):
471
+ for i in range (get_c_recursion_limit () + 1 ):
443
472
e = ExceptionGroup ('eg' , [e ])
444
473
return e
445
474
0 commit comments