Skip to content

Commit

Permalink
[Enum] update class creation for RuntimeError changes (pythonGH-111815)
Browse files Browse the repository at this point in the history
(cherry picked from commit f9e6ce0)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
  • Loading branch information
ethanfurman authored and miss-islington committed Nov 29, 2023
1 parent 3e6b328 commit ff5bb61
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
1 change: 0 additions & 1 deletion Doc/howto/enum.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::

Expand Down
14 changes: 9 additions & 5 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,12 +581,16 @@ def __new__(metacls, cls, bases, classdict, *, boundary=None, _simple=False, **k
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__)
Expand Down

0 comments on commit ff5bb61

Please sign in to comment.