Commit 06339e74 authored by Ethan Furman's avatar Ethan Furman

Enum._convert: sort by value, then by name

parent 92dec548
...@@ -616,9 +616,16 @@ class Enum(metaclass=EnumMeta): ...@@ -616,9 +616,16 @@ class Enum(metaclass=EnumMeta):
# for a consistent reverse mapping of number to name when there # for a consistent reverse mapping of number to name when there
# are multiple names for the same number rather than varying # are multiple names for the same number rather than varying
# between runs due to hash randomization of the module dictionary. # between runs due to hash randomization of the module dictionary.
members = OrderedDict((name, source[name]) members = [
for name in sorted(source.keys()) (name, source[name])
if filter(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 = cls(name, members, module=module)
cls.__reduce_ex__ = _reduce_ex_by_name cls.__reduce_ex__ = _reduce_ex_by_name
module_globals.update(cls.__members__) module_globals.update(cls.__members__)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment