Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0e816c1

Browse filesBrowse files
committed
test_exception_group from CPython 3.12.2
1 parent d446321 commit 0e816c1
Copy full SHA for 0e816c1

File tree

1 file changed

+32
-76
lines changed
Filter options

1 file changed

+32
-76
lines changed

‎Lib/test/test_exception_group.py

Copy file name to clipboardExpand all lines: Lib/test/test_exception_group.py
+32-76Lines changed: 32 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import collections.abc
2-
import traceback
32
import types
43
import unittest
5-
4+
from test.support import C_RECURSION_LIMIT
65

76
class TestExceptionGroupTypeHierarchy(unittest.TestCase):
8-
# TODO: RUSTPYTHON
9-
@unittest.expectedFailure
107
def test_exception_group_types(self):
118
self.assertTrue(issubclass(ExceptionGroup, Exception))
129
self.assertTrue(issubclass(ExceptionGroup, BaseExceptionGroup))
@@ -38,17 +35,13 @@ def test_bad_EG_construction__too_many_args(self):
3835
with self.assertRaisesRegex(TypeError, MSG):
3936
ExceptionGroup('eg', [ValueError('too')], [TypeError('many')])
4037

41-
# TODO: RUSTPYTHON
42-
@unittest.expectedFailure
4338
def test_bad_EG_construction__bad_message(self):
4439
MSG = 'argument 1 must be str, not '
4540
with self.assertRaisesRegex(TypeError, MSG):
4641
ExceptionGroup(ValueError(12), SyntaxError('bad syntax'))
4742
with self.assertRaisesRegex(TypeError, MSG):
4843
ExceptionGroup(None, [ValueError(12)])
4944

50-
# TODO: RUSTPYTHON
51-
@unittest.expectedFailure
5245
def test_bad_EG_construction__bad_excs_sequence(self):
5346
MSG = r'second argument \(exceptions\) must be a sequence'
5447
with self.assertRaisesRegex(TypeError, MSG):
@@ -60,8 +53,6 @@ def test_bad_EG_construction__bad_excs_sequence(self):
6053
with self.assertRaisesRegex(ValueError, MSG):
6154
ExceptionGroup("eg", [])
6255

63-
# TODO: RUSTPYTHON
64-
@unittest.expectedFailure
6556
def test_bad_EG_construction__nested_non_exceptions(self):
6657
MSG = (r'Item [0-9]+ of second argument \(exceptions\)'
6758
' is not an exception')
@@ -72,24 +63,18 @@ def test_bad_EG_construction__nested_non_exceptions(self):
7263

7364

7465
class InstanceCreation(unittest.TestCase):
75-
# TODO: RUSTPYTHON
76-
@unittest.expectedFailure
7766
def test_EG_wraps_Exceptions__creates_EG(self):
7867
excs = [ValueError(1), TypeError(2)]
7968
self.assertIs(
8069
type(ExceptionGroup("eg", excs)),
8170
ExceptionGroup)
8271

83-
# TODO: RUSTPYTHON
84-
@unittest.expectedFailure
8572
def test_BEG_wraps_Exceptions__creates_EG(self):
8673
excs = [ValueError(1), TypeError(2)]
8774
self.assertIs(
8875
type(BaseExceptionGroup("beg", excs)),
8976
ExceptionGroup)
9077

91-
# TODO: RUSTPYTHON
92-
@unittest.expectedFailure
9378
def test_EG_wraps_BaseException__raises_TypeError(self):
9479
MSG= "Cannot nest BaseExceptions in an ExceptionGroup"
9580
with self.assertRaisesRegex(TypeError, MSG):
@@ -99,8 +84,6 @@ def test_BEG_wraps_BaseException__creates_BEG(self):
9984
beg = BaseExceptionGroup("beg", [ValueError(1), KeyboardInterrupt(2)])
10085
self.assertIs(type(beg), BaseExceptionGroup)
10186

102-
# TODO: RUSTPYTHON
103-
@unittest.expectedFailure
10487
def test_EG_subclass_wraps_non_base_exceptions(self):
10588
class MyEG(ExceptionGroup):
10689
pass
@@ -109,8 +92,6 @@ class MyEG(ExceptionGroup):
10992
type(MyEG("eg", [ValueError(12), TypeError(42)])),
11093
MyEG)
11194

112-
# TODO: RUSTPYTHON
113-
@unittest.expectedFailure
11495
def test_EG_subclass_does_not_wrap_base_exceptions(self):
11596
class MyEG(ExceptionGroup):
11697
pass
@@ -119,8 +100,6 @@ class MyEG(ExceptionGroup):
119100
with self.assertRaisesRegex(TypeError, msg):
120101
MyEG("eg", [ValueError(12), KeyboardInterrupt(42)])
121102

122-
# TODO: RUSTPYTHON
123-
@unittest.expectedFailure
124103
def test_BEG_and_E_subclass_does_not_wrap_base_exceptions(self):
125104
class MyEG(BaseExceptionGroup, ValueError):
126105
pass
@@ -129,6 +108,21 @@ class MyEG(BaseExceptionGroup, ValueError):
129108
with self.assertRaisesRegex(TypeError, msg):
130109
MyEG("eg", [ValueError(12), KeyboardInterrupt(42)])
131110

111+
def test_EG_and_specific_subclass_can_wrap_any_nonbase_exception(self):
112+
class MyEG(ExceptionGroup, ValueError):
113+
pass
114+
115+
# The restriction is specific to Exception, not "the other base class"
116+
MyEG("eg", [ValueError(12), Exception()])
117+
118+
def test_BEG_and_specific_subclass_can_wrap_any_nonbase_exception(self):
119+
class MyEG(BaseExceptionGroup, ValueError):
120+
pass
121+
122+
# The restriction is specific to Exception, not "the other base class"
123+
MyEG("eg", [ValueError(12), Exception()])
124+
125+
132126
def test_BEG_subclass_wraps_anything(self):
133127
class MyBEG(BaseExceptionGroup):
134128
pass
@@ -142,8 +136,6 @@ class MyBEG(BaseExceptionGroup):
142136

143137

144138
class StrAndReprTests(unittest.TestCase):
145-
# TODO: RUSTPYTHON
146-
@unittest.expectedFailure
147139
def test_ExceptionGroup(self):
148140
eg = BaseExceptionGroup(
149141
'flat', [ValueError(1), TypeError(2)])
@@ -164,8 +156,6 @@ def test_ExceptionGroup(self):
164156
"ExceptionGroup('flat', "
165157
"[ValueError(1), TypeError(2)]), TypeError(2)])")
166158

167-
# TODO: RUSTPYTHON
168-
@unittest.expectedFailure
169159
def test_BaseExceptionGroup(self):
170160
eg = BaseExceptionGroup(
171161
'flat', [ValueError(1), KeyboardInterrupt(2)])
@@ -188,8 +178,6 @@ def test_BaseExceptionGroup(self):
188178
"BaseExceptionGroup('flat', "
189179
"[ValueError(1), KeyboardInterrupt(2)])])")
190180

191-
# TODO: RUSTPYTHON
192-
@unittest.expectedFailure
193181
def test_custom_exception(self):
194182
class MyEG(ExceptionGroup):
195183
pass
@@ -245,8 +233,6 @@ def create_simple_eg():
245233

246234

247235
class ExceptionGroupFields(unittest.TestCase):
248-
# TODO: RUSTPYTHON
249-
@unittest.expectedFailure
250236
def test_basics_ExceptionGroup_fields(self):
251237
eg = create_simple_eg()
252238

@@ -276,8 +262,6 @@ def test_basics_ExceptionGroup_fields(self):
276262
self.assertIsNone(tb.tb_next)
277263
self.assertEqual(tb.tb_lineno, tb_linenos[1][i])
278264

279-
# TODO: RUSTPYTHON
280-
@unittest.expectedFailure
281265
def test_fields_are_readonly(self):
282266
eg = ExceptionGroup('eg', [TypeError(1), OSError(2)])
283267

@@ -293,8 +277,6 @@ def test_fields_are_readonly(self):
293277

294278

295279
class ExceptionGroupTestBase(unittest.TestCase):
296-
# TODO: RUSTPYTHON
297-
@unittest.expectedFailure
298280
def assertMatchesTemplate(self, exc, exc_type, template):
299281
""" Assert that the exception matches the template
300282
@@ -324,7 +306,6 @@ def setUp(self):
324306
self.eg = create_simple_eg()
325307
self.eg_template = [ValueError(1), TypeError(int), ValueError(2)]
326308

327-
@unittest.skip("TODO: RUSTPYTHON")
328309
def test_basics_subgroup_split__bad_arg_type(self):
329310
bad_args = ["bad arg",
330311
OSError('instance not type'),
@@ -336,20 +317,16 @@ def test_basics_subgroup_split__bad_arg_type(self):
336317
with self.assertRaises(TypeError):
337318
self.eg.split(arg)
338319

339-
340-
@unittest.skip("TODO: RUSTPYTHON")
341320
def test_basics_subgroup_by_type__passthrough(self):
342321
eg = self.eg
343322
self.assertIs(eg, eg.subgroup(BaseException))
344323
self.assertIs(eg, eg.subgroup(Exception))
345324
self.assertIs(eg, eg.subgroup(BaseExceptionGroup))
346325
self.assertIs(eg, eg.subgroup(ExceptionGroup))
347326

348-
@unittest.skip("TODO: RUSTPYTHON")
349327
def test_basics_subgroup_by_type__no_match(self):
350328
self.assertIsNone(self.eg.subgroup(OSError))
351329

352-
@unittest.skip("TODO: RUSTPYTHON")
353330
def test_basics_subgroup_by_type__match(self):
354331
eg = self.eg
355332
testcases = [
@@ -364,15 +341,12 @@ def test_basics_subgroup_by_type__match(self):
364341
self.assertEqual(subeg.message, eg.message)
365342
self.assertMatchesTemplate(subeg, ExceptionGroup, template)
366343

367-
@unittest.skip("TODO: RUSTPYTHON")
368344
def test_basics_subgroup_by_predicate__passthrough(self):
369345
self.assertIs(self.eg, self.eg.subgroup(lambda e: True))
370346

371-
@unittest.skip("TODO: RUSTPYTHON")
372347
def test_basics_subgroup_by_predicate__no_match(self):
373348
self.assertIsNone(self.eg.subgroup(lambda e: False))
374349

375-
@unittest.skip("TODO: RUSTPYTHON")
376350
def test_basics_subgroup_by_predicate__match(self):
377351
eg = self.eg
378352
testcases = [
@@ -392,7 +366,6 @@ def setUp(self):
392366
self.eg = create_simple_eg()
393367
self.eg_template = [ValueError(1), TypeError(int), ValueError(2)]
394368

395-
@unittest.skip("TODO: RUSTPYTHON")
396369
def test_basics_split_by_type__passthrough(self):
397370
for E in [BaseException, Exception,
398371
BaseExceptionGroup, ExceptionGroup]:
@@ -401,14 +374,12 @@ def test_basics_split_by_type__passthrough(self):
401374
match, ExceptionGroup, self.eg_template)
402375
self.assertIsNone(rest)
403376

404-
@unittest.skip("TODO: RUSTPYTHON")
405377
def test_basics_split_by_type__no_match(self):
406378
match, rest = self.eg.split(OSError)
407379
self.assertIsNone(match)
408380
self.assertMatchesTemplate(
409381
rest, ExceptionGroup, self.eg_template)
410382

411-
@unittest.skip("TODO: RUSTPYTHON")
412383
def test_basics_split_by_type__match(self):
413384
eg = self.eg
414385
VE = ValueError
@@ -433,19 +404,16 @@ def test_basics_split_by_type__match(self):
433404
else:
434405
self.assertIsNone(rest)
435406

436-
@unittest.skip("TODO: RUSTPYTHON")
437407
def test_basics_split_by_predicate__passthrough(self):
438408
match, rest = self.eg.split(lambda e: True)
439409
self.assertMatchesTemplate(match, ExceptionGroup, self.eg_template)
440410
self.assertIsNone(rest)
441411

442-
@unittest.skip("TODO: RUSTPYTHON")
443412
def test_basics_split_by_predicate__no_match(self):
444413
match, rest = self.eg.split(lambda e: False)
445414
self.assertIsNone(match)
446415
self.assertMatchesTemplate(rest, ExceptionGroup, self.eg_template)
447416

448-
@unittest.skip("TODO: RUSTPYTHON")
449417
def test_basics_split_by_predicate__match(self):
450418
eg = self.eg
451419
VE = ValueError
@@ -471,19 +439,15 @@ def test_basics_split_by_predicate__match(self):
471439
class DeepRecursionInSplitAndSubgroup(unittest.TestCase):
472440
def make_deep_eg(self):
473441
e = TypeError(1)
474-
for i in range(2000):
442+
for i in range(C_RECURSION_LIMIT + 1):
475443
e = ExceptionGroup('eg', [e])
476444
return e
477445

478-
# TODO: RUSTPYTHON
479-
@unittest.expectedFailure
480446
def test_deep_split(self):
481447
e = self.make_deep_eg()
482448
with self.assertRaises(RecursionError):
483449
e.split(TypeError)
484450

485-
# TODO: RUSTPYTHON
486-
@unittest.expectedFailure
487451
def test_deep_subgroup(self):
488452
e = self.make_deep_eg()
489453
with self.assertRaises(RecursionError):
@@ -507,11 +471,9 @@ def leaf_generator(exc, tbs=None):
507471

508472
class LeafGeneratorTest(unittest.TestCase):
509473
# The leaf_generator is mentioned in PEP 654 as a suggestion
510-
# on how to iterate over leaf nodes of an EG. It is also
474+
# on how to iterate over leaf nodes of an EG. Is is also
511475
# used below as a test utility. So we test it here.
512476

513-
# TODO: RUSTPYTHON
514-
@unittest.expectedFailure
515477
def test_leaf_generator(self):
516478
eg = create_simple_eg()
517479

@@ -549,25 +511,19 @@ def create_nested_eg():
549511

550512

551513
class NestedExceptionGroupBasicsTest(ExceptionGroupTestBase):
552-
# TODO: RUSTPYTHON
553-
@unittest.expectedFailure
554514
def test_nested_group_matches_template(self):
555515
eg = create_nested_eg()
556516
self.assertMatchesTemplate(
557517
eg,
558518
ExceptionGroup,
559519
[[TypeError(bytes)], ValueError(1)])
560520

561-
# TODO: RUSTPYTHON
562-
@unittest.expectedFailure
563521
def test_nested_group_chaining(self):
564522
eg = create_nested_eg()
565523
self.assertIsInstance(eg.exceptions[1].__context__, MemoryError)
566524
self.assertIsInstance(eg.exceptions[1].__cause__, MemoryError)
567525
self.assertIsInstance(eg.exceptions[0].__context__, TypeError)
568526

569-
# TODO: RUSTPYTHON
570-
@unittest.expectedFailure
571527
def test_nested_exception_group_tracebacks(self):
572528
eg = create_nested_eg()
573529

@@ -581,8 +537,6 @@ def test_nested_exception_group_tracebacks(self):
581537
self.assertEqual(tb.tb_lineno, expected)
582538
self.assertIsNone(tb.tb_next)
583539

584-
# TODO: RUSTPYTHON
585-
@unittest.expectedFailure
586540
def test_iteration_full_tracebacks(self):
587541
eg = create_nested_eg()
588542
# check that iteration over leaves
@@ -670,8 +624,6 @@ def tb_linenos(tbs):
670624

671625
class NestedExceptionGroupSplitTest(ExceptionGroupSplitTestBase):
672626

673-
# TODO: RUSTPYTHON
674-
@unittest.expectedFailure
675627
def test_split_by_type(self):
676628
class MyExceptionGroup(ExceptionGroup):
677629
pass
@@ -771,8 +723,6 @@ def level3(i):
771723
self.assertMatchesTemplate(match, ExceptionGroup, [eg_template[0]])
772724
self.assertMatchesTemplate(rest, ExceptionGroup, [eg_template[1]])
773725

774-
# TODO: RUSTPYTHON
775-
@unittest.expectedFailure
776726
def test_split_BaseExceptionGroup(self):
777727
def exc(ex):
778728
try:
@@ -813,8 +763,6 @@ def exc(ex):
813763
self.assertMatchesTemplate(
814764
rest, ExceptionGroup, [ValueError(1)])
815765

816-
# TODO: RUSTPYTHON
817-
@unittest.expectedFailure
818766
def test_split_copies_notes(self):
819767
# make sure each exception group after a split has its own __notes__ list
820768
eg = ExceptionGroup("eg", [ValueError(1), TypeError(2)])
@@ -846,11 +794,23 @@ def test_split_does_not_copy_non_sequence_notes(self):
846794
self.assertFalse(hasattr(match, '__notes__'))
847795
self.assertFalse(hasattr(rest, '__notes__'))
848796

797+
# TODO: RUSTPYTHON
798+
@unittest.expectedFailure
799+
def test_drive_invalid_return_value(self):
800+
class MyEg(ExceptionGroup):
801+
def derive(self, excs):
802+
return 42
803+
804+
eg = MyEg('eg', [TypeError(1), ValueError(2)])
805+
msg = "derive must return an instance of BaseExceptionGroup"
806+
with self.assertRaisesRegex(TypeError, msg):
807+
eg.split(TypeError)
808+
with self.assertRaisesRegex(TypeError, msg):
809+
eg.subgroup(TypeError)
810+
849811

850812
class NestedExceptionGroupSubclassSplitTest(ExceptionGroupSplitTestBase):
851813

852-
# TODO: RUSTPYTHON
853-
@unittest.expectedFailure
854814
def test_split_ExceptionGroup_subclass_no_derive_no_new_override(self):
855815
class EG(ExceptionGroup):
856816
pass
@@ -893,8 +853,6 @@ class EG(ExceptionGroup):
893853
self.assertMatchesTemplate(match, ExceptionGroup, [[TypeError(2)]])
894854
self.assertMatchesTemplate(rest, ExceptionGroup, [ValueError(1)])
895855

896-
# TODO: RUSTPYTHON
897-
@unittest.expectedFailure
898856
def test_split_BaseExceptionGroup_subclass_no_derive_new_override(self):
899857
class EG(BaseExceptionGroup):
900858
def __new__(cls, message, excs, unused):
@@ -937,8 +895,6 @@ def __new__(cls, message, excs, unused):
937895
match, BaseExceptionGroup, [KeyboardInterrupt(2)])
938896
self.assertMatchesTemplate(rest, ExceptionGroup, [ValueError(1)])
939897

940-
# TODO: RUSTPYTHON
941-
@unittest.expectedFailure
942898
def test_split_ExceptionGroup_subclass_derive_and_new_overrides(self):
943899
class EG(ExceptionGroup):
944900
def __new__(cls, message, excs, code):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.