]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-125259: Fix error notes removal in enum initialization (GH-125647) (GH...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 25 Oct 2024 03:24:23 +0000 (05:24 +0200)
committerGitHub <noreply@github.com>
Fri, 25 Oct 2024 03:24:23 +0000 (20:24 -0700)
gh-125259: Fix error notes removal in enum initialization (GH-125647)
(cherry picked from commit 34653bba644aa5481613f398153757d7357e39ea)

Co-authored-by: Mario Šaško <mariosasko777@gmail.com>
Lib/enum.py
Lib/test/test_enum.py
Misc/NEWS.d/next/Library/2024-10-17-16-10-29.gh-issue-125259.oMew0c.rst [new file with mode: 0644]

index ac13e2c733061b7f58186353221666c78d9ccd91..fc765643692db217aff733b0a28cc76a3ff0306b 100644 (file)
@@ -564,22 +564,16 @@ class EnumType(type):
         classdict['_all_bits_'] = 0
         classdict['_inverted_'] = None
         try:
-            exc = None
             classdict['_%s__in_progress' % cls] = True
             enum_class = super().__new__(metacls, cls, bases, classdict, **kwds)
             classdict['_%s__in_progress' % cls] = False
             delattr(enum_class, '_%s__in_progress' % cls)
         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__
+            raise
         # update classdict with any changes made by __init_subclass__
         classdict.update(enum_class.__dict__)
         #
index a99347191f88a83aefadc93febeb8655e9d12c32..e9948de39ed5997fbf597ad57a83c3f9ff3f207d 100644 (file)
@@ -1902,6 +1902,25 @@ class TestSpecial(unittest.TestCase):
             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 (file)
index 0000000..4fa6330
--- /dev/null
@@ -0,0 +1 @@
+Fix the notes removal logic for errors thrown in enum initialization.