Skip to content

Commit 18b1fde

Browse files
authored
gh-106320: Remove _PyInterpreterState_Get() alias (#106321)
Replace calls to the (removed) slow _PyInterpreterState_Get() with fast inlined _PyInterpreterState_GET() function.
1 parent 0530f4f commit 18b1fde

File tree

11 files changed

+29
-22
lines changed

11 files changed

+29
-22
lines changed

Doc/whatsnew/3.13.rst

+7
Original file line numberDiff line numberDiff line change
@@ -605,3 +605,10 @@ Removed
605605

606606
* Remove ``cpython/pytime.h`` header file: it only contained private functions.
607607
(Contributed by Victor Stinner in :gh:`106316`.)
608+
609+
* Remove ``_PyInterpreterState_Get()`` alias to
610+
:c:func:`PyInterpreterState_Get()` which was kept for backward compatibility
611+
with Python 3.8. The `pythoncapi-compat project
612+
<https://github.com/python/pythoncapi-compat/>`__ can be used to get
613+
:c:func:`PyInterpreterState_Get()` on Python 3.8 and older.
614+
(Contributed by Victor Stinner in :gh:`106320`.)

Include/cpython/pystate.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,6 @@ struct _ts {
261261

262262
/* other API */
263263

264-
// Alias for backward compatibility with Python 3.8
265-
#define _PyInterpreterState_Get PyInterpreterState_Get
266-
267264
/* An alias for the internal _PyThreadState_New(),
268265
kept for stable ABI compatibility. */
269266
PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
@@ -295,7 +292,7 @@ PyAPI_FUNC(int) PyGILState_Check(void);
295292
This function doesn't check for error. Return NULL before _PyGILState_Init()
296293
is called and after _PyGILState_Fini() is called.
297294
298-
See also _PyInterpreterState_Get() and _PyInterpreterState_GET(). */
295+
See also PyInterpreterState_Get() and _PyInterpreterState_GET(). */
299296
PyAPI_FUNC(PyInterpreterState *) _PyGILState_GetInterpreterStateUnsafe(void);
300297

301298
/* The implementation of sys._current_frames() Returns a dict mapping

Include/internal/pycore_pystate.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ _Py_EnsureFuncTstateNotNULL(const char *func, PyThreadState *tstate)
108108
109109
The caller must hold the GIL.
110110
111-
See also _PyInterpreterState_Get()
111+
See also PyInterpreterState_Get()
112112
and _PyGILState_GetInterpreterStateUnsafe(). */
113113
static inline PyInterpreterState* _PyInterpreterState_GET(void) {
114114
PyThreadState *tstate = _PyThreadState_GET();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Remove ``_PyInterpreterState_Get()`` alias to
2+
:c:func:`PyInterpreterState_Get()` which was kept for backward compatibility
3+
with Python 3.8. Patch by Victor Stinner.

Modules/_threadmodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ thread_run(void *boot_raw)
11041104
static PyObject *
11051105
thread_daemon_threads_allowed(PyObject *module, PyObject *Py_UNUSED(ignored))
11061106
{
1107-
PyInterpreterState *interp = _PyInterpreterState_Get();
1107+
PyInterpreterState *interp = _PyInterpreterState_GET();
11081108
if (interp->feature_flags & Py_RTFLAGS_DAEMON_THREADS) {
11091109
Py_RETURN_TRUE;
11101110
}

Objects/typeobject.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -6362,7 +6362,7 @@ object___reduce_ex___impl(PyObject *self, int protocol)
63626362
/*[clinic end generated code: output=2e157766f6b50094 input=f326b43fb8a4c5ff]*/
63636363
{
63646364
#define objreduce \
6365-
(_Py_INTERP_CACHED_OBJECT(_PyInterpreterState_Get(), objreduce))
6365+
(_Py_INTERP_CACHED_OBJECT(_PyInterpreterState_GET(), objreduce))
63666366
PyObject *reduce, *res;
63676367

63686368
if (objreduce == NULL) {
@@ -9688,7 +9688,7 @@ resolve_slotdups(PyTypeObject *type, PyObject *name)
96889688
/* XXX Maybe this could be optimized more -- but is it worth it? */
96899689

96909690
/* pname and ptrs act as a little cache */
9691-
PyInterpreterState *interp = _PyInterpreterState_Get();
9691+
PyInterpreterState *interp = _PyInterpreterState_GET();
96929692
#define pname _Py_INTERP_CACHED_OBJECT(interp, type_slots_pname)
96939693
#define ptrs _Py_INTERP_CACHED_OBJECT(interp, type_slots_ptrs)
96949694
pytype_slotdef *p, **pp;

Objects/unicodeobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -5831,7 +5831,7 @@ _PyUnicode_DecodeUnicodeEscapeInternal(const char *s,
58315831
PyObject *errorHandler = NULL;
58325832
PyObject *exc = NULL;
58335833
_PyUnicode_Name_CAPI *ucnhash_capi;
5834-
PyInterpreterState *interp = _PyInterpreterState_Get();
5834+
PyInterpreterState *interp = _PyInterpreterState_GET();
58355835

58365836
// so we can remember if we've seen an invalid escape char or not
58375837
*first_invalid_escape = NULL;

Python/ceval_gil.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -491,15 +491,15 @@ take_gil(PyThreadState *tstate)
491491

492492
void _PyEval_SetSwitchInterval(unsigned long microseconds)
493493
{
494-
PyInterpreterState *interp = _PyInterpreterState_Get();
494+
PyInterpreterState *interp = _PyInterpreterState_GET();
495495
struct _gil_runtime_state *gil = interp->ceval.gil;
496496
assert(gil != NULL);
497497
gil->interval = microseconds;
498498
}
499499

500500
unsigned long _PyEval_GetSwitchInterval(void)
501501
{
502-
PyInterpreterState *interp = _PyInterpreterState_Get();
502+
PyInterpreterState *interp = _PyInterpreterState_GET();
503503
struct _gil_runtime_state *gil = interp->ceval.gil;
504504
assert(gil != NULL);
505505
return gil->interval;

Python/hamt.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2425,7 +2425,7 @@ hamt_alloc(void)
24252425
}
24262426

24272427
#define _empty_hamt \
2428-
(&_Py_INTERP_SINGLETON(_PyInterpreterState_Get(), hamt_empty))
2428+
(&_Py_INTERP_SINGLETON(_PyInterpreterState_GET(), hamt_empty))
24292429

24302430
PyHamtObject *
24312431
_PyHamt_New(void)

Python/import.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ check_multi_interp_extensions(PyInterpreterState *interp)
11231123
int
11241124
_PyImport_CheckSubinterpIncompatibleExtensionAllowed(const char *name)
11251125
{
1126-
PyInterpreterState *interp = _PyInterpreterState_Get();
1126+
PyInterpreterState *interp = _PyInterpreterState_GET();
11271127
if (check_multi_interp_extensions(interp)) {
11281128
assert(!_Py_IsMainInterpreter(interp));
11291129
PyErr_Format(PyExc_ImportError,

Python/instrumentation.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,7 @@ _Py_call_instrumentation_instruction(PyThreadState *tstate, _PyInterpreterFrame*
12191219
PyObject *
12201220
_PyMonitoring_RegisterCallback(int tool_id, int event_id, PyObject *obj)
12211221
{
1222-
PyInterpreterState *is = _PyInterpreterState_Get();
1222+
PyInterpreterState *is = _PyInterpreterState_GET();
12231223
assert(0 <= tool_id && tool_id < PY_MONITORING_TOOL_IDS);
12241224
assert(0 <= event_id && event_id < PY_MONITORING_EVENTS);
12251225
PyObject *callback = is->monitoring_callables[tool_id][event_id];
@@ -1678,7 +1678,7 @@ int
16781678
_PyMonitoring_SetEvents(int tool_id, _PyMonitoringEventSet events)
16791679
{
16801680
assert(0 <= tool_id && tool_id < PY_MONITORING_TOOL_IDS);
1681-
PyInterpreterState *interp = _PyInterpreterState_Get();
1681+
PyInterpreterState *interp = _PyInterpreterState_GET();
16821682
assert(events < (1 << PY_MONITORING_UNGROUPED_EVENTS));
16831683
if (check_tool(interp, tool_id)) {
16841684
return -1;
@@ -1696,7 +1696,7 @@ int
16961696
_PyMonitoring_SetLocalEvents(PyCodeObject *code, int tool_id, _PyMonitoringEventSet events)
16971697
{
16981698
assert(0 <= tool_id && tool_id < PY_MONITORING_TOOL_IDS);
1699-
PyInterpreterState *interp = _PyInterpreterState_Get();
1699+
PyInterpreterState *interp = _PyInterpreterState_GET();
17001700
assert(events < (1 << PY_MONITORING_UNGROUPED_EVENTS));
17011701
if (check_tool(interp, tool_id)) {
17021702
return -1;
@@ -1758,7 +1758,7 @@ monitoring_use_tool_id_impl(PyObject *module, int tool_id, PyObject *name)
17581758
PyErr_SetString(PyExc_ValueError, "tool name must be a str");
17591759
return NULL;
17601760
}
1761-
PyInterpreterState *interp = _PyInterpreterState_Get();
1761+
PyInterpreterState *interp = _PyInterpreterState_GET();
17621762
if (interp->monitoring_tool_names[tool_id] != NULL) {
17631763
PyErr_Format(PyExc_ValueError, "tool %d is already in use", tool_id);
17641764
return NULL;
@@ -1782,7 +1782,7 @@ monitoring_free_tool_id_impl(PyObject *module, int tool_id)
17821782
if (check_valid_tool(tool_id)) {
17831783
return NULL;
17841784
}
1785-
PyInterpreterState *interp = _PyInterpreterState_Get();
1785+
PyInterpreterState *interp = _PyInterpreterState_GET();
17861786
Py_CLEAR(interp->monitoring_tool_names[tool_id]);
17871787
Py_RETURN_NONE;
17881788
}
@@ -1804,7 +1804,7 @@ monitoring_get_tool_impl(PyObject *module, int tool_id)
18041804
if (check_valid_tool(tool_id)) {
18051805
return NULL;
18061806
}
1807-
PyInterpreterState *interp = _PyInterpreterState_Get();
1807+
PyInterpreterState *interp = _PyInterpreterState_GET();
18081808
PyObject *name = interp->monitoring_tool_names[tool_id];
18091809
if (name == NULL) {
18101810
Py_RETURN_NONE;
@@ -1865,7 +1865,7 @@ monitoring_get_events_impl(PyObject *module, int tool_id)
18651865
if (check_valid_tool(tool_id)) {
18661866
return -1;
18671867
}
1868-
_Py_Monitors *m = &_PyInterpreterState_Get()->monitors;
1868+
_Py_Monitors *m = &_PyInterpreterState_GET()->monitors;
18691869
_PyMonitoringEventSet event_set = get_events(m, tool_id);
18701870
return event_set;
18711871
}
@@ -1990,7 +1990,7 @@ monitoring_restart_events_impl(PyObject *module)
19901990
* last restart version > instrumented version for all code objects
19911991
* last restart version < current version
19921992
*/
1993-
PyInterpreterState *interp = _PyInterpreterState_Get();
1993+
PyInterpreterState *interp = _PyInterpreterState_GET();
19941994
interp->last_restart_version = interp->monitoring_version + 1;
19951995
interp->monitoring_version = interp->last_restart_version + 1;
19961996
if (instrument_all_executing_code_objects(interp)) {
@@ -2038,7 +2038,7 @@ static PyObject *
20382038
monitoring__all_events_impl(PyObject *module)
20392039
/*[clinic end generated code: output=6b7581e2dbb690f6 input=62ee9672c17b7f0e]*/
20402040
{
2041-
PyInterpreterState *interp = _PyInterpreterState_Get();
2041+
PyInterpreterState *interp = _PyInterpreterState_GET();
20422042
PyObject *res = PyDict_New();
20432043
if (res == NULL) {
20442044
return NULL;

0 commit comments

Comments
 (0)