Skip to content

refactor: use PyThreadState_GetInterpreter() instead of tstate->interp - #6230

Merged
rwgk merged 2 commits into
pybind:masterfrom
henryiii:refactor/threadstate-getinterpreter
Oct 31, 2026
Merged

rwgk merged 2 commits into
pybind:masterfrom
henryiii:refactor/threadstate-getinterpreter

Conversation

@henryiii

Copy link
Copy Markdown
Collaborator

🤖 AI text below 🤖

Description

Replace direct reads of tstate->interp in detail/internals.h and subinterpreter.h with the public PyThreadState_GetInterpreter() (available since CPython 3.9). A small detail::get_interpreter(PyThreadState *) helper keeps the call sites clean.

PyThreadState is opaque under the stable ABI, so this is groundwork for that work, but it stands on its own. It comes out of #6104 (finding B8).

  • PyPy: PyPy 7.3.19 (PyPy 3.10) does not export PyThreadState_GetInterpreter; 7.3.23 and 8.0 do. pybind11 supports PyPy 7.3.17+, so the helper keeps tstate->interp under PYPY_VERSION.
  • GraalPy: the GraalPy 25.0 and 25.3 headers declare PyThreadState_GetInterpreter, so GraalPy uses the CPython path.

Suggested changelog entry:

  • Use PyThreadState_GetInterpreter() instead of reading PyThreadState::interp directly.

henryiii and others added 2 commits October 30, 2026 21:52
PyThreadState is opaque under the stable ABI; the accessor function is
public since Python 3.9. Older PyPy (e.g. 7.3.19 / PyPy 3.10) does not
provide it, so PyPy keeps reading tstate->interp.

See pybind#6104 (B8).

Assisted-by: ClaudeCode:claude-opus-5-5
— codex GPT-6 on behalf of Ralf —

Keep direct thread-state field access for PyPy 7.x, since some releases
lack PyThreadState_GetInterpreter. Use the public accessor on PyPy 8.x,
where it is available for both supported Python versions.
@rwgk
rwgk merged commit a581ca2 into pybind:master Oct 31, 2026
92 of 93 checks passed
@rwgk
rwgk deleted the refactor/threadstate-getinterpreter branch October 31, 2026 21:01
@github-actions github-actions Bot added the needs changelog Possibly needs a changelog entry label Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs changelog Possibly needs a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants