This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Add private _PyThreadState_UncheckedGet() to get the current thread state
Type: Stage:
Components: Versions: Python 3.6, Python 3.5
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: gregory.p.smith, larry, njs, python-dev, vstinner, wenzel
Priority: normal Keywords: patch

Created on 2016-01-19 12:30 by vstinner, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit
pythreadstate_fastget.patch vstinner, 2016-01-19 12:30 review
Messages (6)
msg258587 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-01-19 12:30
The issue #25150 modified pystate.h to hide _PyThreadState_Current. Sadly, this change broke the vmprof project:

Attached patches adds a new private _PyThreadState_FastGet() function to get the current thread state but don't call Py_FatalError() if it is NULL.

The patch also uses replace direct access to _PyThreadState_Current with _PyThreadState_FastGet(), except inside ceval.c and pystate.c.

Calling Py_FatalError() to handle errors is not really a great API... Bad that's a different story, I don't want to break anything here.

I want to add the private function to Python 3.5.2 because I consider that the removal of the _PyThreadState_Current symbol is a regression introduced in Python 3.5.1.

We have no rule for the Python private API, it can change *anytime*.
msg258598 - (view) Author: Nathaniel Smith (njs) * (Python committer) Date: 2016-01-19 15:10
Name should be _PyThreadState_UncheckedGet
msg258633 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2016-01-19 23:25
Overall +1 to this private API.  I like the UncheckedGet name better than FastGet but don't really care what the name is so long as it keeps this property: It must be non-blocking and safe to call from a signal handler.  Returning NULL in the event the value could not be obtained in such a manner is fine (unlikely to happen from the looks of the 3.5 code).

They are private and this fixes the regression in 3.5.1.  people (ab)using these private APIs should be fine writing conditional compilation code to deal with that.

We've got a similar patch on our CPython 2.7 interpreter at work (more complicated as it must do a non-blocking lock acquire in the old 2.7 code).
msg258656 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-01-20 10:20
New changeset f9461f1e0559 by Victor Stinner in branch '3.5':
Add _PyThreadState_UncheckedGet()

New changeset d4f13c9a2b07 by Victor Stinner in branch 'default':
Merge 3.5
msg258657 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-01-20 10:21
Function added

@fijal: Sorry for the annoyance of the Python 3.5.1 regression.
msg264178 - (view) Author: Wenzel Jakob (wenzel) Date: 2016-04-25 15:16
I've also run into this regression. FWIW this is what I've ended up using to work around it (it's a mess, but what are we to do..)

#if PY_VERSION_HEX >= 0x03050000 && PY_VERSION_HEX < 0x03050200
extern "C" {
    /* Manually import _PyThreadState_Current symbol */
    struct _Py_atomic_address { void *value; };
    PyAPI_DATA(_Py_atomic_address) _PyThreadState_Current;

PyThreadState *get_thread_state_unchecked() {
#if   PY_VERSION_HEX < 0x03000000
    return _PyThreadState_Current;
#elif PY_VERSION_HEX < 0x03050000
    return (PyThreadState*) _Py_atomic_load_relaxed(&_PyThreadState_Current);
#elif PY_VERSION_HEX < 0x03050200
    return (PyThreadState*) _PyThreadState_Current.value;
    return _PyThreadState_UncheckedGet();
Date User Action Args
2022-04-11 14:58:26adminsetgithub: 70342
2016-04-25 15:16:37wenzelsetnosy: + wenzel
messages: + msg264178
2016-01-20 10:21:22vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg258657

title: Add private _PyThreadState_FastGet() to get the current thread state -> Add private _PyThreadState_UncheckedGet() to get the current thread state
2016-01-20 10:20:39python-devsetnosy: + python-dev
messages: + msg258656
2016-01-19 23:25:35gregory.p.smithsetnosy: + gregory.p.smith
messages: + msg258633
2016-01-19 15:10:03njssetnosy: + njs
messages: + msg258598
2016-01-19 12:30:33vstinnercreate