Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-108867: Add PyThreadState_GetUnchecked() function#108870

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
vstinner merged 2 commits intopython:mainfromvstinner:tstate_getunsafe
Oct 3, 2023

Conversation

vstinner
Copy link
Member

@vstinnervstinner commentedSep 4, 2023
edited
Loading

Add PyThreadState_GetUnchecked() function: similar to
PyThreadState_Get(), but don't issue a fatal error if it is NULL. The
caller is responsible to check if the result is NULL. Previously,
this function was private and known as _PyThreadState_UncheckedGet().


📚 Documentation preview 📚:https://cpython-previews--108870.org.readthedocs.build/

@vstinner
Copy link
MemberAuthor

I was worried aboutPy_TRASHCAN_BEGIN_CONDITION() macro which currently calls_PyThreadState_UncheckedGet(). This macro uses private functions:

/* Python 3.9 private API, invoked by the macros below. */PyAPI_FUNC(int)_PyTrash_begin(PyThreadState*tstate,PyObject*op);PyAPI_FUNC(void)_PyTrash_end(PyThreadState*tstate);/* Python 3.10 private API, invoked by the Py_TRASHCAN_BEGIN(). */PyAPI_FUNC(int)_PyTrash_cond(PyObject*op,destructordealloc);

I was worried that we still provide Python 3.9 functions. Is it the a stable ABI? Nope, these functions are not part ofMisc/stable_abi.toml.

Moreover, I explicitly removed the following macros from the limited C API in Python 3.9:

  • PyTrash_UNWIND_LEVEL
  • Py_TRASHCAN_BEGIN_CONDITION()
  • Py_TRASHCAN_BEGIN()
  • Py_TRASHCAN_END()
  • Py_TRASHCAN_SAFE_BEGIN()
  • Py_TRASHCAN_SAFE_END()

So in fact, we keep Python 3.9 functions for the ABI backward compatibility, whereas we donot support these APIs in the stable ABI.

In short, it's ok to rename_PyThreadState_UncheckedGet() toPyThreadState_GetUnsafe(), it's not used by the stable ABI.

@vstinner
Copy link
MemberAuthor

A bunch of projects currently uses_PyThreadState_UncheckedGet(), whereas I removed the function from the public C API in Python 3.13.

Affected projects (18):

  • Cython (0.29.36)
  • catboost (1.2)
  • cffi (1.15.1)
  • debugpy (1.6.7)
  • dlib (19.24.2)
  • fastobo (0.12.2)
  • frozendict (2.3.8)
  • onnx (1.14.0)
  • onnxoptimizer (0.3.13)
  • onnxsim (0.4.33)
  • orjson (3.9.1)
  • osmium (3.6.0)
  • praat-parselmouth (0.4.3)
  • ptvsd (4.3.2)
  • pybind11 (2.10.4)
  • pydevd (2.9.6)
  • pydevd-pycharm (232.8296.19)
  • pythonnet (3.0.1)

In this list, IMO the interesting one is Cython:

$gitgrep-E'(_PyThreadState_UncheckedGet|__Pyx_PyThreadState_Current)'Cython/Utility/AsyncGen.c:tstate=__Pyx_PyThreadState_Current;Cython/Utility/AsyncGen.c:tstate=__Pyx_PyThreadState_Current;Cython/Utility/Coroutine.c:__Pyx_PyGen__FetchStopIterationValue(__Pyx_PyThreadState_Current,pvalue)Cython/Utility/Coroutine.c:tstate=__Pyx_PyThreadState_Current;Cython/Utility/Coroutine.c:__Pyx_PyGen__FetchStopIterationValue(__Pyx_PyThreadState_Current,&val);Cython/Utility/Exceptions.c:__pyx_assertions_enabled_flag= !_PyInterpreterState_GetConfig(__Pyx_PyThreadState_Current->interp)->optimization_level;Cython/Utility/Exceptions.c:#define__Pyx_PyThreadState_assign  $local_tstate_cname = __Pyx_PyThreadState_Current;Cython/Utility/Exceptions.c:PyThreadState*tstate=__Pyx_PyThreadState_Current;Cython/Utility/Exceptions.c:        (void)__Pyx_CLineForTraceback(__Pyx_PyThreadState_Current,c_line);Cython/Utility/Exceptions.c:PyThreadState*tstate=__Pyx_PyThreadState_Current;Cython/Utility/ModuleSetupCode.c:// Py3<3.5.2 does not support _PyThreadState_UncheckedGet().Cython/Utility/ModuleSetupCode.c:#define__Pyx_PyThreadState_Current PyThreadState_Get()Cython/Utility/ModuleSetupCode.c:#define__Pyx_PyThreadState_Current PyThreadState_GET()Cython/Utility/ModuleSetupCode.c:#define__Pyx_PyThreadState_Current _PyThreadState_UncheckedGet()Cython/Utility/ModuleSetupCode.c:#define__Pyx_PyThreadState_Current PyThreadState_GET()Cython/Utility/ModuleSetupCode.c:#define__Pyx_PyThreadState_Current _PyThreadState_CurrentCython/Utility/ModuleSetupCode.c:current=tcur==__Pyx_PyThreadState_Current;Cython/Utility/ObjectHandling.c:PyThreadState*tstate=__Pyx_PyThreadState_Current;Cython/Utility/Profile.c:tstate=__Pyx_PyThreadState_Current;                                          \Cython/Utility/Profile.c:PyThreadState*tstate=__Pyx_PyThreadState_Current;                                 \Cython/Utility/Profile.c:tstate=__Pyx_PyThreadState_Current;                                       \Cython/Utility/Profile.c:PyThreadState*tstate=__Pyx_PyThreadState_Current;                            \Cython/Utility/Profile.c:PyThreadState*tstate=__Pyx_PyThreadState_Current;                                \Cython/Utility/Profile.c:tstate=__Pyx_PyThreadState_Current;                                        \Cython/Utility/Profile.c:PyThreadState*tstate=__Pyx_PyThreadState_Current;                             \Cython/Utility/Profile.c:PyThreadState*tstate=__Pyx_PyThreadState_Current;                                 \

I can easily add PyThreadState_GetUnsafe() topythoncapi-compat by calling_PyThreadState_UncheckedGet() on Python 3.12 and older. This private function is available since Python 3.5. It seems to be available on PyPy:

$ cd /usr/include/pypy3.9/$ grep _PyThreadState_UncheckedGet -R ../pylifecycle.h:    (_Py_IsFinalizing() ? _PyThreadState_UncheckedGet() : NULL)./pypy_decl.h:#define _PyThreadState_UncheckedGet _PyPyThreadState_UncheckedGet./pypy_decl.h:PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void);

@vstinner
Copy link
MemberAuthor

Iasked in the C API Working Group which function name is better: useUnchecked suffix orUnsafe suffix?

@vstinner
Copy link
MemberAuthor

I renamed the function to PyThreadState_GetUnchecked() and addressed@encukou's suggestion on the documentation.

Add PyThreadState_GetUnchecked() function: similar toPyThreadState_Get(), but don't issue a fatal error if it is NULL. Thecaller is responsible to check if the result is NULL. Previously,this function was private and known as _PyThreadState_UncheckedGet().
@vstinnervstinner changed the titlegh-108867: Add PyThreadState_GetUnsafe() functiongh-108867: Add PyThreadState_GetUnchecked() functionOct 3, 2023
@vstinner
Copy link
MemberAuthor

I renamed the function to PyThreadState_GetUnchecked()

Oops, my PR still used PyThreadState_GetUnsafe() name. I messed up something.

Anyway, I updated it again, and now it uses PyThreadState_GetUnchecked() name everywhere.

@vstinnervstinnerenabled auto-merge (squash)October 3, 2023 16:23
@vstinnervstinner merged commitd735016 intopython:mainOct 3, 2023
@vstinnervstinner deleted the tstate_getunsafe branchOctober 3, 2023 16:53
@vstinner
Copy link
MemberAuthor

I added PyThreadState_GetUnchecked() to pythoncapi-compat:python/pythoncapi-compat@f78c780

Glyphack pushed a commit to Glyphack/cpython that referenced this pull requestSep 2, 2024
…8870)Add PyThreadState_GetUnchecked() function: similar toPyThreadState_Get(), but don't issue a fatal error if it is NULL. Thecaller is responsible to check if the result is NULL. Previously,this function was private and known as _PyThreadState_UncheckedGet().
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@encukouencukouencukou left review comments

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@vstinner@encukou@bedevere-bot

[8]ページ先頭

©2009-2025 Movatter.jp