diff --git a/source/monkeyPatches/__init__.py b/source/monkeyPatches/__init__.py index 0f25c62db54..3423218b97f 100644 --- a/source/monkeyPatches/__init__.py +++ b/source/monkeyPatches/__init__.py @@ -13,7 +13,3 @@ def applyMonkeyPatches(): # Apply several monkey patches to comtypes from . import comtypesMonkeyPatches comtypesMonkeyPatches.applyMonkeyPatches() - - # Apply patches to Enum, prevent cyclic references on ValueError during construction - from . import enumPatches - enumPatches.replace__new__() diff --git a/source/monkeyPatches/enumPatches.py b/source/monkeyPatches/enumPatches.py deleted file mode 100644 index 1f6b1d9dc29..00000000000 --- a/source/monkeyPatches/enumPatches.py +++ /dev/null @@ -1,60 +0,0 @@ -# A part of NonVisual Desktop Access (NVDA) -# Copyright (C) 2021 NV Access Limited -# This file is covered by the GNU General Public License. -# See the file COPYING for more details. - - -def replace__new__(): - import enum - # prevent cyclic references on ValueError during construction - enum.Enum.__new__ = _replacement__new__ - - -def _replacement__new__(cls, value): - """ Copied from python standard library enum.py class Enum. - Prevent cyclic references on ValueError during construction. - Local variable exc must be deleted, otherwise: - - ref to exc held by the frame - - ref to traceback held by exc - - ref to frame held by traceback - """ - # all enum instances are actually created during class construction - # without calling this method; this method is called by the metaclass' - # __call__ (i.e. Color(3) ), and by pickle - if type(value) is cls: - # For lookups like Color(Color.RED) - return value - # by-value search for a matching enum member - # see if it's in the reverse mapping (for hashable values) - try: - return cls._value2member_map_[value] - except KeyError: - # Not found, no need to do long O(n) search - pass - except TypeError: - # not there, now do long search -- O(n) behavior - for member in cls._member_map_.values(): - if member._value_ == value: - return member - # still not found -- try _missing_ hook - try: - result = cls._missing_(value) - except Exception as e: - e.__context__ = ValueError("%r is not a valid %s" % (value, cls.__name__)) - raise e - - if isinstance(result, cls): - return result - - ve_exc = ValueError( - "%r is not a valid %s" % (value, cls.__name__) - ) - if result is None: - raise ve_exc - - te_exc = TypeError( - 'error in %s._missing_: returned %r instead of None or a valid member' - % (cls.__name__, result) - ) - te_exc.__context__ = ve_exc - raise te_exc