mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
gh-105522: [Enum] Correctly handle possible exceptions during testing (GH-105523)
This commit is contained in:
parent
fce93c80ae
commit
199438b7cc
1 changed files with 94 additions and 52 deletions
|
@ -38,6 +38,25 @@ def load_tests(loader, tests, ignore):
|
||||||
))
|
))
|
||||||
return tests
|
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__
|
MODULE = __name__
|
||||||
SHORT_MODULE = MODULE.split('.')[-1]
|
SHORT_MODULE = MODULE.split('.')[-1]
|
||||||
|
|
||||||
|
@ -75,30 +94,42 @@ try:
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
FlagStooges = exc
|
FlagStooges = exc
|
||||||
|
|
||||||
class FlagStoogesWithZero(Flag):
|
try:
|
||||||
NOFLAG = 0
|
class FlagStoogesWithZero(Flag):
|
||||||
LARRY = 1
|
NOFLAG = 0
|
||||||
CURLY = 2
|
LARRY = 1
|
||||||
MOE = 4
|
CURLY = 2
|
||||||
BIG = 389
|
MOE = 4
|
||||||
|
BIG = 389
|
||||||
|
except Exception as exc:
|
||||||
|
FlagStoogesWithZero = exc
|
||||||
|
|
||||||
class IntFlagStooges(IntFlag):
|
try:
|
||||||
LARRY = 1
|
class IntFlagStooges(IntFlag):
|
||||||
CURLY = 2
|
LARRY = 1
|
||||||
MOE = 4
|
CURLY = 2
|
||||||
BIG = 389
|
MOE = 4
|
||||||
|
BIG = 389
|
||||||
|
except Exception as exc:
|
||||||
|
IntFlagStooges = exc
|
||||||
|
|
||||||
class IntFlagStoogesWithZero(IntFlag):
|
try:
|
||||||
NOFLAG = 0
|
class IntFlagStoogesWithZero(IntFlag):
|
||||||
LARRY = 1
|
NOFLAG = 0
|
||||||
CURLY = 2
|
LARRY = 1
|
||||||
MOE = 4
|
CURLY = 2
|
||||||
BIG = 389
|
MOE = 4
|
||||||
|
BIG = 389
|
||||||
|
except Exception as exc:
|
||||||
|
IntFlagStoogesWithZero = exc
|
||||||
|
|
||||||
# for pickle test and subclass tests
|
# for pickle test and subclass tests
|
||||||
class Name(StrEnum):
|
try:
|
||||||
BDFL = 'Guido van Rossum'
|
class Name(StrEnum):
|
||||||
FLUFL = 'Barry Warsaw'
|
BDFL = 'Guido van Rossum'
|
||||||
|
FLUFL = 'Barry Warsaw'
|
||||||
|
except Exception as exc:
|
||||||
|
Name = exc
|
||||||
|
|
||||||
try:
|
try:
|
||||||
Question = Enum('Question', 'who what when where why', module=__name__)
|
Question = Enum('Question', 'who what when where why', module=__name__)
|
||||||
|
@ -204,26 +235,35 @@ class classproperty:
|
||||||
|
|
||||||
# for global repr tests
|
# for global repr tests
|
||||||
|
|
||||||
@enum.global_enum
|
try:
|
||||||
class HeadlightsK(IntFlag, boundary=enum.KEEP):
|
@enum.global_enum
|
||||||
OFF_K = 0
|
class HeadlightsK(IntFlag, boundary=enum.KEEP):
|
||||||
LOW_BEAM_K = auto()
|
OFF_K = 0
|
||||||
HIGH_BEAM_K = auto()
|
LOW_BEAM_K = auto()
|
||||||
FOG_K = auto()
|
HIGH_BEAM_K = auto()
|
||||||
|
FOG_K = auto()
|
||||||
|
except Exception as exc:
|
||||||
|
HeadlightsK = exc
|
||||||
|
|
||||||
|
|
||||||
@enum.global_enum
|
try:
|
||||||
class HeadlightsC(IntFlag, boundary=enum.CONFORM):
|
@enum.global_enum
|
||||||
OFF_C = 0
|
class HeadlightsC(IntFlag, boundary=enum.CONFORM):
|
||||||
LOW_BEAM_C = auto()
|
OFF_C = 0
|
||||||
HIGH_BEAM_C = auto()
|
LOW_BEAM_C = auto()
|
||||||
FOG_C = auto()
|
HIGH_BEAM_C = auto()
|
||||||
|
FOG_C = auto()
|
||||||
|
except Exception as exc:
|
||||||
|
HeadlightsC = exc
|
||||||
|
|
||||||
|
|
||||||
@enum.global_enum
|
try:
|
||||||
class NoName(Flag):
|
@enum.global_enum
|
||||||
ONE = 1
|
class NoName(Flag):
|
||||||
TWO = 2
|
ONE = 1
|
||||||
|
TWO = 2
|
||||||
|
except Exception as exc:
|
||||||
|
NoName = exc
|
||||||
|
|
||||||
|
|
||||||
# tests
|
# tests
|
||||||
|
@ -1124,9 +1164,8 @@ class TestSpecial(unittest.TestCase):
|
||||||
green = 2
|
green = 2
|
||||||
blue = 3
|
blue = 3
|
||||||
|
|
||||||
|
@reraise_if_not_enum(Theory)
|
||||||
def test_enum_function_with_qualname(self):
|
def test_enum_function_with_qualname(self):
|
||||||
if isinstance(Theory, Exception):
|
|
||||||
raise Theory
|
|
||||||
self.assertEqual(Theory.__qualname__, 'spanish_inquisition')
|
self.assertEqual(Theory.__qualname__, 'spanish_inquisition')
|
||||||
|
|
||||||
def test_enum_of_types(self):
|
def test_enum_of_types(self):
|
||||||
|
@ -1355,6 +1394,7 @@ class TestSpecial(unittest.TestCase):
|
||||||
test_pickle_dump_load(self.assertIs, MyUnBrokenEnum.I)
|
test_pickle_dump_load(self.assertIs, MyUnBrokenEnum.I)
|
||||||
test_pickle_dump_load(self.assertIs, MyUnBrokenEnum)
|
test_pickle_dump_load(self.assertIs, MyUnBrokenEnum)
|
||||||
|
|
||||||
|
@reraise_if_not_enum(FloatStooges)
|
||||||
def test_floatenum_fromhex(self):
|
def test_floatenum_fromhex(self):
|
||||||
h = float.hex(FloatStooges.MOE.value)
|
h = float.hex(FloatStooges.MOE.value)
|
||||||
self.assertIs(FloatStooges.fromhex(h), FloatStooges.MOE)
|
self.assertIs(FloatStooges.fromhex(h), FloatStooges.MOE)
|
||||||
|
@ -1475,6 +1515,7 @@ class TestSpecial(unittest.TestCase):
|
||||||
self.assertIs(ThreePart((3, 3.0, 'three')), ThreePart.THREE)
|
self.assertIs(ThreePart((3, 3.0, 'three')), ThreePart.THREE)
|
||||||
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):
|
def test_intenum_from_bytes(self):
|
||||||
self.assertIs(IntStooges.from_bytes(b'\x00\x03', 'big'), IntStooges.MOE)
|
self.assertIs(IntStooges.from_bytes(b'\x00\x03', 'big'), IntStooges.MOE)
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
|
@ -1503,33 +1544,28 @@ class TestSpecial(unittest.TestCase):
|
||||||
class Huh(MyStr, MyInt, Enum):
|
class Huh(MyStr, MyInt, Enum):
|
||||||
One = 1
|
One = 1
|
||||||
|
|
||||||
|
@reraise_if_not_enum(Stooges)
|
||||||
def test_pickle_enum(self):
|
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.CURLY)
|
||||||
test_pickle_dump_load(self.assertIs, Stooges)
|
test_pickle_dump_load(self.assertIs, Stooges)
|
||||||
|
|
||||||
|
@reraise_if_not_enum(IntStooges)
|
||||||
def test_pickle_int(self):
|
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.CURLY)
|
||||||
test_pickle_dump_load(self.assertIs, IntStooges)
|
test_pickle_dump_load(self.assertIs, IntStooges)
|
||||||
|
|
||||||
|
@reraise_if_not_enum(FloatStooges)
|
||||||
def test_pickle_float(self):
|
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.CURLY)
|
||||||
test_pickle_dump_load(self.assertIs, FloatStooges)
|
test_pickle_dump_load(self.assertIs, FloatStooges)
|
||||||
|
|
||||||
|
@reraise_if_not_enum(Answer)
|
||||||
def test_pickle_enum_function(self):
|
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.him)
|
||||||
test_pickle_dump_load(self.assertIs, Answer)
|
test_pickle_dump_load(self.assertIs, Answer)
|
||||||
|
|
||||||
|
@reraise_if_not_enum(Question)
|
||||||
def test_pickle_enum_function_with_module(self):
|
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.who)
|
||||||
test_pickle_dump_load(self.assertIs, Question)
|
test_pickle_dump_load(self.assertIs, Question)
|
||||||
|
|
||||||
|
@ -1592,9 +1628,8 @@ class TestSpecial(unittest.TestCase):
|
||||||
[Season.SUMMER, Season.WINTER, Season.AUTUMN, Season.SPRING],
|
[Season.SUMMER, Season.WINTER, Season.AUTUMN, Season.SPRING],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@reraise_if_not_enum(Name)
|
||||||
def test_subclassing(self):
|
def test_subclassing(self):
|
||||||
if isinstance(Name, Exception):
|
|
||||||
raise Name
|
|
||||||
self.assertEqual(Name.BDFL, 'Guido van Rossum')
|
self.assertEqual(Name.BDFL, 'Guido van Rossum')
|
||||||
self.assertTrue(Name.BDFL, Name('Guido van Rossum'))
|
self.assertTrue(Name.BDFL, Name('Guido van Rossum'))
|
||||||
self.assertIs(Name.BDFL, getattr(Name, 'BDFL'))
|
self.assertIs(Name.BDFL, getattr(Name, 'BDFL'))
|
||||||
|
@ -3330,9 +3365,13 @@ class OldTestFlag(unittest.TestCase):
|
||||||
self.assertIn(e, Perm)
|
self.assertIn(e, Perm)
|
||||||
self.assertIs(type(e), Perm)
|
self.assertIs(type(e), Perm)
|
||||||
|
|
||||||
|
@reraise_if_not_enum(
|
||||||
|
FlagStooges,
|
||||||
|
FlagStoogesWithZero,
|
||||||
|
IntFlagStooges,
|
||||||
|
IntFlagStoogesWithZero,
|
||||||
|
)
|
||||||
def test_pickle(self):
|
def test_pickle(self):
|
||||||
if isinstance(FlagStooges, Exception):
|
|
||||||
raise FlagStooges
|
|
||||||
test_pickle_dump_load(self.assertIs, FlagStooges.CURLY)
|
test_pickle_dump_load(self.assertIs, FlagStooges.CURLY)
|
||||||
test_pickle_dump_load(self.assertEqual,
|
test_pickle_dump_load(self.assertEqual,
|
||||||
FlagStooges.CURLY|FlagStooges.MOE)
|
FlagStooges.CURLY|FlagStooges.MOE)
|
||||||
|
@ -3637,6 +3676,7 @@ class OldTestIntFlag(unittest.TestCase):
|
||||||
self.assertTrue(isinstance(Open.WO | Open.RW, Open))
|
self.assertTrue(isinstance(Open.WO | Open.RW, Open))
|
||||||
self.assertEqual(Open.WO | Open.RW, 3)
|
self.assertEqual(Open.WO | Open.RW, 3)
|
||||||
|
|
||||||
|
@reraise_if_not_enum(HeadlightsK)
|
||||||
def test_global_repr_keep(self):
|
def test_global_repr_keep(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
repr(HeadlightsK(0)),
|
repr(HeadlightsK(0)),
|
||||||
|
@ -3651,6 +3691,7 @@ class OldTestIntFlag(unittest.TestCase):
|
||||||
'%(m)s.HeadlightsK(8)' % {'m': SHORT_MODULE},
|
'%(m)s.HeadlightsK(8)' % {'m': SHORT_MODULE},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@reraise_if_not_enum(HeadlightsC)
|
||||||
def test_global_repr_conform1(self):
|
def test_global_repr_conform1(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
repr(HeadlightsC(0)),
|
repr(HeadlightsC(0)),
|
||||||
|
@ -3665,6 +3706,7 @@ class OldTestIntFlag(unittest.TestCase):
|
||||||
'%(m)s.OFF_C' % {'m': SHORT_MODULE},
|
'%(m)s.OFF_C' % {'m': SHORT_MODULE},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@reraise_if_not_enum(NoName)
|
||||||
def test_global_enum_str(self):
|
def test_global_enum_str(self):
|
||||||
self.assertEqual(str(NoName.ONE & NoName.TWO), 'NoName(0)')
|
self.assertEqual(str(NoName.ONE & NoName.TWO), 'NoName(0)')
|
||||||
self.assertEqual(str(NoName(0)), 'NoName(0)')
|
self.assertEqual(str(NoName(0)), 'NoName(0)')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue