Skip to content

gh-105522: Correctly handle possible exceptions in Enum creation in test_enum #105523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 96 additions & 54 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ def load_tests(loader, tests, ignore):
))
return tests

def reraise_if_not_enum(*enum_types_or_exceptions):
from functools import wraps

def decorator(func):
@wraps(func)
def inner(*args, **kwargs):
excs = [
e
for e in enum_types_or_exceptions
if isinstance(e, Exception)
]
if len(excs) == 1:
raise excs[0]
elif excs:
raise ExceptionGroup('Enum Exceptions', excs)
return func(*args, **kwargs)
return inner
return decorator

MODULE = __name__
SHORT_MODULE = MODULE.split('.')[-1]

Expand Down Expand Up @@ -75,30 +94,42 @@ class FlagStooges(Flag):
except Exception as exc:
FlagStooges = exc

class FlagStoogesWithZero(Flag):
NOFLAG = 0
LARRY = 1
CURLY = 2
MOE = 4
BIG = 389

class IntFlagStooges(IntFlag):
LARRY = 1
CURLY = 2
MOE = 4
BIG = 389

class IntFlagStoogesWithZero(IntFlag):
NOFLAG = 0
LARRY = 1
CURLY = 2
MOE = 4
BIG = 389
try:
class FlagStoogesWithZero(Flag):
NOFLAG = 0
LARRY = 1
CURLY = 2
MOE = 4
BIG = 389
except Exception as exc:
FlagStoogesWithZero = exc

try:
class IntFlagStooges(IntFlag):
LARRY = 1
CURLY = 2
MOE = 4
BIG = 389
except Exception as exc:
IntFlagStooges = exc

try:
class IntFlagStoogesWithZero(IntFlag):
NOFLAG = 0
LARRY = 1
CURLY = 2
MOE = 4
BIG = 389
except Exception as exc:
IntFlagStoogesWithZero = exc

# for pickle test and subclass tests
class Name(StrEnum):
BDFL = 'Guido van Rossum'
FLUFL = 'Barry Warsaw'
try:
class Name(StrEnum):
BDFL = 'Guido van Rossum'
FLUFL = 'Barry Warsaw'
except Exception as exc:
Name = exc

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

# for global repr tests

@enum.global_enum
class HeadlightsK(IntFlag, boundary=enum.KEEP):
OFF_K = 0
LOW_BEAM_K = auto()
HIGH_BEAM_K = auto()
FOG_K = auto()
try:
@enum.global_enum
class HeadlightsK(IntFlag, boundary=enum.KEEP):
OFF_K = 0
LOW_BEAM_K = auto()
HIGH_BEAM_K = auto()
FOG_K = auto()
except Exception as exc:
HeadlightsK = exc


@enum.global_enum
class HeadlightsC(IntFlag, boundary=enum.CONFORM):
OFF_C = 0
LOW_BEAM_C = auto()
HIGH_BEAM_C = auto()
FOG_C = auto()
try:
@enum.global_enum
class HeadlightsC(IntFlag, boundary=enum.CONFORM):
OFF_C = 0
LOW_BEAM_C = auto()
HIGH_BEAM_C = auto()
FOG_C = auto()
except Exception as exc:
HeadlightsC = exc


@enum.global_enum
class NoName(Flag):
ONE = 1
TWO = 2
try:
@enum.global_enum
class NoName(Flag):
ONE = 1
TWO = 2
except Exception as exc:
NoName = exc


# tests
Expand Down Expand Up @@ -1124,9 +1164,8 @@ def red(self):
green = 2
blue = 3

@reraise_if_not_enum(Theory)
def test_enum_function_with_qualname(self):
if isinstance(Theory, Exception):
raise Theory
self.assertEqual(Theory.__qualname__, 'spanish_inquisition')

def test_enum_of_types(self):
Expand Down Expand Up @@ -1355,6 +1394,7 @@ class MyUnBrokenEnum(UnBrokenInt, Enum):
test_pickle_dump_load(self.assertIs, MyUnBrokenEnum.I)
test_pickle_dump_load(self.assertIs, MyUnBrokenEnum)

@reraise_if_not_enum(FloatStooges)
def test_floatenum_fromhex(self):
h = float.hex(FloatStooges.MOE.value)
self.assertIs(FloatStooges.fromhex(h), FloatStooges.MOE)
Expand Down Expand Up @@ -1475,6 +1515,7 @@ class ThreePart(Enum):
self.assertIs(ThreePart((3, 3.0, 'three')), ThreePart.THREE)
self.assertIs(ThreePart(3, 3.0, 'three'), ThreePart.THREE)

@reraise_if_not_enum(IntStooges)
def test_intenum_from_bytes(self):
self.assertIs(IntStooges.from_bytes(b'\x00\x03', 'big'), IntStooges.MOE)
with self.assertRaises(ValueError):
Expand Down Expand Up @@ -1503,33 +1544,28 @@ def repr(self):
class Huh(MyStr, MyInt, Enum):
One = 1

@reraise_if_not_enum(Stooges)
def test_pickle_enum(self):
if isinstance(Stooges, Exception):
raise Stooges
test_pickle_dump_load(self.assertIs, Stooges.CURLY)
test_pickle_dump_load(self.assertIs, Stooges)

@reraise_if_not_enum(IntStooges)
def test_pickle_int(self):
if isinstance(IntStooges, Exception):
raise IntStooges
test_pickle_dump_load(self.assertIs, IntStooges.CURLY)
test_pickle_dump_load(self.assertIs, IntStooges)

@reraise_if_not_enum(FloatStooges)
def test_pickle_float(self):
if isinstance(FloatStooges, Exception):
raise FloatStooges
test_pickle_dump_load(self.assertIs, FloatStooges.CURLY)
test_pickle_dump_load(self.assertIs, FloatStooges)

@reraise_if_not_enum(Answer)
def test_pickle_enum_function(self):
if isinstance(Answer, Exception):
raise Answer
test_pickle_dump_load(self.assertIs, Answer.him)
test_pickle_dump_load(self.assertIs, Answer)

@reraise_if_not_enum(Question)
def test_pickle_enum_function_with_module(self):
if isinstance(Question, Exception):
raise Question
test_pickle_dump_load(self.assertIs, Question.who)
test_pickle_dump_load(self.assertIs, Question)

Expand Down Expand Up @@ -1592,9 +1628,8 @@ class Season(Enum):
[Season.SUMMER, Season.WINTER, Season.AUTUMN, Season.SPRING],
)

@reraise_if_not_enum(Name)
def test_subclassing(self):
if isinstance(Name, Exception):
raise Name
self.assertEqual(Name.BDFL, 'Guido van Rossum')
self.assertTrue(Name.BDFL, Name('Guido van Rossum'))
self.assertIs(Name.BDFL, getattr(Name, 'BDFL'))
Expand Down Expand Up @@ -3330,9 +3365,13 @@ def test_programatic_function_from_dict(self):
self.assertIn(e, Perm)
self.assertIs(type(e), Perm)

@reraise_if_not_enum(
FlagStooges,
FlagStoogesWithZero,
IntFlagStooges,
IntFlagStoogesWithZero,
)
def test_pickle(self):
if isinstance(FlagStooges, Exception):
raise FlagStooges
test_pickle_dump_load(self.assertIs, FlagStooges.CURLY)
test_pickle_dump_load(self.assertEqual,
FlagStooges.CURLY|FlagStooges.MOE)
Expand Down Expand Up @@ -3637,6 +3676,7 @@ def test_type(self):
self.assertTrue(isinstance(Open.WO | Open.RW, Open))
self.assertEqual(Open.WO | Open.RW, 3)

@reraise_if_not_enum(HeadlightsK)
def test_global_repr_keep(self):
self.assertEqual(
repr(HeadlightsK(0)),
Expand All @@ -3651,6 +3691,7 @@ def test_global_repr_keep(self):
'%(m)s.HeadlightsK(8)' % {'m': SHORT_MODULE},
)

@reraise_if_not_enum(HeadlightsC)
def test_global_repr_conform1(self):
self.assertEqual(
repr(HeadlightsC(0)),
Expand All @@ -3665,6 +3706,7 @@ def test_global_repr_conform1(self):
'%(m)s.OFF_C' % {'m': SHORT_MODULE},
)

@reraise_if_not_enum(NoName)
def test_global_enum_str(self):
self.assertEqual(str(NoName.ONE & NoName.TWO), 'NoName(0)')
self.assertEqual(str(NoName(0)), 'NoName(0)')
Expand Down