[Python-checkins] cpython: Enum._convert: sort by value, then by name

ethan.furman python-checkins at python.org
Sun Sep 11 16:25:49 EDT 2016


https://hg.python.org/cpython/rev/5637c9b4dd4c
changeset:   103653:5637c9b4dd4c
user:        Ethan Furman <ethan at stoneleaf.us>
date:        Sun Sep 11 13:25:26 2016 -0700
summary:
  Enum._convert: sort by value, then by name

files:
  Lib/enum.py |  13 ++++++++++---
  1 files changed, 10 insertions(+), 3 deletions(-)


diff --git a/Lib/enum.py b/Lib/enum.py
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -616,9 +616,16 @@
         # 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))
+        members = [
+                (name, source[name])
+                for name in source.keys()
+                if filter(name)]
+        try:
+            # sort by value
+            members.sort(key=lambda t: (t[1], t[0]))
+        except TypeError:
+            # unless some values aren't comparable, in which case sort by name
+            members.sort(key=lambda t: t[0])
         cls = cls(name, members, module=module)
         cls.__reduce_ex__ = _reduce_ex_by_name
         module_globals.update(cls.__members__)

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list