Skip to content

Commit 0453e49

Browse files
authored
gh-131238: Convert pycore_pystate.h static inline to functions (#131352)
Convert static inline functions to functions: * _Py_IsMainThread() * _PyInterpreterState_Main() * _Py_IsMainInterpreterFinalizing() * _Py_GetMainConfig()
1 parent 80e00ec commit 0453e49

File tree

2 files changed

+43
-33
lines changed

2 files changed

+43
-33
lines changed

Include/internal/pycore_pystate.h

+5-33
Original file line numberDiff line numberDiff line change
@@ -51,36 +51,18 @@ extern "C" {
5151

5252
/* Check if the current thread is the main thread.
5353
Use _Py_IsMainInterpreter() to check if it's the main interpreter. */
54-
static inline int
55-
_Py_IsMainThread(void)
56-
{
57-
unsigned long thread = PyThread_get_thread_ident();
58-
return (thread == _PyRuntime.main_thread);
59-
}
60-
54+
extern int _Py_IsMainThread(void);
6155

62-
static inline PyInterpreterState *
63-
_PyInterpreterState_Main(void)
64-
{
65-
return _PyRuntime.interpreters.main;
66-
}
56+
// Export for '_testinternalcapi' shared extension
57+
PyAPI_FUNC(PyInterpreterState*) _PyInterpreterState_Main(void);
6758

6859
static inline int
6960
_Py_IsMainInterpreter(PyInterpreterState *interp)
7061
{
7162
return (interp == _PyInterpreterState_Main());
7263
}
7364

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

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

9476
extern int _PyThreadState_IsRunningMain(PyThreadState *);
9577
extern void _PyInterpreterState_ReinitRunningMain(PyThreadState *);
96-
97-
98-
static inline const PyConfig *
99-
_Py_GetMainConfig(void)
100-
{
101-
PyInterpreterState *interp = _PyInterpreterState_Main();
102-
if (interp == NULL) {
103-
return NULL;
104-
}
105-
return _PyInterpreterState_GetConfig(interp);
106-
}
78+
extern const PyConfig* _Py_GetMainConfig(void);
10779

10880

10981
/* Only handle signals on the main thread of the main interpreter. */

Python/pystate.c

+38
Original file line numberDiff line numberDiff line change
@@ -3137,3 +3137,41 @@ _PyThreadState_ClearMimallocHeaps(PyThreadState *tstate)
31373137
}
31383138
#endif
31393139
}
3140+
3141+
3142+
int
3143+
_Py_IsMainThread(void)
3144+
{
3145+
unsigned long thread = PyThread_get_thread_ident();
3146+
return (thread == _PyRuntime.main_thread);
3147+
}
3148+
3149+
3150+
PyInterpreterState *
3151+
_PyInterpreterState_Main(void)
3152+
{
3153+
return _PyRuntime.interpreters.main;
3154+
}
3155+
3156+
3157+
int
3158+
_Py_IsMainInterpreterFinalizing(PyInterpreterState *interp)
3159+
{
3160+
/* bpo-39877: Access _PyRuntime directly rather than using
3161+
tstate->interp->runtime to support calls from Python daemon threads.
3162+
After Py_Finalize() has been called, tstate can be a dangling pointer:
3163+
point to PyThreadState freed memory. */
3164+
return (_PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL &&
3165+
interp == &_PyRuntime._main_interpreter);
3166+
}
3167+
3168+
3169+
const PyConfig *
3170+
_Py_GetMainConfig(void)
3171+
{
3172+
PyInterpreterState *interp = _PyInterpreterState_Main();
3173+
if (interp == NULL) {
3174+
return NULL;
3175+
}
3176+
return _PyInterpreterState_GetConfig(interp);
3177+
}

0 commit comments

Comments
 (0)