Skip to content

Commit 7cbab74

Browse files
committed
Fix f_builtins of a traced frame
1 parent a0dc844 commit 7cbab74

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎src/core/IronPython/Runtime/Exceptions/TraceBack.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,9 @@ public FunctionCode f_code {
198198

199199
public object f_builtins {
200200
get {
201-
return _context.LanguageContext.BuiltinModuleDict;
201+
// a frame created by the trace listener has no context of its own, but the listener knows the language context
202+
PythonContext context = _context?.LanguageContext ?? _traceAdapter.PythonContext;
203+
return context.BuiltinModuleDict;
202204
}
203205
}
204206

‎tests/suite/modules/system_related/test_sys.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,20 @@ def test_settrace_Xtracing_module_frame(self):
329329
out = subprocess.check_output([sys.executable, '-X:Tracing', '-c', code])
330330
self.assertEqual(out.strip(), b"['<module>', 'g']")
331331

332+
def test_settrace_f_builtins(self):
333+
# The frame of a traced function has the same builtins as the frame of code that is not traced
334+
import builtins
335+
def g():
336+
return sys._getframe(0).f_builtins
337+
338+
sys.settrace(lambda *args: None)
339+
try:
340+
traced = g()
341+
finally:
342+
sys.settrace(None)
343+
self.assertIs(traced, builtins.__dict__)
344+
self.assertIs(sys._getframe(0).f_builtins, builtins.__dict__)
345+
332346
@skipUnlessIronPython()
333347
@unittest.expectedFailure # dummy getrefcount removed in https://github.com/IronLanguages/ironpython3/pull/577
334348
def test_getrefcount(self):

0 commit comments

Comments
 (0)