Skip to content

Commit 280066a

Browse files
committed
fix clear frame locals
python/cpython#113939
1 parent 44adfe2 commit 280066a

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

returnn/torch/engine.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ def _handle_run_exception(self, exc: Exception, *, always_direct_print: bool = F
255255
exc_ext = []
256256
for frame in iter_traceback(exc.__traceback__):
257257
frame_self = frame.f_locals.get("self")
258+
frame.f_locals.clear() # https://github.com/python/cpython/issues/113939
258259
if isinstance(frame_self, (torch.nn.Module, rf.Module)):
259260
func = get_func_from_code_object(frame.f_code, frame=frame)
260261
if func and func.__name__ and func.__name__.startswith("_") and not func.__name__.startswith("__"):

returnn/util/better_exchook.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,7 @@ def _try_set(old, prefix, func):
12241224
output(color(" no locals", color.fg_colors[0]))
12251225
else:
12261226
output(color(" -- code not available --", color.fg_colors[0]))
1227+
f.f_locals.clear() # https://github.com/python/cpython/issues/113939
12271228
if isframe(_tb):
12281229
_tb = _tb.f_back
12291230
elif is_stack_summary(_tb):
@@ -1496,8 +1497,10 @@ def get_func_from_code_object(co, frame=None):
14961497
_attr_name = "__code__" if PY3 else "func_code"
14971498
if frame:
14981499
func_name = frame.f_code.co_name
1499-
if "self" in frame.f_locals:
1500-
candidate = getattr(frame.f_locals["self"].__class__, func_name, None)
1500+
frame_self = frame.f_locals.get("self")
1501+
frame.f_locals.clear() # https://github.com/python/cpython/issues/113939
1502+
if frame_self is not None:
1503+
candidate = getattr(frame_self.__class__, func_name, None)
15011504
if candidate and (getattr(candidate, _attr_name, None) is co or isinstance(co, DummyFrame)):
15021505
return candidate
15031506
try:

0 commit comments

Comments
 (0)