[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:
Ethan Furman 2021-04-12 15:03:29 -07:00 committed by GitHub
parent de06baa9de
commit 6379924ecd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 13 deletions

View file

@ -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):
"""