mirror of https://github.com/python/cpython
gh-125259: Fix error notes removal in enum initialization (GH-125647)
This commit is contained in:
parent
aaed91cabc
commit
34653bba64
16
Lib/enum.py
16
Lib/enum.py
|
@ -557,22 +557,16 @@ class EnumType(type):
|
||||||
classdict['_all_bits_'] = 0
|
classdict['_all_bits_'] = 0
|
||||||
classdict['_inverted_'] = None
|
classdict['_inverted_'] = None
|
||||||
try:
|
try:
|
||||||
exc = None
|
|
||||||
classdict['_%s__in_progress' % cls] = True
|
classdict['_%s__in_progress' % cls] = True
|
||||||
enum_class = super().__new__(metacls, cls, bases, classdict, **kwds)
|
enum_class = super().__new__(metacls, cls, bases, classdict, **kwds)
|
||||||
classdict['_%s__in_progress' % cls] = False
|
classdict['_%s__in_progress' % cls] = False
|
||||||
delattr(enum_class, '_%s__in_progress' % cls)
|
delattr(enum_class, '_%s__in_progress' % cls)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# since 3.12 the line "Error calling __set_name__ on '_proto_member' instance ..."
|
# since 3.12 the note "Error calling __set_name__ on '_proto_member' instance ..."
|
||||||
# is tacked on to the error instead of raising a RuntimeError
|
# is tacked on to the error instead of raising a RuntimeError, so discard it
|
||||||
# recreate the exception to discard
|
if hasattr(e, '__notes__'):
|
||||||
exc = type(e)(str(e))
|
del e.__notes__
|
||||||
exc.__cause__ = e.__cause__
|
raise
|
||||||
exc.__context__ = e.__context__
|
|
||||||
tb = e.__traceback__
|
|
||||||
if exc is not None:
|
|
||||||
raise exc.with_traceback(tb)
|
|
||||||
#
|
|
||||||
# update classdict with any changes made by __init_subclass__
|
# update classdict with any changes made by __init_subclass__
|
||||||
classdict.update(enum_class.__dict__)
|
classdict.update(enum_class.__dict__)
|
||||||
#
|
#
|
||||||
|
|
|
@ -1888,6 +1888,25 @@ class TestSpecial(unittest.TestCase):
|
||||||
class Wrong(Enum, str):
|
class Wrong(Enum, str):
|
||||||
NotHere = 'error before this point'
|
NotHere = 'error before this point'
|
||||||
|
|
||||||
|
def test_raise_custom_error_on_creation(self):
|
||||||
|
class InvalidRgbColorError(ValueError):
|
||||||
|
def __init__(self, r, g, b):
|
||||||
|
self.r = r
|
||||||
|
self.g = g
|
||||||
|
self.b = b
|
||||||
|
super().__init__(f'({r}, {g}, {b}) is not a valid RGB color')
|
||||||
|
|
||||||
|
with self.assertRaises(InvalidRgbColorError):
|
||||||
|
class RgbColor(Enum):
|
||||||
|
RED = (255, 0, 0)
|
||||||
|
GREEN = (0, 255, 0)
|
||||||
|
BLUE = (0, 0, 255)
|
||||||
|
INVALID = (256, 0, 0)
|
||||||
|
|
||||||
|
def __init__(self, r, g, b):
|
||||||
|
if not all(0 <= val <= 255 for val in (r, g, b)):
|
||||||
|
raise InvalidRgbColorError(r, g, b)
|
||||||
|
|
||||||
def test_intenum_transitivity(self):
|
def test_intenum_transitivity(self):
|
||||||
class number(IntEnum):
|
class number(IntEnum):
|
||||||
one = 1
|
one = 1
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix the notes removal logic for errors thrown in enum initialization.
|
Loading…
Reference in New Issue