summaryrefslogtreecommitdiffstats
path: root/Lib/enum.py
diff options
context:
space:
mode:
authorAndrew Svetlov <andrew.svetlov@gmail.com>2018-12-26 18:45:33 (GMT)
committerEthan Furman <ethan@stoneleaf.us>2018-12-26 18:45:33 (GMT)
commit34ae04f74dcf4ac97d07c3e82eaf8f619d80cedb (patch)
treed56bc8cc27a87025e8fb480eb47249534a136511 /Lib/enum.py
parent3a81076bbf899b1a549f005dd9299e7ae0946321 (diff)
downloadcpython-34ae04f74dcf4ac97d07c3e82eaf8f619d80cedb.zip
cpython-34ae04f74dcf4ac97d07c3e82eaf8f619d80cedb.tar.gz
cpython-34ae04f74dcf4ac97d07c3e82eaf8f619d80cedb.tar.bz2
Speed-up building enums by value, e.g. http.HTTPStatus(200) (#11318)
bpo-35585: Speed up enum by-value lookup
Diffstat (limited to 'Lib/enum.py')
-rw-r--r--Lib/enum.py6
1 files changed, 4 insertions, 2 deletions
diff --git a/Lib/enum.py b/Lib/enum.py
index fec1aed..f7452f0 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -563,8 +563,10 @@ class Enum(metaclass=EnumMeta):
# by-value search for a matching enum member
# see if it's in the reverse mapping (for hashable values)
try:
- if value in cls._value2member_map_:
- return cls._value2member_map_[value]
+ return cls._value2member_map_[value]
+ except KeyError:
+ # Not found, no need to do long O(n) search
+ pass
except TypeError:
# not there, now do long search -- O(n) behavior
for member in cls._member_map_.values():