Skip to content

FIX: missing line events for raise statements and finally: ... bodies in Python 3.12+#356

Merged
Erotemic merged 2 commits into
pyutils:mainfrom
TTsangSC:sys_mon_event_fix
Jul 17, 2025
Merged

FIX: missing line events for raise statements and finally: ... bodies in Python 3.12+#356
Erotemic merged 2 commits into
pyutils:mainfrom
TTsangSC:sys_mon_event_fix

Conversation

@TTsangSC
Copy link
Copy Markdown
Collaborator

@TTsangSC TTsangSC commented Jul 16, 2025

(Closes #355.)

Changes

  • line_profiler/_line_profiler.pyx
    Added handling for sys.monitoring.events.RAISE and .RERAISE
  • tests/test_line_profiler.py::test_profiling_exception()
    New test for checking whether raise statements and finally: ...
    bodies are properly profiled

Caveats

line_profiler/_line_profiler.pyx
    Added handling for `sys.monitoring.events.RAISE` and `.RERAISE`

tests/test_line_profiler.py::test_profiling_exception()
    New test for checking whether `raise` statements and `finally: ...`
    bodies are properly profiled
@TTsangSC TTsangSC changed the title Fix for #355 FIX: missing line events for raise statements and finally: ... bodies Jul 16, 2025
@TTsangSC TTsangSC changed the title FIX: missing line events for raise statements and finally: ... bodies FIX: missing line events for raise statements and finally: ... bodies in Python 3.12+ Jul 16, 2025
@TTsangSC
Copy link
Copy Markdown
Collaborator Author

For some weird reasons the finally body keeps being ignored in Python 3.9 if we have an exception – tested everything from 3.8 to 3.13 and it's the only version with that behavior.

I think that the behavior in every other version should be the correct one, and thus just XFailed the test on 3.9. @Erotemic do you think we should investigate further into this discrepancy or just let it be?

@codecov
Copy link
Copy Markdown

codecov Bot commented Jul 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.36%. Comparing base (90cd65e) to head (ce3e2be).
Report is 8 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #356   +/-   ##
=======================================
  Coverage   87.36%   87.36%           
=======================================
  Files          18       18           
  Lines        1630     1630           
  Branches      347      347           
=======================================
  Hits         1424     1424           
  Misses        151      151           
  Partials       55       55           

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2cfc693...ce3e2be. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Erotemic
Copy link
Copy Markdown
Member

do you think we should investigate further into this discrepancy or just let it be?

Usually I want to dive into these sort of things, but 3.9 is so close to end of life, that I'm very tempted to say let it be. I think we could just document it, and maybe come back to it later. I want to keep supporting 3.8+ for awhile, but profiling on raise and finally statements might be niche enough where nobody actually using 3.9 is impacted or notices. Does it work in current or older versions? Or is it something that's always been bugged?

I'm also thinking that we've added so much, even though we aren't breaking backwards compat, we do a major version bump anyway, and then do another if we decide to change APIs. If there are fixes that need to be applied after we do the next release, it will be easier to see 5.0 as the breakpoint rather than 4.3.

@Erotemic Erotemic merged commit 5817f3e into pyutils:main Jul 17, 2025
36 checks passed
@TTsangSC
Copy link
Copy Markdown
Collaborator Author

TTsangSC commented Jul 17, 2025

As far as I can tell the behavior is consistent on 3.8, 3.10, and 3.11 (and now also 3.12+ after the fix), so it isn't so much a backward compatibility problem but a quirk of 3.9 and 3.9 alone. Again, I can't seem to locate the root cause, but as you've said 3.9 is due for EoL in months; so yeah I agree, it should be okay for us to handle it this way.

I don't personally have a preference towards either 4.3 vs 5.0; on the one hand we've extensively (if I may say so) extended the features, and a major bump might be good for advertising that, but on the other we haven't really broken existing APIs (at least in obvious ways) so SemVer dictates a minor bump...

@TTsangSC TTsangSC deleted the sys_mon_event_fix branch July 17, 2025 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG (?): line data not reported when exception raised

2 participants