Skip to content

Conversation

@krassowski
Copy link
Member

@krassowski krassowski commented Apr 25, 2025

  • Add a tests interruption of %%time and %%timeit magics (the former should fail)
  • Fix %%time magic exception capture
    Note that this fix is fully backward compatible since users would not get timing information if there was an exception anyways due to an early return in line 1379:
    except:
    self.shell.showtraceback()
    return
  • Fix %%debug magic exception capture

@krassowski krassowski added the bug label Apr 25, 2025
@krassowski krassowski added this to the 8.36 milestone Apr 25, 2025
We should not use bare `except:` in magics as it prevents
catching `KeyboardException` and other system-exiting exceptions.
@krassowski krassowski marked this pull request as ready for review April 25, 2025 09:22
@krassowski krassowski linked an issue Apr 25, 2025 that may be closed by this pull request
@krassowski krassowski requested a review from Copilot April 25, 2025 09:37
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes the interruption behavior for the %%time and %%debug magics by updating exception capture and adding tests that simulate KeyboardInterrupt scenarios.

  • Add new tests to verify that %%time and %%timeit (and similarly %%debug) correctly propagate KeyboardInterrupt.
  • Update exception handling in execution.py to explicitly catch Exception rather than a bare except, improving clarity and behavior.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
tests/test_magic.py Added tests for interruption of %%time and %%timeit magics and removed duplicate imports.
IPython/core/magics/execution.py Refactored exception capture and improved formatting in the timing magics.
Comments suppressed due to low confidence (2)

tests/test_magic.py:862

  • Consider using a timeout with thread.join (e.g. thread.join(5)) to avoid potential hanging tests if the interruption mechanism fails.
thread.join()

tests/test_magic.py:859

  • [nitpick] Add an inline comment or reference for _interrupt_after_1s to clarify its purpose in the test, which will help future maintainers understand the interruption strategy.
thread = Thread(target=_interrupt_after_1s)

@Darshan808
Copy link
Collaborator

LGTM 👍

@krassowski krassowski merged commit c01ed4a into ipython:main Apr 25, 2025
18 checks passed
meeseeksmachine pushed a commit to meeseeksmachine/ipython that referenced this pull request Apr 25, 2025
krassowski added a commit that referenced this pull request Apr 25, 2025
…%debug` magics) (#14891)

Backport PR #14890: Fix interruption of `%%time` and `%%debug` magics
@krassowski krassowski deleted the fix-interruption-of-time-magic branch April 25, 2025 18:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

%%time does not raise on interruption

2 participants