diff options
author | Ethan Furman <ethan@stoneleaf.us> | 2016-08-20 14:19:31 (GMT) |
---|---|---|
committer | Ethan Furman <ethan@stoneleaf.us> | 2016-08-20 14:19:31 (GMT) |
commit | e8e61277ff31d7b5ae87ca21808c4a6fbdab4954 (patch) | |
tree | f44a362609960627ade0521c9b8de0c1dfbc2693 /Lib/enum.py | |
parent | 3e458755782e8f63dd9eb29cb134c2b65657d17f (diff) | |
download | cpython-e8e61277ff31d7b5ae87ca21808c4a6fbdab4954.zip cpython-e8e61277ff31d7b5ae87ca21808c4a6fbdab4954.tar.gz cpython-e8e61277ff31d7b5ae87ca21808c4a6fbdab4954.tar.bz2 |
issue26981: add _order_ compatibility shim to enum.Enum
Diffstat (limited to 'Lib/enum.py')
-rw-r--r-- | Lib/enum.py | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/Lib/enum.py b/Lib/enum.py index 99db9e6..e7889a8 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -64,9 +64,11 @@ class _EnumDict(dict): """ if _is_sunder(key): - raise ValueError('_names_ are reserved for future Enum use') + if key not in ('_order_', ): + raise ValueError('_names_ are reserved for future Enum use') elif _is_dunder(key): - pass + if key == '__order__': + key = '_order_' elif key in self._member_names: # descriptor overwriting an enum? raise TypeError('Attempted to reuse key: %r' % key) @@ -106,6 +108,9 @@ class EnumMeta(type): for name in classdict._member_names: del classdict[name] + # adjust the sunders + _order_ = classdict.pop('_order_', None) + # check for illegal enum names (any others?) invalid_names = set(members) & {'mro', } if invalid_names: @@ -210,6 +215,14 @@ class EnumMeta(type): if save_new: enum_class.__new_member__ = __new__ enum_class.__new__ = Enum.__new__ + + # py3 support for definition order (helps keep py2/py3 code in sync) + if _order_ is not None: + if isinstance(_order_, str): + _order_ = _order_.replace(',', ' ').split() + if _order_ != enum_class._member_names_: + raise TypeError('member order does not match _order_') + return enum_class def __bool__(self): |