mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
bpo-33866: enum: Stop using OrderedDict (GH-7698)
This commit is contained in:
parent
ea3dc8029a
commit
e57f91a0f0
2 changed files with 7 additions and 15 deletions
|
@ -281,7 +281,7 @@ Iterating over the members of an enum does not provide the aliases::
|
||||||
>>> list(Shape)
|
>>> list(Shape)
|
||||||
[<Shape.SQUARE: 2>, <Shape.DIAMOND: 1>, <Shape.CIRCLE: 3>]
|
[<Shape.SQUARE: 2>, <Shape.DIAMOND: 1>, <Shape.CIRCLE: 3>]
|
||||||
|
|
||||||
The special attribute ``__members__`` is an ordered dictionary mapping names
|
The special attribute ``__members__`` is a read-only ordered mapping of names
|
||||||
to members. It includes all names defined in the enumeration, including the
|
to members. It includes all names defined in the enumeration, including the
|
||||||
aliases::
|
aliases::
|
||||||
|
|
||||||
|
@ -998,7 +998,7 @@ Finer Points
|
||||||
Supported ``__dunder__`` names
|
Supported ``__dunder__`` names
|
||||||
""""""""""""""""""""""""""""""
|
""""""""""""""""""""""""""""""
|
||||||
|
|
||||||
:attr:`__members__` is an :class:`OrderedDict` of ``member_name``:``member``
|
:attr:`__members__` is a read-only ordered mapping of ``member_name``:``member``
|
||||||
items. It is only available on the class.
|
items. It is only available on the class.
|
||||||
|
|
||||||
:meth:`__new__`, if specified, must create and return the enum members; it is
|
:meth:`__new__`, if specified, must create and return the enum members; it is
|
||||||
|
|
18
Lib/enum.py
18
Lib/enum.py
|
@ -1,12 +1,6 @@
|
||||||
import sys
|
import sys
|
||||||
from types import MappingProxyType, DynamicClassAttribute
|
from types import MappingProxyType, DynamicClassAttribute
|
||||||
|
|
||||||
# try _collections first to reduce startup cost
|
|
||||||
try:
|
|
||||||
from _collections import OrderedDict
|
|
||||||
except ImportError:
|
|
||||||
from collections import OrderedDict
|
|
||||||
|
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'EnumMeta',
|
'EnumMeta',
|
||||||
|
@ -168,7 +162,7 @@ class EnumMeta(type):
|
||||||
# create our new Enum type
|
# create our new Enum type
|
||||||
enum_class = super().__new__(metacls, cls, bases, classdict)
|
enum_class = super().__new__(metacls, cls, bases, classdict)
|
||||||
enum_class._member_names_ = [] # names in definition order
|
enum_class._member_names_ = [] # names in definition order
|
||||||
enum_class._member_map_ = OrderedDict() # name->value map
|
enum_class._member_map_ = {} # name->value map
|
||||||
enum_class._member_type_ = member_type
|
enum_class._member_type_ = member_type
|
||||||
|
|
||||||
# save attributes from super classes so we know if we can take
|
# save attributes from super classes so we know if we can take
|
||||||
|
@ -630,14 +624,12 @@ class Enum(metaclass=EnumMeta):
|
||||||
source = vars(source)
|
source = vars(source)
|
||||||
else:
|
else:
|
||||||
source = module_globals
|
source = module_globals
|
||||||
# We use an OrderedDict of sorted source keys so that the
|
# _value2member_map_ is populated in the same order every time
|
||||||
# _value2member_map is populated in the same order every time
|
|
||||||
# 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.
|
||||||
# between runs due to hash randomization of the module dictionary.
|
|
||||||
members = [
|
members = [
|
||||||
(name, source[name])
|
(name, value)
|
||||||
for name in source.keys()
|
for name, value in source.items()
|
||||||
if filter(name)]
|
if filter(name)]
|
||||||
try:
|
try:
|
||||||
# sort by value
|
# sort by value
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue