mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
bpo-42248: [Enum] ensure exceptions raised in `_missing_
` are released (GH-25350)
This commit is contained in:
parent
67c0b3d89c
commit
8c14f5a787
3 changed files with 57 additions and 19 deletions
43
Lib/enum.py
43
Lib/enum.py
|
@ -919,25 +919,30 @@ class Enum(metaclass=EnumType):
|
|||
except Exception as e:
|
||||
exc = e
|
||||
result = None
|
||||
if isinstance(result, cls):
|
||||
return result
|
||||
elif (
|
||||
Flag is not None and issubclass(cls, Flag)
|
||||
and cls._boundary_ is EJECT and isinstance(result, int)
|
||||
):
|
||||
return result
|
||||
else:
|
||||
ve_exc = ValueError("%r is not a valid %s" % (value, cls.__qualname__))
|
||||
if result is None and exc is None:
|
||||
raise ve_exc
|
||||
elif exc is None:
|
||||
exc = TypeError(
|
||||
'error in %s._missing_: returned %r instead of None or a valid member'
|
||||
% (cls.__name__, result)
|
||||
)
|
||||
if not isinstance(exc, ValueError):
|
||||
exc.__context__ = ve_exc
|
||||
raise exc
|
||||
try:
|
||||
if isinstance(result, cls):
|
||||
return result
|
||||
elif (
|
||||
Flag is not None and issubclass(cls, Flag)
|
||||
and cls._boundary_ is EJECT and isinstance(result, int)
|
||||
):
|
||||
return result
|
||||
else:
|
||||
ve_exc = ValueError("%r is not a valid %s" % (value, cls.__qualname__))
|
||||
if result is None and exc is None:
|
||||
raise ve_exc
|
||||
elif exc is None:
|
||||
exc = TypeError(
|
||||
'error in %s._missing_: returned %r instead of None or a valid member'
|
||||
% (cls.__name__, result)
|
||||
)
|
||||
if not isinstance(exc, ValueError):
|
||||
exc.__context__ = ve_exc
|
||||
raise exc
|
||||
finally:
|
||||
# ensure all variables that could hold an exception are destroyed
|
||||
exc = None
|
||||
ve_exc = None
|
||||
|
||||
def _generate_next_value_(name, start, count, last_values):
|
||||
"""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue