Skip to content

Commit 32830cf

Browse files
[3.13] gh-125259: Fix error notes removal in enum initialization (GH-125647) (GH-125858)
gh-125259: Fix error notes removal in enum initialization (GH-125647) (cherry picked from commit 34653bb) Co-authored-by: Mario Šaško <[email protected]>
1 parent 083c581 commit 32830cf

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

Lib/enum.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -564,22 +564,16 @@ def __new__(metacls, cls, bases, classdict, *, boundary=None, _simple=False, **k
564564
classdict['_all_bits_'] = 0
565565
classdict['_inverted_'] = None
566566
try:
567-
exc = None
568567
classdict['_%s__in_progress' % cls] = True
569568
enum_class = super().__new__(metacls, cls, bases, classdict, **kwds)
570569
classdict['_%s__in_progress' % cls] = False
571570
delattr(enum_class, '_%s__in_progress' % cls)
572571
except Exception as e:
573-
# since 3.12 the line "Error calling __set_name__ on '_proto_member' instance ..."
574-
# is tacked on to the error instead of raising a RuntimeError
575-
# recreate the exception to discard
576-
exc = type(e)(str(e))
577-
exc.__cause__ = e.__cause__
578-
exc.__context__ = e.__context__
579-
tb = e.__traceback__
580-
if exc is not None:
581-
raise exc.with_traceback(tb)
582-
#
572+
# since 3.12 the note "Error calling __set_name__ on '_proto_member' instance ..."
573+
# is tacked on to the error instead of raising a RuntimeError, so discard it
574+
if hasattr(e, '__notes__'):
575+
del e.__notes__
576+
raise
583577
# update classdict with any changes made by __init_subclass__
584578
classdict.update(enum_class.__dict__)
585579
#

Lib/test/test_enum.py

+19
Original file line numberDiff line numberDiff line change
@@ -1902,6 +1902,25 @@ def test_wrong_inheritance_order(self):
19021902
class Wrong(Enum, str):
19031903
NotHere = 'error before this point'
19041904

1905+
def test_raise_custom_error_on_creation(self):
1906+
class InvalidRgbColorError(ValueError):
1907+
def __init__(self, r, g, b):
1908+
self.r = r
1909+
self.g = g
1910+
self.b = b
1911+
super().__init__(f'({r}, {g}, {b}) is not a valid RGB color')
1912+
1913+
with self.assertRaises(InvalidRgbColorError):
1914+
class RgbColor(Enum):
1915+
RED = (255, 0, 0)
1916+
GREEN = (0, 255, 0)
1917+
BLUE = (0, 0, 255)
1918+
INVALID = (256, 0, 0)
1919+
1920+
def __init__(self, r, g, b):
1921+
if not all(0 <= val <= 255 for val in (r, g, b)):
1922+
raise InvalidRgbColorError(r, g, b)
1923+
19051924
def test_intenum_transitivity(self):
19061925
class number(IntEnum):
19071926
one = 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix the notes removal logic for errors thrown in enum initialization.

0 commit comments

Comments
 (0)