mirror of
https://github.com/python/cpython.git
synced 2025-09-09 18:32:22 +00:00
[3.9] bpo-42248: [Enum] ensure exceptions raised in `_missing_
` are released (GH-25350). (GH-25370)
(cherry picked from commit 8c14f5a787
)
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
This commit is contained in:
parent
de06baa9de
commit
6379924ecd
3 changed files with 51 additions and 13 deletions
31
Lib/enum.py
31
Lib/enum.py
|
@ -669,19 +669,24 @@ class Enum(metaclass=EnumMeta):
|
|||
except Exception as e:
|
||||
exc = e
|
||||
result = None
|
||||
if isinstance(result, cls):
|
||||
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)
|
||||
)
|
||||
exc.__context__ = ve_exc
|
||||
raise exc
|
||||
try:
|
||||
if isinstance(result, cls):
|
||||
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)
|
||||
)
|
||||
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