diff --git a/Lib/test/test_monitoring.py b/Lib/test/test_monitoring.py index 5c2d69934b02ea..dfc98e09b35273 100644 --- a/Lib/test/test_monitoring.py +++ b/Lib/test/test_monitoring.py @@ -702,6 +702,25 @@ def b(): self.check_lines(f, [1,3,5,4,2,4]) + def test_line_same_line_as_def(self): + + def func(): line = 1; line = 2 + + self.check_lines(func, [0]) + + def test_line_multi_line_body_starting_on_def_line(self): + + def func(): line = 1; \ + line = 2 + + self.check_lines(func, [0, 1]) + + def test_line_lambda_body(self): + + lam = lambda: 1 + + self.check_lines(lam, [0]) + class TestDisable(MonitoringTestBase, unittest.TestCase): def gen(self, cond): diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-18-12-01-47.gh-issue-155992.Yl4gI2.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-18-12-01-47.gh-issue-155992.Yl4gI2.rst new file mode 100644 index 00000000000000..92a8acd64ecc7a --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-18-12-01-47.gh-issue-155992.Yl4gI2.rst @@ -0,0 +1,3 @@ +Fix a regression in ``sys.monitoring`` where the ``LINE`` event did not fire +for the first line of a code object when the line also contains the ``def`` +or ``lambda`` statement. The event is emitted again as it was in Python 3.12. diff --git a/Python/instrumentation.c b/Python/instrumentation.c index 806d3fbf5d6b19..09566b0355f795 100644 --- a/Python/instrumentation.c +++ b/Python/instrumentation.c @@ -1349,7 +1349,9 @@ _Py_call_instrumentation_line(PyThreadState *tstate, _PyInterpreterFrame* frame, /* RESUME and INSTRUMENTED_RESUME are needed for the operation of * instrumentation, so must never be hidden by an INSTRUMENTED_LINE. */ - if (prev_opcode != RESUME && prev_opcode != INSTRUMENTED_RESUME) { + if (_PyOpcode_Deopt[prev_opcode] != RESUME && + prev_opcode != INSTRUMENTED_RESUME) + { goto done; } }