bpo-44747: Refactor usage of sys._getframe at typing module (#27387)

Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
This commit is contained in:
Yurii Karabas 2021-07-30 16:49:24 +03:00 committed by GitHub
parent 7b975f81e4
commit ea4673ed07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 21 deletions

View File

@ -798,10 +798,7 @@ class TypeVar( _Final, _Immutable, _TypeVarLike, _root=True):
raise TypeError("A single constraint is not allowed") raise TypeError("A single constraint is not allowed")
msg = "TypeVar(name, constraint, ...): constraints must be types." msg = "TypeVar(name, constraint, ...): constraints must be types."
self.__constraints__ = tuple(_type_check(t, msg) for t in constraints) self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
try: def_mod = _caller()
def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
except (AttributeError, ValueError):
def_mod = None
if def_mod != 'typing': if def_mod != 'typing':
self.__module__ = def_mod self.__module__ = def_mod
@ -904,10 +901,7 @@ class ParamSpec(_Final, _Immutable, _TypeVarLike, _root=True):
def __init__(self, name, *, bound=None, covariant=False, contravariant=False): def __init__(self, name, *, bound=None, covariant=False, contravariant=False):
self.__name__ = name self.__name__ = name
super().__init__(bound, covariant, contravariant) super().__init__(bound, covariant, contravariant)
try: def_mod = _caller()
def_mod = sys._getframe(1).f_globals.get('__name__', '__main__')
except (AttributeError, ValueError):
def_mod = None
if def_mod != 'typing': if def_mod != 'typing':
self.__module__ = def_mod self.__module__ = def_mod
@ -1400,10 +1394,7 @@ def _allow_reckless_class_checks(depth=3):
The abc and functools modules indiscriminately call isinstance() and The abc and functools modules indiscriminately call isinstance() and
issubclass() on the whole MRO of a user class, which may contain protocols. issubclass() on the whole MRO of a user class, which may contain protocols.
""" """
try: return _caller(depth) in {'abc', 'functools', None}
return sys._getframe(depth).f_globals['__name__'] in ['abc', 'functools']
except (AttributeError, ValueError): # For platforms without _getframe().
return True
_PROTO_ALLOWLIST = { _PROTO_ALLOWLIST = {
@ -2238,11 +2229,7 @@ def NamedTuple(typename, fields=None, /, **kwargs):
elif kwargs: elif kwargs:
raise TypeError("Either list of fields or keywords" raise TypeError("Either list of fields or keywords"
" can be provided to NamedTuple, not both") " can be provided to NamedTuple, not both")
try: return _make_nmtuple(typename, fields, module=_caller())
module = sys._getframe(1).f_globals.get('__name__', '__main__')
except (AttributeError, ValueError):
module = None
return _make_nmtuple(typename, fields, module=module)
_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {}) _NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
@ -2357,11 +2344,10 @@ def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
" but not both") " but not both")
ns = {'__annotations__': dict(fields)} ns = {'__annotations__': dict(fields)}
try: module = _caller()
if module is not None:
# Setting correct module is necessary to make typed dict classes pickleable. # Setting correct module is necessary to make typed dict classes pickleable.
ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__') ns['__module__'] = module
except (AttributeError, ValueError):
pass
return _TypedDictMeta(typename, (), ns, total=total) return _TypedDictMeta(typename, (), ns, total=total)

View File

@ -0,0 +1,2 @@
Refactor usage of ``sys._getframe`` in ``typing`` module. Patch provided by
Yurii Karabas.