Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ea4673e

Browse files
bpo-44747: Refactor usage of sys._getframe at typing module (#27387)
Co-authored-by: Ken Jin <[email protected]>
1 parent 7b975f8 commit ea4673e

2 files changed

Lines changed: 9 additions & 21 deletions

File tree

Lib/typing.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -798,10 +798,7 @@ def __init__(self, name, *constraints, bound=None,
798798
raise TypeError("A single constraint is not allowed")
799799
msg = "TypeVar(name, constraint, ...): constraints must be types."
800800
self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
801-
try:
802-
def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
803-
except (AttributeError, ValueError):
804-
def_mod = None
801+
def_mod = _caller()
805802
if def_mod != 'typing':
806803
self.__module__ = def_mod
807804

@@ -904,10 +901,7 @@ def kwargs(self):
904901
def __init__(self, name, *, bound=None, covariant=False, contravariant=False):
905902
self.__name__ = name
906903
super().__init__(bound, covariant, contravariant)
907-
try:
908-
def_mod = sys._getframe(1).f_globals.get('__name__', '__main__')
909-
except (AttributeError, ValueError):
910-
def_mod = None
904+
def_mod = _caller()
911905
if def_mod != 'typing':
912906
self.__module__ = def_mod
913907

@@ -1400,10 +1394,7 @@ def _allow_reckless_class_checks(depth=3):
14001394
The abc and functools modules indiscriminately call isinstance() and
14011395
issubclass() on the whole MRO of a user class, which may contain protocols.
14021396
"""
1403-
try:
1404-
return sys._getframe(depth).f_globals['__name__'] in ['abc', 'functools']
1405-
except (AttributeError, ValueError): # For platforms without _getframe().
1406-
return True
1397+
return _caller(depth) in {'abc', 'functools', None}
14071398

14081399

14091400
_PROTO_ALLOWLIST = {
@@ -2238,11 +2229,7 @@ class Employee(NamedTuple):
22382229
elif kwargs:
22392230
raise TypeError("Either list of fields or keywords"
22402231
" can be provided to NamedTuple, not both")
2241-
try:
2242-
module = sys._getframe(1).f_globals.get('__name__', '__main__')
2243-
except (AttributeError, ValueError):
2244-
module = None
2245-
return _make_nmtuple(typename, fields, module=module)
2232+
return _make_nmtuple(typename, fields, module=_caller())
22462233

22472234
_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
22482235

@@ -2357,11 +2344,10 @@ class body be required.
23572344
" but not both")
23582345

23592346
ns = {'__annotations__': dict(fields)}
2360-
try:
2347+
module = _caller()
2348+
if module is not None:
23612349
# Setting correct module is necessary to make typed dict classes pickleable.
2362-
ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
2363-
except (AttributeError, ValueError):
2364-
pass
2350+
ns['__module__'] = module
23652351

23662352
return _TypedDictMeta(typename, (), ns, total=total)
23672353

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Refactor usage of ``sys._getframe`` in ``typing`` module. Patch provided by
2+
Yurii Karabas.

0 commit comments

Comments
 (0)