Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
_stdlib_enum -> global_enum
rename to `global_enum` and have `global_enum` update the module's
namespace with the members

remove `_stdlib_enum` where the enums were not being exported to the
module's namespace
  • Loading branch information
ethanfurman committed Mar 25, 2021
commit e01379cef9da6612c6417644e24a026d58979861
3 changes: 1 addition & 2 deletions Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import sys
from _ast import *
from contextlib import contextmanager, nullcontext
from enum import IntEnum, auto, _stdlib_enum
from enum import IntEnum, auto


def parse(source, filename='<unknown>', mode='exec', *,
Expand Down Expand Up @@ -636,7 +636,6 @@ class Param(expr_context):
# We unparse those infinities to INFSTR.
_INFSTR = "1e" + repr(sys.float_info.max_10_exp + 1)

@_stdlib_enum
class _Precedence(IntEnum):
"""Precedence table that originated from python grammar."""

Expand Down
1 change: 0 additions & 1 deletion Lib/asyncio/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

# The enum should be here to break circular dependencies between
# base_events and sslproto
@enum._stdlib_enum
class _SendfileMode(enum.Enum):
UNSUPPORTED = enum.auto()
TRY_NATIVE = enum.auto()
Expand Down
18 changes: 9 additions & 9 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'auto', 'unique',
'property',
'FlagBoundary', 'STRICT', 'CONFORM', 'EJECT', 'KEEP',
'global_flag_repr', 'global_int_repr', 'global_enum_str',
'global_flag_repr', 'global_int_repr', 'global_enum',
]


Expand Down Expand Up @@ -777,8 +777,7 @@ def _convert_(cls, name, module, filter, source=None, boundary=None):
members.sort(key=lambda t: t[0])
cls = cls(name, members, module=module, boundary=boundary or KEEP)
cls.__reduce_ex__ = _reduce_ex_by_name
_stdlib_enum(cls)
module_globals.update(cls.__members__)
global_enum(cls)
module_globals[name] = cls
return cls

Expand Down Expand Up @@ -1331,9 +1330,6 @@ def _power_of_two(value):
return False
return value == 2 ** _high_bit(value)

def global_enum_str(self):
return self.name

def global_int_repr(self):
return '%s.%s' % (self.__class__.__module__, self.name)

Expand All @@ -1360,12 +1356,16 @@ def global_flag_repr(self):
res = '~%s' % (res, )
return res

def _stdlib_enum(cls):
def global_enum(cls):
"""
decorator that makes the repr() of an enum member reference its module
instead of its class; also exports all members to the enum's module's
global namespace
"""
if issubclass(cls, Flag):
cls.__repr__ = global_flag_repr
else:
cls.__repr__ = global_int_repr
if str not in cls.__mro__:
cls.__str__ = global_enum_str
vars(sys.modules[cls.__module__]).update(cls.__members__)
return cls

2 changes: 1 addition & 1 deletion Lib/http/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from enum import IntEnum, _stdlib_enum
from enum import IntEnum

__all__ = ['HTTPStatus']

Expand Down
1 change: 0 additions & 1 deletion Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2448,7 +2448,6 @@ class _empty:
"""Marker object for Signature.empty and Parameter.empty."""


@enum._stdlib_enum
class _ParameterKind(enum.IntEnum):
POSITIONAL_ONLY = 0
POSITIONAL_OR_KEYWORD = 1
Expand Down
3 changes: 1 addition & 2 deletions Lib/plistlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@
from xml.parsers.expat import ParserCreate


PlistFormat = enum._stdlib_enum(enum.Enum('PlistFormat', 'FMT_XML FMT_BINARY', module=__name__))
globals().update(PlistFormat.__members__)
PlistFormat = enum.global_enum(enum.Enum('PlistFormat', 'FMT_XML FMT_BINARY', module=__name__))


class UID:
Expand Down
3 changes: 1 addition & 2 deletions Lib/pstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@
import marshal
import re

from enum import Enum, _stdlib_enum
from enum import Enum
from functools import cmp_to_key
from dataclasses import dataclass
from typing import Dict

__all__ = ["Stats", "SortKey", "FunctionProfile", "StatsProfile"]

@_stdlib_enum
class SortKey(str, Enum):
CALLS = 'calls', 'ncalls'
CUMULATIVE = 'cumulative', 'cumtime'
Expand Down
1 change: 0 additions & 1 deletion Lib/py_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def __str__(self):
return self.msg


@enum._stdlib_enum
class PycInvalidationMode(enum.Enum):
TIMESTAMP = 1
CHECKED_HASH = 2
Expand Down
4 changes: 1 addition & 3 deletions Lib/re.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@

__version__ = "2.2.1"

@enum._stdlib_enum
@enum.global_enum
class RegexFlag(enum.IntFlag, boundary=enum.KEEP):
ASCII = A = sre_compile.SRE_FLAG_ASCII # assume ascii "locale"
IGNORECASE = I = sre_compile.SRE_FLAG_IGNORECASE # ignore case
Expand All @@ -155,8 +155,6 @@ class RegexFlag(enum.IntFlag, boundary=enum.KEEP):
TEMPLATE = T = sre_compile.SRE_FLAG_TEMPLATE # disable backtracking
DEBUG = sre_compile.SRE_FLAG_DEBUG # dump pattern after compilation

globals().update(RegexFlag.__members__)

# sre exception
error = sre_compile.error

Expand Down
6 changes: 0 additions & 6 deletions Lib/ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@
import os
from collections import namedtuple
from enum import Enum as _Enum, IntEnum as _IntEnum, IntFlag as _IntFlag
from enum import _stdlib_enum

import _ssl # if we can't import it, let the error propagate

Expand Down Expand Up @@ -156,7 +155,6 @@
_SSLv2_IF_EXISTS = getattr(_SSLMethod, 'PROTOCOL_SSLv2', None)


@_stdlib_enum
class TLSVersion(_IntEnum):
MINIMUM_SUPPORTED = _ssl.PROTO_MINIMUM_SUPPORTED
SSLv3 = _ssl.PROTO_SSLv3
Expand All @@ -167,7 +165,6 @@ class TLSVersion(_IntEnum):
MAXIMUM_SUPPORTED = _ssl.PROTO_MAXIMUM_SUPPORTED


@_stdlib_enum
class _TLSContentType(_IntEnum):
"""Content types (record layer)

Expand All @@ -182,7 +179,6 @@ class _TLSContentType(_IntEnum):
INNER_CONTENT_TYPE = 0x101


@_stdlib_enum
class _TLSAlertType(_IntEnum):
"""Alert types for TLSContentType.ALERT messages

Expand Down Expand Up @@ -224,7 +220,6 @@ class _TLSAlertType(_IntEnum):
NO_APPLICATION_PROTOCOL = 120


@_stdlib_enum
class _TLSMessageType(_IntEnum):
"""Message types (handshake protocol)

Expand Down Expand Up @@ -469,7 +464,6 @@ def fromname(cls, name):
return super().__new__(cls, *_txt2obj(name, name=True))


@_stdlib_enum
class Purpose(_ASN1Object, _Enum):
"""SSLContext purpose flags with X509v3 Extended Key Usage objects
"""
Expand Down
1 change: 0 additions & 1 deletion Lib/tkinter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ def _splitdict(tk, v, cut_minus=True, conv=None):
return dict


enum._stdlib_enum
class EventType(enum.StrEnum):
KeyPress = '2'
Key = KeyPress
Expand Down
3 changes: 1 addition & 2 deletions Lib/uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import os
import sys

from enum import Enum, _stdlib_enum
from enum import Enum


__author__ = 'Ka-Ping Yee <ping@zesty.ca>'
Expand Down Expand Up @@ -75,7 +75,6 @@
bytes_ = bytes # The built-in bytes type


@_stdlib_enum
class SafeUUID(Enum):
safe = 0
unsafe = -1
Expand Down