8000 gh-105522: [Enum] Correctly handle possible exceptions during testing… · python/cpython@199438b · GitHub
[go: up one dir, main page]

Skip to content

Commit 199438b

Browse files
authored
gh-105522: [Enum] Correctly handle possible exceptions during testing (GH-105523)
1 parent fce93c8 commit 199438b

File tree

1 file changed

+96
-54
lines changed

1 file changed

+96
-54
lines changed

Lib/test/test_enum.py

Lines changed: 96 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,25 @@ def load_tests(loader, tests, ignore):
3838
))
3939
return tests
4040

41+
def reraise_if_not_enum(*enum_types_or_exceptions):
42+
from functools import wraps
43+
44+
def decorator(func):
45+
@wraps(func)
46+
def inner(*args, **kwargs):
47+
excs = [
48+
e
49+
for e in enum_types_or_exceptions
50+
if isinstance(e, Exception)
51+
]
52+
if len(excs) == 1:
53+
raise excs[0]
54+
elif excs:
55+
raise ExceptionGroup('Enum Exceptions', excs)
56+
return func(*args, **kwargs)
57+
return inner
58+
return decorator
59+
4160
MODULE = __name__
4261
SHORT_MODULE = MODULE.split('.')[-1]
4362

@@ -75,30 +94,42 @@ class FlagStooges(Flag):
7594
except Exception as exc:
7695
FlagStooges = exc
7796

78-
class FlagStoogesWithZero(Flag):
79-
NOFLAG = 0
80-
LARRY = 1
81-
CURLY = 2
82-
MOE = 4
83-
BIG = 389
84-
85-
class IntFlagStooges(IntFlag):
86-
LARRY = 1
87-
CURLY = 2
88-
MOE = 4
89-
BIG = 389
90-
91-
class IntFlagStoogesWithZero(IntFlag):
92-
NOFLAG = 0
93-
LARRY = 1
94-
CURLY = 2
95-
MOE = 4
96-
BIG = 389
97+
try:
98+
class FlagStoogesWithZero(Flag):
99+
NOFLAG = 0
100+
LARRY = 1
101+
CURLY = 2
102+
MOE = 4
103+
BIG = 389
104+
except Exception as exc:
105+
FlagStoogesWithZero = exc
106+
107+
try:
108+
class IntFlagStooges(IntFlag):
109+
LARRY = 1
110+
CURLY = 2
111+
MOE = 4
112+
BIG = 389
113+
except Exception as exc:
114+
IntFlagStooges = exc
115+
116+
try:
117+
class IntFlagStoogesWithZero(IntFlag):
118+
NOFLAG = 0
119+
LARRY = 1
120+
CURLY = 2
121+
MOE = 4
122+
BIG = 389
123+
except Exception as exc:
124+
IntFlagStoogesWithZero = exc
97125

98126
# for pickle test and subclass tests
99-
class Name(StrEnum):
100-
BDFL = 'Guido van Rossum'
101-
FLUFL = 'Barry Warsaw'
127+
try:
128+
class Name(StrEnum):
129+
BDFL = 'Guido van Rossum'
130+
FLUFL = 'Barry Warsaw'
131+
except Exception as exc:
132+
Name = exc
102133

103134
try:
104135
Question = Enum('Question', 'who what when where why', module=__name__)
@@ -204,26 +235,35 @@ def __get__(self, instance, ownerclass):
204235

205236
# for global repr tests
206237

207-
@enum.global_enum
208-
class HeadlightsK(IntFlag, boundary=enum.KEEP):
209-
OFF_K = 0
210-
LOW_BEAM_K = auto()
211-
HIGH_BEAM_K = auto()
212-
FOG_K = auto()
238+
try:
239+
@enum.global_enum
240+
class HeadlightsK(IntFlag, boundary=enum.KEEP):
241+
OFF_K = 0
242+
LOW_BEAM_K = auto()
243+
HIGH_BEAM_K = auto()
244+
FOG_K = auto()
245+
except Exception as exc:
246+
HeadlightsK = exc
213247

214248

215-
@enum.global_enum
216-
class HeadlightsC(IntFlag, boundary=enum.CONFORM):
217-
OFF_C = 0
218-
LOW_BEAM_C = auto()
219-
HIGH_BEAM_C = auto()
220-
FOG_C = auto()
249+
try:
250+
@enum.global_enum
251+
class HeadlightsC(IntFlag, boundary=enum.CONFORM):
252+
OFF_C = 0
253+
LOW_BEAM_C = auto()
254+
HIGH_BEAM_C = auto()
255+
FOG_C = auto()
256+
except Exception as exc:
257+
HeadlightsC = exc
221258

222259

223-
@enum.global_enum
224-
class NoName(Flag):
225-
ONE = 1
226-
TWO = 2
260+
try:
261+
@enum.global_enum
262+
class NoName(Flag):
263+
ONE = 1
264+
TWO = 2
265+
except Exception as exc:
266+
NoName = exc
227267

228268

229269
# tests
@@ -1124,9 +1164,8 @@ def red(self):
11241164
green = 2
11251165
blue = 3
11261166

1167+
@reraise_if_not_enum(Theory)
11271168
def test_enum_function_with_qualname(self):
1128-
if isinstance(Theory, Exception):
1129-
raise Theory
11301169
self.assertEqual(Theory.__qualname__, 'spanish_inquisition')
11311170

11321171
def test_enum_of_types(self):
@@ -1355,6 +1394,7 @@ class MyUnBrokenEnum(UnBrokenInt, Enum):
13551394
test_pickle_dump_load(self.assertIs, MyUnBrokenEnum.I)
13561395
test_pickle_dump_load(self.assertIs, MyUnBrokenEnum)
13571396

1397+
@reraise_if_not_enum(FloatStooges)
13581398
def test_floatenum_fromhex(self):
13591399
h = float.hex(FloatStooges.MOE.value)
13601400
self.assertIs(FloatStooges.fromhex(h), FloatStooges.MOE)
@@ -1475,6 +1515,7 @@ class ThreePart(Enum):
14751515
self.assertIs(ThreePart((3, 3.0, 'three')), ThreePart.THREE)
14761516
self.assertIs(ThreePart(3, 3.0, 'three'), ThreePart.THREE)
14771517

1518+
@reraise_if_not_enum(IntStooges)
14781519
def test_intenum_from_bytes(self):
14791520
self.assertIs(IntStooges.from_bytes(b'\x00\x03', 'big'), IntStooges.MOE)
14801521
with self.assertRaises(ValueError):
@@ -1503,33 +1544,28 @@ def repr(self):
15031544
class Huh(MyStr, MyInt, Enum):
15041545
One = 1
15051546

1547+
@reraise_if_not_enum(Stooges)
15061548
def test_pickle_enum(self):
1507-
if isinstance(Stooges, Exception):
1508-
raise Stooges
15091549
test_pickle_dump_load(self.assertIs, Stooges.CURLY)
15101550
test_pickle_dump_load(self.assertIs, Stooges)
15111551

1552+
@reraise_if_not_enum(IntStooges)
15121553
def test_pickle_int(self):
1513-
if isinstance(IntStooges, Exception):
1514-
raise IntStooges
15151554
test_pickle_dump_load(self.assertIs, IntStooges.CURLY)
15161555
test_pickle_dump_load(self.assertIs, IntStooges)
15171556

1557+
@reraise_if_not_enum(FloatStooges)
15181558
def test_pickle_float(self):
1519-
if isinstance(FloatStooges, Exception):
1520-
raise FloatStooges
15211559
test_pickle_dump_load(self.assertIs, FloatStooges.CURLY)
15221560
test_pickle_dump_load(self.assertIs, FloatStooges)
15231561

1562+
@reraise_if_not_enum(Answer)
15241563
def test_pickle_enum_function(self):
1525-
if isinstance(Answer, Exception):
1526-
raise Answer
15271564
test_pickle_dump_load(self.assertIs, Answer.him)
15281565
test_pickle_dump_load(self.assertIs, Answer)
15291566

1567+
@reraise_if_not_enum(Question)
15301568
def test_pickle_enum_function_with_module(self):
1531-
if isinstance(Question, Exception):
1532-
raise Question
15331569
test_pickle_dump_load(self.assertIs, Question.who)
15341570
test_pickle_dump_load(self.assertIs, Question)
15351571

@@ -1592,9 +1628,8 @@ class Season(Enum):
15921628
[Season.SUMMER, Season.WINTER, Season.AUTUMN, Season.SPRING],
15931629
)
15941630

1631+
@reraise_if_not_enum(Name)
15951632
def test_subclassing(self):
1596< EED3 /code>-
if isinstance(Name, Exception):
1597-
raise Name
15981633
self.assertEqual(Name.BDFL, 'Guido van Rossum')
15991634
self.assertTrue(Name.BDFL, Name('Guido van Rossum'))
16001635
self.assertIs(Name.BDFL, getattr(Name, 'BDFL'))
@@ -3330,9 +3365,13 @@ def test_programatic_function_from_dict(self):
33303365
self.assertIn(e, Perm)
33313366
self.assertIs(type(e), Perm)
33323367

3368+
@reraise_if_not_enum(
3369+
FlagStooges,
3370+
FlagStoogesWithZero,
3371+
IntFlagStooges,
3372+
IntFlagStoogesWithZero,
3373+
)
33333374
def test_pickle(self):
3334-
if isinstance(FlagStooges, Exception):
3335-
raise FlagStooges
33363375
test_pickle_dump_load(self.assertIs, FlagStooges.CURLY)
33373376
test_pickle_dump_load(self.assertEqual,
33383377
FlagStooges.CURLY|FlagStooges.MOE)
@@ -3637,6 +3676,7 @@ def test_type(self):
36373676
self.assertTrue(isinstance(Open.WO | Open.RW, Open))
36383677
self.assertEqual(Open.WO | Open.RW, 3)
36393678

3679+
@reraise_if_not_enum(HeadlightsK)
36403680
def test_global_repr_keep(self):
36413681
self.assertEqual(
36423682
repr(HeadlightsK(0)),
@@ -3651,6 +3691,7 @@ def test_global_repr_keep(self):
36513691
'%(m)s.HeadlightsK(8)' % {'m': SHORT_MODULE},
36523692
)
36533693

3694+
@reraise_if_not_enum(HeadlightsC)
36543695
def test_global_repr_conform1(self):
36553696
self.assertEqual(
36563697
repr(HeadlightsC(0)),
@@ -3665,6 +3706,7 @@ def test_global_repr_conform1(self):
36653706
'%(m)s.OFF_C' % {'m': SHORT_MODULE},
36663707
)
36673708

3709 1CF5 +
@reraise_if_not_enum(NoName)
36683710
def test_global_enum_str(self):
36693711
self.assertEqual(str(NoName.ONE & NoName.TWO), 'NoName(0)')
36703712
self.assertEqual(str(NoName(0)), 'NoName(0)')

0 commit comments

Comments
 (0)
0