Skip to content

Use PyGILState_Ensure to avoid accessing CPython internals - #269

Merged
nitzmahone merged 2 commits into
python-cffi:mainfrom
ngoldbaum:rm-old-python-hacks
Aug 3, 2026
Merged

Use PyGILState_Ensure to avoid accessing CPython internals#269
nitzmahone merged 2 commits into
python-cffi:mainfrom
ngoldbaum:rm-old-python-hacks

Conversation

@ngoldbaum

Copy link
Copy Markdown
Contributor

Following a suggestion from @kumaraditya303, this removes direct access to the PyThreadState struct in gil_ensure() and replaces it with PyGILState_Ensure.

The direct struct access is the cause of the crash in #263.

The current code dates back to Python 3.6 and works around differences spanning the Python 2->3 transition. That is all historical now. The code that is currently active is identical to the content of PyGILState_Ensure.

Comment thread src/c/call_python.c

@kumaraditya303 kumaraditya303 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@nitzmahone
nitzmahone merged commit 8e5b6a5 into python-cffi:main Aug 3, 2026
38 checks passed
nitzmahone added a commit that referenced this pull request Aug 3, 2026
* Use PyGilState_Ensure to avoid accessing CPython internals

* Avoid more direct PyThreadState accesses

(cherry picked from commit 8e5b6a5)

Co-authored-by: Nathan Goldbaum <nathan.goldbaum@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants