issue23673

add private method to enum to support replacing global constants with Enum members:
- search for candidate constants via supplied filter
- create new enum class and members
- insert enum class and replace constants with members via supplied module name
- replace __reduce_ex__ with function that returns member name, so previous Python versions can unpickle
modify IntEnum classes to use new method
This commit is contained in:
Ethan Furman 2015-03-18 18:19:30 -07:00
parent d833779cea
commit 482fe0477e
4 changed files with 47 additions and 8 deletions

View file

@ -69,15 +69,15 @@ __all__.extend(os._get_exports_list(_socket))
# Note that _socket only knows about the integer values. The public interface
# in this module understands the enums and translates them back from integers
# where needed (e.g. .family property of a socket object).
AddressFamily = IntEnum('AddressFamily',
{name: value for name, value in globals().items()
if name.isupper() and name.startswith('AF_')})
globals().update(AddressFamily.__members__)
IntEnum._convert(
'AddressFamily',
__name__,
lambda C: C.isupper() and C.startswith('AF_'))
SocketKind = IntEnum('SocketKind',
{name: value for name, value in globals().items()
if name.isupper() and name.startswith('SOCK_')})
globals().update(SocketKind.__members__)
IntEnum._convert(
'SocketKind',
__name__,
lambda C: C.isupper() and C.startswith('SOCK_'))
def _intenum_converter(value, enum_klass):
"""Convert a numeric family value to an IntEnum member.