summaryrefslogtreecommitdiff
path: root/Lib/enum.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/enum.py')
-rw-r--r--Lib/enum.py20
1 files changed, 16 insertions, 4 deletions
diff --git a/Lib/enum.py b/Lib/enum.py
index b8787d19b8..99db9e6b7f 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -1,8 +1,14 @@
import sys
-from collections import OrderedDict
from types import MappingProxyType, DynamicClassAttribute
-__all__ = ['Enum', 'IntEnum', 'unique']
+# try _collections first to reduce startup cost
+try:
+ from _collections import OrderedDict
+except ImportError:
+ from collections import OrderedDict
+
+
+__all__ = ['EnumMeta', 'Enum', 'IntEnum', 'unique']
def _is_descriptor(obj):
@@ -544,8 +550,14 @@ class Enum(metaclass=EnumMeta):
source = vars(source)
else:
source = module_globals
- members = {name: value for name, value in source.items()
- if filter(name)}
+ # We use an OrderedDict of sorted source keys so that the
+ # _value2member_map is populated in the same order every time
+ # for a consistent reverse mapping of number to name when there
+ # are multiple names for the same number rather than varying
+ # between runs due to hash randomization of the module dictionary.
+ members = OrderedDict((name, source[name])
+ for name in sorted(source.keys())
+ if filter(name))
cls = cls(name, members, module=module)
cls.__reduce_ex__ = _reduce_ex_by_name
module_globals.update(cls.__members__)