diff options
author | Ethan Furman <ethan@stoneleaf.us> | 2016-09-11 20:25:26 (GMT) |
---|---|---|
committer | Ethan Furman <ethan@stoneleaf.us> | 2016-09-11 20:25:26 (GMT) |
commit | 06339e7493bc696b7b9213437ce0182048b37bab (patch) | |
tree | 1944c5d23596a727bc1a7739290bb4a8b473ff6a /Lib/enum.py | |
parent | 92dec548ff1494b86f08bd3753ca91a9330b4ea9 (diff) | |
download | cpython-06339e7493bc696b7b9213437ce0182048b37bab.zip cpython-06339e7493bc696b7b9213437ce0182048b37bab.tar.gz cpython-06339e7493bc696b7b9213437ce0182048b37bab.tar.bz2 |
Enum._convert: sort by value, then by name
Diffstat (limited to 'Lib/enum.py')
-rw-r--r-- | Lib/enum.py | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/Lib/enum.py b/Lib/enum.py index 1f87664..d830320 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -616,9 +616,16 @@ class Enum(metaclass=EnumMeta): # 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__) |