]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] [Enum] update class creation for RuntimeError changes (GH-111815) (GH-112526)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 29 Nov 2023 21:49:52 +0000 (22:49 +0100)
committerGitHub <noreply@github.com>
Wed, 29 Nov 2023 21:49:52 +0000 (13:49 -0800)
(cherry picked from commit f9e6ce03953e9ee988d55324dc715b0ef2303cfb)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
Doc/howto/enum.rst
Lib/enum.py

index a136c76303c8ef7cb784f26a47a8ebde0902a511..ffdafb749c73a922ab819ade1b312f5892ecaf46 100644 (file)
@@ -1439,7 +1439,6 @@ alias::
     Traceback (most recent call last):
       ...
     ValueError: aliases not allowed in DuplicateFreeEnum:  'GRENE' --> 'GREEN'
-    Error calling __set_name__ on '_proto_member' instance 'GRENE' in 'Color'
 
 .. note::
 
index 4bd3756ec5bc0cef81490f97b32e85e00ac36d07..1502bfe9158520ddf192c7ba09b7682f8986d11d 100644 (file)
@@ -581,12 +581,16 @@ class EnumType(type):
         try:
             exc = None
             enum_class = super().__new__(metacls, cls, bases, classdict, **kwds)
-        except RuntimeError as e:
-            # any exceptions raised by member.__new__ will get converted to a
-            # RuntimeError, so get that original exception back and raise it instead
-            exc = e.__cause__ or e
+        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
+            raise exc.with_traceback(tb)
         #
         # update classdict with any changes made by __init_subclass__
         classdict.update(enum_class.__dict__)