From 7cbab74fa501db588923f7dc30bbfc3d2d578da3 Mon Sep 17 00:00:00 2001 From: Pavel Koneski Date: Thu, 29 Oct 2026 13:23:13 -0700 Subject: [PATCH] Fix f_builtins of a traced frame --- .../IronPython/Runtime/Exceptions/TraceBack.cs | 4 +++- tests/suite/modules/system_related/test_sys.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/core/IronPython/Runtime/Exceptions/TraceBack.cs b/src/core/IronPython/Runtime/Exceptions/TraceBack.cs index 8a18f1b28..422064bef 100644 --- a/src/core/IronPython/Runtime/Exceptions/TraceBack.cs +++ b/src/core/IronPython/Runtime/Exceptions/TraceBack.cs @@ -198,7 +198,9 @@ public FunctionCode f_code { public object f_builtins { get { - return _context.LanguageContext.BuiltinModuleDict; + // a frame created by the trace listener has no context of its own, but the listener knows the language context + PythonContext context = _context?.LanguageContext ?? _traceAdapter.PythonContext; + return context.BuiltinModuleDict; } } diff --git a/tests/suite/modules/system_related/test_sys.py b/tests/suite/modules/system_related/test_sys.py index 9dac0c7f4..8bc3704f1 100644 --- a/tests/suite/modules/system_related/test_sys.py +++ b/tests/suite/modules/system_related/test_sys.py @@ -329,6 +329,20 @@ def test_settrace_Xtracing_module_frame(self): out = subprocess.check_output([sys.executable, '-X:Tracing', '-c', code]) self.assertEqual(out.strip(), b"['', 'g']") + def test_settrace_f_builtins(self): + # The frame of a traced function has the same builtins as the frame of code that is not traced + import builtins + def g(): + return sys._getframe(0).f_builtins + + sys.settrace(lambda *args: None) + try: + traced = g() + finally: + sys.settrace(None) + self.assertIs(traced, builtins.__dict__) + self.assertIs(sys._getframe(0).f_builtins, builtins.__dict__) + @skipUnlessIronPython() @unittest.expectedFailure # dummy getrefcount removed in https://github.com/IronLanguages/ironpython3/pull/577 def test_getrefcount(self):