8000 gh-119180: Only fetch globals and locals if necessary in `annotationl… · python/cpython@504ae60 · GitHub
[go: up one dir, main page]

Skip to content
< 10000 /react-partial>

Commit 504ae60

Browse files
authored
gh-119180: Only fetch globals and locals if necessary in annotationlib.get_annotations() (#135644)
1 parent e4ccd46 commit 504ae60

File tree

2 files changed

+44
-41
lines changed

2 files changed

+44
-41
lines changed

Lib/annotationlib.py

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -939,48 +939,49 @@ def get_annotations(
939939
if not eval_str:
940940
return dict(ann)
941941

942-
if isinstance(obj, type):
943-
# class
944-
obj_globals = None
945-
module_name = getattr(obj, "__module__", None)
946-
if module_name:
947-
module = sys.modules.get(module_name, None)
948-
if module:
949-
obj_globals = getattr(module, "__dict__", None)
950-
obj_locals = dict(vars(obj))
951-
unwrap = obj
952-
elif isinstance(obj, types.ModuleType):
953-
# module
954-
obj_globals = getattr(obj, "__dict__")
955-
obj_locals = None
956-
unwrap = None
957-
elif callable(obj):
958-
# this includes types.Function, types.BuiltinFunctionType,
959-
# types.BuiltinMethodType, functools.partial, functools.singledispatch,
960-
# "class funclike" from Lib/test/test_inspect... on and on it goes.
961-
obj_globals = getattr(obj, "__globals__", None)
962-
obj_locals = None
963-
unwrap = obj
964-
else:
965-
obj_globals = obj_locals = unwrap = None
966-
967-
if unwrap is not None:
968-
while True:
969-
if hasattr(unwrap, "__wrapped__"):
970-
unwrap = unwrap.__wrapped__
971-
continue
972-
if functools := sys.modules.get("functools"):
973-
if isinstance(unwrap, functools.partial):
974-
unwrap = unwrap.func
942+
if globals is None or locals is None:
943+
if isinstance(obj, type):
944+
# class
945+
obj_globals = None
946+
module_name = getattr(obj, "__module__", None)
947+
if module_name:
948+
module = sys.modules.get(module_name, None)
949+
if module:
950+
obj_globals = getattr(module, "__dict__", None)
951+
obj_locals = dict(vars(obj))
952+
unwrap = obj
953+
elif isinstance(obj, types.ModuleType):
954+
# module
955+
obj_globals = getattr(obj, "__dict__")
956+
obj_locals = None
957+
unwrap = None
958+
elif callable(obj):
959+
# this includes types.Function, types.BuiltinFunctionType,
960+
# types.BuiltinMethodType, functools.partial, functools.singledispatch,
961+
# "class funclike" from Lib/test/test_inspect... on and on it goes.
962+
obj_globals = getattr(obj, "__globals__", None)
963+
obj_locals = None
964+
unwrap = obj
965+
else:
966+
obj_globals = obj_locals = unwrap = None
967+
968+
if unwrap is not None:
969+
while True:
970+
if hasattr(unwrap, "__wrapped__"):
971+
unwrap = unwrap.__wrapped__
975972
continue
976-
break
977-
if hasattr(unwrap, "__globals__"):
978-
obj_globals = unwrap.__globals__
979-
980-
if globals is None:
981-
globals = obj_globals
982-
if locals is None:
983-
locals = obj_locals
973+
if functools := sys.modules.get("functools"):
974+
if isinstance(unwrap, functools.partial):
975+
unwrap = unwrap.func
976+
continue
977+
break
978+
if hasattr(unwrap, "__globals__"):
979+
obj_globals = unwrap.__globals__
980+
981+
if globals is None:
982+
globals = obj_globals
983+
if locals is None:
984+
locals = obj_locals
984985

985986
# "Inject" type parameters into the local namespace
986987
# (unless they are shadowed by assignments *in* the local namespace),
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Only fetch globals and locals if necessary in
2+
:func:`annotationlib.get_annotations`

0 commit comments

Comments
 (0)
0