Skip to content

gh-131238: Convert pycore_pystate.h static inline to functions #131352

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 5 additions & 33 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,36 +51,18 @@ extern "C" {

/* Check if the current thread is the main thread.
Use _Py_IsMainInterpreter() to check if it's the main interpreter. */
static inline int
_Py_IsMainThread(void)
{
unsigned long thread = PyThread_get_thread_ident();
return (thread == _PyRuntime.main_thread);
}

extern int _Py_IsMainThread(void);

static inline PyInterpreterState *
_PyInterpreterState_Main(void)
{
return _PyRuntime.interpreters.main;
}
// Export for '_testinternalcapi' shared extension
PyAPI_FUNC(PyInterpreterState*) _PyInterpreterState_Main(void);

static inline int
_Py_IsMainInterpreter(PyInterpreterState *interp)
{
return (interp == _PyInterpreterState_Main());
}

static inline int
_Py_IsMainInterpreterFinalizing(PyInterpreterState *interp)
{
/* bpo-39877: Access _PyRuntime directly rather than using
tstate->interp->runtime to support calls from Python daemon threads.
After Py_Finalize() has been called, tstate can be a dangling pointer:
point to PyThreadState freed memory. */
return (_PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL &&
interp == &_PyRuntime._main_interpreter);
}
extern int _Py_IsMainInterpreterFinalizing(PyInterpreterState *interp);

// Export for _interpreters module.
PyAPI_FUNC(PyObject *) _PyInterpreterState_GetIDObject(PyInterpreterState *);
Expand All @@ -93,17 +75,7 @@ PyAPI_FUNC(void) _PyErr_SetInterpreterAlreadyRunning(void);

extern int _PyThreadState_IsRunningMain(PyThreadState *);
extern void _PyInterpreterState_ReinitRunningMain(PyThreadState *);


static inline const PyConfig *
_Py_GetMainConfig(void)
{
PyInterpreterState *interp = _PyInterpreterState_Main();
if (interp == NULL) {
return NULL;
}
return _PyInterpreterState_GetConfig(interp);
}
extern const PyConfig* _Py_GetMainConfig(void);


/* Only handle signals on the main thread of the main interpreter. */
Expand Down
38 changes: 38 additions & 0 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -3137,3 +3137,41 @@ _PyThreadState_ClearMimallocHeaps(PyThreadState *tstate)
}
#endif
}


int
_Py_IsMainThread(void)
{
unsigned long thread = PyThread_get_thread_ident();
return (thread == _PyRuntime.main_thread);
}


PyInterpreterState *
_PyInterpreterState_Main(void)
{
return _PyRuntime.interpreters.main;
}


int
_Py_IsMainInterpreterFinalizing(PyInterpreterState *interp)
{
/* bpo-39877: Access _PyRuntime directly rather than using
tstate->interp->runtime to support calls from Python daemon threads.
After Py_Finalize() has been called, tstate can be a dangling pointer:
point to PyThreadState freed memory. */
return (_PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL &&
interp == &_PyRuntime._main_interpreter);
}


const PyConfig *
_Py_GetMainConfig(void)
{
PyInterpreterState *interp = _PyInterpreterState_Main();
if (interp == NULL) {
return NULL;
}
return _PyInterpreterState_GetConfig(interp);
}
Loading