mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
bpo-34536: raise error for invalid _missing_ results (GH-9147)
* raise exception if _missing_ returns None or invalid type
This commit is contained in:
parent
a5d1eb8d8b
commit
019f0a0cb8
3 changed files with 54 additions and 1 deletions
20
Lib/enum.py
20
Lib/enum.py
|
@ -585,7 +585,25 @@ class Enum(metaclass=EnumMeta):
|
|||
if member._value_ == value:
|
||||
return member
|
||||
# still not found -- try _missing_ hook
|
||||
return cls._missing_(value)
|
||||
try:
|
||||
exc = None
|
||||
result = cls._missing_(value)
|
||||
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.__name__))
|
||||
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
|
||||
|
||||
def _generate_next_value_(name, start, count, last_values):
|
||||
for last_value in reversed(last_values):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue