Skip to content

Commit 81a7be3

Browse files
authored
bpo-40268: Rename _PyInterpreterState_GET_UNSAFE() (pythonGH-19509)
Rename _PyInterpreterState_GET_UNSAFE() to _PyInterpreterState_GET() for consistency with _PyThreadState_GET() and to have a shorter name (help to fit into 80 columns). Add also "assert(tstate != NULL);" to the function.
1 parent 4a3fe08 commit 81a7be3

23 files changed

+53
-52
lines changed

Include/cpython/pystate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ PyAPI_FUNC(int) PyGILState_Check(void);
165165
This function doesn't check for error. Return NULL before _PyGILState_Init()
166166
is called and after _PyGILState_Fini() is called.
167167
168-
See also _PyInterpreterState_Get() and _PyInterpreterState_GET_UNSAFE(). */
168+
See also _PyInterpreterState_Get() and _PyInterpreterState_GET(). */
169169
PyAPI_FUNC(PyInterpreterState *) _PyGILState_GetInterpreterStateUnsafe(void);
170170

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

Include/internal/pycore_pystate.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ static inline PyThreadState *_PyThreadState_GET(void) {
7878
7979
See also _PyInterpreterState_Get()
8080
and _PyGILState_GetInterpreterStateUnsafe(). */
81-
static inline PyInterpreterState* _PyInterpreterState_GET_UNSAFE(void) {
81+
static inline PyInterpreterState* _PyInterpreterState_GET(void) {
8282
PyThreadState *tstate = _PyThreadState_GET();
83+
assert(tstate != NULL);
8384
return tstate->interp;
8485
}
8586

Modules/_io/textio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ io_check_errors(PyObject *errors)
994994
{
995995
assert(errors != NULL && errors != Py_None);
996996

997-
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
997+
PyInterpreterState *interp = _PyInterpreterState_GET();
998998
#ifndef Py_DEBUG
999999
/* In release mode, only check in development mode (-X dev) */
10001000
if (!_PyInterpreterState_GetConfig(interp)->dev_mode) {

Modules/_threadmodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
10911091
boot = PyMem_NEW(struct bootstate, 1);
10921092
if (boot == NULL)
10931093
return PyErr_NoMemory();
1094-
boot->interp = _PyInterpreterState_GET_UNSAFE();
1094+
boot->interp = _PyInterpreterState_GET();
10951095
boot->func = func;
10961096
boot->args = args;
10971097
boot->keyw = keyw;
@@ -1213,7 +1213,7 @@ particular thread within a system.");
12131213
static PyObject *
12141214
thread__count(PyObject *self, PyObject *Py_UNUSED(ignored))
12151215
{
1216-
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
1216+
PyInterpreterState *interp = _PyInterpreterState_GET();
12171217
return PyLong_FromLong(interp->num_threads);
12181218
}
12191219

@@ -1556,7 +1556,7 @@ PyInit__thread(void)
15561556
PyObject *m, *d, *v;
15571557
double time_max;
15581558
double timeout_max;
1559-
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
1559+
PyInterpreterState *interp = _PyInterpreterState_GET();
15601560

15611561
/* Initialize types: */
15621562
if (PyType_Ready(&localdummytype) < 0)

Modules/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ pymain_repl(PyConfig *config, PyCompilerFlags *cf, int *exitcode)
498498
static void
499499
pymain_run_python(int *exitcode)
500500
{
501-
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
501+
PyInterpreterState *interp = _PyInterpreterState_GET();
502502
/* pymain_run_stdin() modify the config */
503503
PyConfig *config = (PyConfig*)_PyInterpreterState_GetConfig(interp);
504504

Modules/posixmodule.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ run_at_forkers(PyObject *lst, int reverse)
451451
void
452452
PyOS_BeforeFork(void)
453453
{
454-
run_at_forkers(_PyInterpreterState_GET_UNSAFE()->before_forkers, 1);
454+
run_at_forkers(_PyInterpreterState_GET()->before_forkers, 1);
455455

456456
_PyImport_AcquireLock();
457457
}
@@ -462,7 +462,7 @@ PyOS_AfterFork_Parent(void)
462462
if (_PyImport_ReleaseLock() <= 0)
463463
Py_FatalError("failed releasing import lock after fork");
464464

465-
run_at_forkers(_PyInterpreterState_GET_UNSAFE()->after_forkers_parent, 0);
465+
run_at_forkers(_PyInterpreterState_GET()->after_forkers_parent, 0);
466466
}
467467

468468
void
@@ -476,7 +476,7 @@ PyOS_AfterFork_Child(void)
476476
_PyRuntimeState_ReInitThreads(runtime);
477477
_PyInterpreterState_DeleteExceptMain(runtime);
478478

479-
run_at_forkers(_PyInterpreterState_GET_UNSAFE()->after_forkers_child, 0);
479+
run_at_forkers(_PyInterpreterState_GET()->after_forkers_child, 0);
480480
}
481481

482482
static int
@@ -6185,7 +6185,7 @@ os_register_at_fork_impl(PyObject *module, PyObject *before,
61856185
check_null_or_callable(after_in_parent, "after_in_parent")) {
61866186
return NULL;
61876187
}
6188-
interp = _PyInterpreterState_GET_UNSAFE();
6188+
interp = _PyInterpreterState_GET();
61896189

61906190
if (register_at_forker(&interp->before_forkers, before)) {
61916191
return NULL;
@@ -6216,7 +6216,7 @@ os_fork1_impl(PyObject *module)
62166216
{
62176217
pid_t pid;
62186218

6219-
if (_PyInterpreterState_GET_UNSAFE() != PyInterpreterState_Main()) {
6219+
if (_PyInterpreterState_GET() != PyInterpreterState_Main()) {
62206220
PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
62216221
return NULL;
62226222
}
@@ -6251,7 +6251,7 @@ os_fork_impl(PyObject *module)
62516251
{
62526252
pid_t pid;
62536253

6254-
if (_PyInterpreterState_GET_UNSAFE() != PyInterpreterState_Main()) {
6254+
if (_PyInterpreterState_GET() != PyInterpreterState_Main()) {
62556255
PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
62566256
return NULL;
62576257
}
@@ -6859,7 +6859,7 @@ os_forkpty_impl(PyObject *module)
68596859
int master_fd = -1;
68606860
pid_t pid;
68616861

6862-
if (_PyInterpreterState_GET_UNSAFE() != PyInterpreterState_Main()) {
6862+
if (_PyInterpreterState_GET() != PyInterpreterState_Main()) {
68636863
PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
68646864
return NULL;
68656865
}

Modules/signalmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,7 @@ PyOS_FiniInterrupts(void)
17841784
int
17851785
PyOS_InterruptOccurred(void)
17861786
{
1787-
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
1787+
PyInterpreterState *interp = _PyInterpreterState_GET();
17881788
if (!_Py_ThreadCanHandleSignals(interp)) {
17891789
return 0;
17901790
}
@@ -1821,7 +1821,7 @@ _PySignal_AfterFork(void)
18211821
int
18221822
_PyOS_IsMainThread(void)
18231823
{
1824-
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
1824+
PyInterpreterState *interp = _PyInterpreterState_GET();
18251825
return _Py_ThreadCanHandleSignals(interp);
18261826
}
18271827

Objects/codeobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "structmember.h"
77
#include "pycore_code.h"
88
#include "pycore_interp.h" // PyInterpreterState.co_extra_freefuncs
9-
#include "pycore_pystate.h" // _PyInterpreterState_GET_UNSAFE()
9+
#include "pycore_pystate.h" // _PyInterpreterState_GET()
1010
#include "pycore_tupleobject.h"
1111
#include "clinic/codeobject.c.h"
1212

@@ -555,7 +555,7 @@ code_dealloc(PyCodeObject *co)
555555
co->co_opcache_size = 0;
556556

557557
if (co->co_extra != NULL) {
558-
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
558+
PyInterpreterState *interp = _PyInterpreterState_GET();
559559
_PyCodeObjectExtra *co_extra = co->co_extra;
560560

561561
for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
@@ -1074,7 +1074,7 @@ _PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
10741074
int
10751075
_PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
10761076
{
1077-
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
1077+
PyInterpreterState *interp = _PyInterpreterState_GET();
10781078

10791079
if (!PyCode_Check(code) || index < 0 ||
10801080
index >= interp->co_extra_user_count) {

Objects/moduleobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ _add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
165165
PyObject *
166166
PyModule_Create2(struct PyModuleDef* module, int module_api_version)
167167
{
168-
if (!_PyImport_IsInitialized(_PyInterpreterState_GET_UNSAFE())) {
168+
if (!_PyImport_IsInitialized(_PyInterpreterState_GET())) {
169169
PyErr_SetString(PyExc_SystemError,
170170
"Python import machinery not initialized");
171171
return NULL;
@@ -684,7 +684,7 @@ module_dealloc(PyModuleObject *m)
684684
static PyObject *
685685
module_repr(PyModuleObject *m)
686686
{
687-
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
687+
PyInterpreterState *interp = _PyInterpreterState_GET();
688688

689689
return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
690690
}

Objects/unicodeobject.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ unicode_check_encoding_errors(const char *encoding, const char *errors)
436436
return 0;
437437
}
438438

439-
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
439+
PyInterpreterState *interp = _PyInterpreterState_GET();
440440
#ifndef Py_DEBUG
441441
/* In release mode, only check in development mode (-X dev) */
442442
if (!_PyInterpreterState_GetConfig(interp)->dev_mode) {
@@ -3615,7 +3615,7 @@ PyUnicode_EncodeLocale(PyObject *unicode, const char *errors)
36153615
PyObject *
36163616
PyUnicode_EncodeFSDefault(PyObject *unicode)
36173617
{
3618-
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
3618+
PyInterpreterState *interp = _PyInterpreterState_GET();
36193619
if (interp->fs_codec.utf8) {
36203620
return unicode_encode_utf8(unicode,
36213621
interp->fs_codec.error_handler,
@@ -3851,7 +3851,7 @@ PyUnicode_DecodeFSDefault(const char *s) {
38513851
PyObject*
38523852
PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size)
38533853
{
3854-
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
3854+
PyInterpreterState *interp = _PyInterpreterState_GET();
38553855
if (interp->fs_codec.utf8) {
38563856
return unicode_decode_utf8(s, size,
38573857
interp->fs_codec.error_handler,
@@ -16009,7 +16009,7 @@ _PyUnicode_FiniEncodings(PyThreadState *tstate)
1600916009
int
1601016010
_PyUnicode_EnableLegacyWindowsFSEncoding(void)
1601116011
{
16012-
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
16012+
PyInterpreterState *interp = _PyInterpreterState_GET();
1601316013
PyConfig *config = (PyConfig *)_PyInterpreterState_GetConfig(interp);
1601416014

1601516015
/* Set the filesystem encoding to mbcs/replace (PEP 529) */

0 commit comments

Comments
 (0)