From 43d1e1deb21e5b1998c349e5ae706586addc531e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20=C5=A0a=C5=A1ko?= Date: Tue, 22 Oct 2024 22:42:22 +0200 Subject: [PATCH 1/2] gh-125259: Fix error notes removal in enum initialization (GH-125647) (cherry picked from commit 34653bba644aa5481613f398153757d7357e39ea) --- Lib/enum.py | 17 ++++++----------- Lib/test/test_enum.py | 19 +++++++++++++++++++ ...-10-17-16-10-29.gh-issue-125259.oMew0c.rst | 1 + 3 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2024-10-17-16-10-29.gh-issue-125259.oMew0c.rst diff --git a/Lib/enum.py b/Lib/enum.py index d9859b3c0a9f98..85e4c72106127f 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -592,19 +592,14 @@ def __new__(metacls, cls, bases, classdict, *, boundary=None, _simple=False, **k classdict['_all_bits_'] = 0 classdict['_inverted_'] = None try: - exc = None enum_class = super().__new__(metacls, cls, bases, classdict, **kwds) except Exception as e: - # since 3.12 the line "Error calling __set_name__ on '_proto_member' instance ..." - # is tacked on to the error instead of raising a RuntimeError - # recreate the exception to discard - exc = type(e)(str(e)) - exc.__cause__ = e.__cause__ - exc.__context__ = e.__context__ - tb = e.__traceback__ - if exc is not None: - raise exc.with_traceback(tb) - # + # since 3.12 the note "Error calling __set_name__ on '_proto_member' instance ..." + # is tacked on to the error instead of raising a RuntimeError, so discard it + if hasattr(e, '__notes__'): + del e.__notes__ + del e + raise # update classdict with any changes made by __init_subclass__ classdict.update(enum_class.__dict__) # diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index aff5b7f68f01bc..2e50ae0fe96586 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -1852,6 +1852,25 @@ def test_wrong_inheritance_order(self): class Wrong(Enum, str): 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): class number(IntEnum): one = 1 diff --git a/Misc/NEWS.d/next/Library/2024-10-17-16-10-29.gh-issue-125259.oMew0c.rst b/Misc/NEWS.d/next/Library/2024-10-17-16-10-29.gh-issue-125259.oMew0c.rst new file mode 100644 index 00000000000000..4fa6330abea512 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-10-17-16-10-29.gh-issue-125259.oMew0c.rst @@ -0,0 +1 @@ +Fix the notes removal logic for errors thrown in enum initialization. From 96b6db29d5c137b7f9d27ef6e468f39d80b113c4 Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Thu, 24 Oct 2024 20:19:23 -0700 Subject: [PATCH 2/2] remove del --- Lib/enum.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/enum.py b/Lib/enum.py index 85e4c72106127f..eaa517e2fbc39b 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -598,7 +598,6 @@ def __new__(metacls, cls, bases, classdict, *, boundary=None, _simple=False, **k # is tacked on to the error instead of raising a RuntimeError, so discard it if hasattr(e, '__notes__'): del e.__notes__ - del e raise # update classdict with any changes made by __init_subclass__ classdict.update(enum_class.__dict__)