Skip to content

gh-134075: Document how to make C extensions interruptible - #159571

Open
shaury04 wants to merge 1 commit into
python:mainfrom
shaury04:gh-134075-checksignals-guide
Open

shaury04 wants to merge 1 commit into
python:mainfrom
shaury04:gh-134075-checksignals-guide

Conversation

@shaury04

@shaury04 shaury04 commented Oct 16, 2026 •

Copy link
Copy Markdown

Long-running C extensions can delay Ctrl-C handling until they return to the
interpreter. Add a guide explaining where to call PyErr_CheckSignals(), how to
propagate exceptions and clean up partial results, and how to keep extension
state safe when signal handlers execute arbitrary Python code.

Include a C example that processes immutable input in chunks, reattaches the
thread state before checking signals, and discards its private output on error.
Cover free-threaded builds, checking frequency, blocking library calls, and
main-thread signal handling. Link to the guide from the C API reference and
make the attached-thread-state requirement explicit there.

Fixes #134075.

Validation

  • Both changed HTML pages build with --nitpicky --fail-on-warning and no warnings.
  • Full HTML documentation was generated. Its known reference warnings cause
    the strict full-tree build to exit nonzero, as expected by the docs CI workflow;
    all three checks in Doc/tools/check-warnings.py pass against the repository's
    warning baseline.
  • sphinx-lint --enable=default-role passes for both changed files.
  • The exact C example compiles with -Wall -Wextra -Werror (unused parameters
    excluded) and passes five tests on CPython 3.13.7: chunk boundaries and contents,
    invalid input, KeyboardInterrupt, preservation of custom exceptions, and a
    returning handler that re-enters the extension. The interruption tests verify
    that the C function returns NULL, rather than allowing deferred delivery after
    a successful return. The standalone validation harness is supplied with the
    review artifacts, outside the documentation-only patch.
  • git diff --check passes.

AI assistance was used to draft the documentation and validate the example.

@python-cla-bot

python-cla-bot Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@shaury04
shaury04 force-pushed the gh-134075-checksignals-guide branch from c956d33 to c6f2373 Compare October 16, 2026 19:31
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #35199671 | 📁 Comparing c6f2373 against main (9cb7a5b)

  🔍 Preview build  

2 files changed
± c-api/exceptions.html
± extending/extending.html


.. _extending-interruptible:

Making long-running operations interruptible

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@zackw This PR seems to be AI-generated, but is this what you were looking for?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

Give PyErr_CheckSignals guide-level documentation

2 participants