python / python/cpython

Give PyErr_CheckSignals guide-level documentation

Open
#134,075 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

docs
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Documentation

Compiled extensions that "want to be interruptible by user requests" need to call PyErr_CheckSignals periodically; when it returns -1, they should stop work, clean up after themselves, and return to the interpreter.

This function is not mentioned at all in the extending and embedding guide, and its reference documentation makes it sound unimportant. As a result, many extension authors are (it seems to me) not aware that it exists or that they ought to be calling it, and so many extensions that should be interruptible by user requests aren't. For example, if you execute this code

>>> import numpy as np
>>> rng = np.random.default_rng()
>>> x = rng.random(1000000000)

and then immediately hit control-C, you'll be waiting at least a few seconds before you get the prompt back.

PyErr_CheckSignals also has the unusual property of potentially executing arbitrary Python code before returning to its caller, which makes it easy to use unsafely. Making an extension interruptible throughout may necessitate calling PyErr_CheckSignals from innermost loops where one would ideally release the GIL, which makes that worse (see #133465).

Thus, I suggest that a guide to correct use of PyErr_CheckSignals should be added to the extending and embedding guide.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the extending and embedding guide alongside the PyErr_CheckSignals reference documentation. Add a guide to using PyErr_CheckSignals safely, including handling a -1 return, cleanup, and its potential to execute arbitrary Python code; the documentation should make clear how extensions can remain interruptible.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
65/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.