summaryrefslogtreecommitdiffstats
path: root/Lib/enum.py
diff options
context:
space:
mode:
authorEthan Furman <ethan@stoneleaf.us>2018-09-12 18:43:34 (GMT)
committerGitHub <noreply@github.com>2018-09-12 18:43:34 (GMT)
commit019f0a0cb85ebc234356415f3638b9bd77528e55 (patch)
tree283d1c2dacb285c8f091bf4bb1f79e7520026fb1 /Lib/enum.py
parenta5d1eb8d8b7add31b5f5d9bbb31cee1a491b2c08 (diff)
downloadcpython-019f0a0cb85ebc234356415f3638b9bd77528e55.zip
cpython-019f0a0cb85ebc234356415f3638b9bd77528e55.tar.gz
cpython-019f0a0cb85ebc234356415f3638b9bd77528e55.tar.bz2
bpo-34536: raise error for invalid _missing_ results (GH-9147)
* raise exception if _missing_ returns None or invalid type
Diffstat (limited to 'Lib/enum.py')
-rw-r--r--Lib/enum.py20
1 files changed, 19 insertions, 1 deletions
diff --git a/Lib/enum.py b/Lib/enum.py
index 0839671..02405c8 100644
--- a/Lib/enum.py
+++ b/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):