From ad3e39dc277b89e39b58d603bfd9587001fb2a6a Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 18 Nov 2024 17:11:12 -0700 Subject: [PATCH 1/2] gh-126986: Drop _PyInterpreterState_FailIfNotRunning() (gh-126988) We replace it with _PyErr_SetInterpreterAlreadyRunning(). --- Include/internal/pycore_pystate.h | 2 +- Python/crossinterp.c | 3 +-- Python/pystate.c | 20 ++++++++------------ 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h index b0e72523f58ed8..0df2d537e1dc67 100644 --- a/Include/internal/pycore_pystate.h +++ b/Include/internal/pycore_pystate.h @@ -84,7 +84,7 @@ PyAPI_FUNC(PyObject *) _PyInterpreterState_GetIDObject(PyInterpreterState *); PyAPI_FUNC(int) _PyInterpreterState_SetRunningMain(PyInterpreterState *); PyAPI_FUNC(void) _PyInterpreterState_SetNotRunningMain(PyInterpreterState *); PyAPI_FUNC(int) _PyInterpreterState_IsRunningMain(PyInterpreterState *); -PyAPI_FUNC(int) _PyInterpreterState_FailIfRunningMain(PyInterpreterState *); +PyAPI_FUNC(void) _PyErr_SetInterpreterAlreadyRunning(void); extern int _PyThreadState_IsRunningMain(PyThreadState *); extern void _PyInterpreterState_ReinitRunningMain(PyThreadState *); diff --git a/Python/crossinterp.c b/Python/crossinterp.c index 2af0dd6191aa4e..f6b5324e27073c 100644 --- a/Python/crossinterp.c +++ b/Python/crossinterp.c @@ -985,8 +985,7 @@ _PyXI_ApplyErrorCode(_PyXI_errcode code, PyInterpreterState *interp) break; case _PyXI_ERR_ALREADY_RUNNING: assert(interp != NULL); - assert(_PyInterpreterState_IsRunningMain(interp)); - _PyInterpreterState_FailIfRunningMain(interp); + _PyErr_SetInterpreterAlreadyRunning(); break; case _PyXI_ERR_MAIN_NS_FAILURE: PyErr_SetString(PyExc_InterpreterError, diff --git a/Python/pystate.c b/Python/pystate.c index ad3fdce69bf1d5..bc099ee5027771 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1050,10 +1050,17 @@ get_main_thread(PyInterpreterState *interp) return _Py_atomic_load_ptr_relaxed(&interp->threads.main); } +void +_PyErr_SetInterpreterAlreadyRunning(void) +{ + PyErr_SetString(PyExc_InterpreterError, "interpreter already running"); +} + int _PyInterpreterState_SetRunningMain(PyInterpreterState *interp) { - if (_PyInterpreterState_FailIfRunningMain(interp) < 0) { + if (get_main_thread(interp) != NULL) { + _PyErr_SetInterpreterAlreadyRunning(); return -1; } PyThreadState *tstate = current_fast_get(); @@ -1099,17 +1106,6 @@ _PyThreadState_IsRunningMain(PyThreadState *tstate) return get_main_thread(interp) == tstate; } -int -_PyInterpreterState_FailIfRunningMain(PyInterpreterState *interp) -{ - if (get_main_thread(interp) != NULL) { - PyErr_SetString(PyExc_InterpreterError, - "interpreter already running"); - return -1; - } - return 0; -} - void _PyInterpreterState_ReinitRunningMain(PyThreadState *tstate) { From b927efcc2b015de8f4a6547b7593fc1813bdaa35 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Thu, 21 Nov 2024 09:38:22 -0700 Subject: [PATCH 2/2] Do not backport the API/ABI changes. --- Include/internal/pycore_pystate.h | 2 +- Python/crossinterp.c | 3 ++- Python/pystate.c | 21 ++++++++++++++------- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h index 0df2d537e1dc67..b0e72523f58ed8 100644 --- a/Include/internal/pycore_pystate.h +++ b/Include/internal/pycore_pystate.h @@ -84,7 +84,7 @@ PyAPI_FUNC(PyObject *) _PyInterpreterState_GetIDObject(PyInterpreterState *); PyAPI_FUNC(int) _PyInterpreterState_SetRunningMain(PyInterpreterState *); PyAPI_FUNC(void) _PyInterpreterState_SetNotRunningMain(PyInterpreterState *); PyAPI_FUNC(int) _PyInterpreterState_IsRunningMain(PyInterpreterState *); -PyAPI_FUNC(void) _PyErr_SetInterpreterAlreadyRunning(void); +PyAPI_FUNC(int) _PyInterpreterState_FailIfRunningMain(PyInterpreterState *); extern int _PyThreadState_IsRunningMain(PyThreadState *); extern void _PyInterpreterState_ReinitRunningMain(PyThreadState *); diff --git a/Python/crossinterp.c b/Python/crossinterp.c index f6b5324e27073c..01bb45d23bac1b 100644 --- a/Python/crossinterp.c +++ b/Python/crossinterp.c @@ -985,7 +985,8 @@ _PyXI_ApplyErrorCode(_PyXI_errcode code, PyInterpreterState *interp) break; case _PyXI_ERR_ALREADY_RUNNING: assert(interp != NULL); - _PyErr_SetInterpreterAlreadyRunning(); + // In 3.14+ we use _PyErr_SetInterpreterAlreadyRunning(). + PyErr_SetString(PyExc_InterpreterError, "interpreter already running"); break; case _PyXI_ERR_MAIN_NS_FAILURE: PyErr_SetString(PyExc_InterpreterError, diff --git a/Python/pystate.c b/Python/pystate.c index bc099ee5027771..960895e5badc27 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1050,17 +1050,12 @@ get_main_thread(PyInterpreterState *interp) return _Py_atomic_load_ptr_relaxed(&interp->threads.main); } -void -_PyErr_SetInterpreterAlreadyRunning(void) -{ - PyErr_SetString(PyExc_InterpreterError, "interpreter already running"); -} - int _PyInterpreterState_SetRunningMain(PyInterpreterState *interp) { if (get_main_thread(interp) != NULL) { - _PyErr_SetInterpreterAlreadyRunning(); + // In 3.14+ we use _PyErr_SetInterpreterAlreadyRunning(). + PyErr_SetString(PyExc_InterpreterError, "interpreter already running"); return -1; } PyThreadState *tstate = current_fast_get(); @@ -1106,6 +1101,18 @@ _PyThreadState_IsRunningMain(PyThreadState *tstate) return get_main_thread(interp) == tstate; } +// This has been removed in 3.14. +int +_PyInterpreterState_FailIfRunningMain(PyInterpreterState *interp) +{ + if (get_main_thread(interp) != NULL) { + PyErr_SetString(PyExc_InterpreterError, + "interpreter already running"); + return -1; + } + return 0; +} + void _PyInterpreterState_ReinitRunningMain(PyThreadState *tstate) {