From a2a390da44802c792e2727ca0390413db49ce6ee Mon Sep 17 00:00:00 2001 From: xiejunyi Date: Mon, 22 Mar 2021 22:16:00 +0800 Subject: [PATCH] fix listobject.c use static variables under building under building Python with --with-experimental-isolated-subinterpreters may cause crash --- Objects/listobject.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/Objects/listobject.c b/Objects/listobject.c index e7987a6d352bfa..05d37dc86590bc 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -12,6 +12,8 @@ #include /* For size_t */ #endif +_Py_static_string(_indexerr, "list index out of range"); + /*[clinic input] class list "PyListObject *" "&PyList_Type" [clinic start generated code]*/ @@ -218,8 +220,6 @@ valid_index(Py_ssize_t i, Py_ssize_t limit) return (size_t) i < (size_t) limit; } -static PyObject *indexerr = NULL; - PyObject * PyList_GetItem(PyObject *op, Py_ssize_t i) { @@ -228,11 +228,10 @@ PyList_GetItem(PyObject *op, Py_ssize_t i) return NULL; } if (!valid_index(i, Py_SIZE(op))) { - if (indexerr == NULL) { - indexerr = PyUnicode_FromString( - "list index out of range"); - if (indexerr == NULL) - return NULL; + PyObject *indexerr = _PyUnicode_FromId(&_indexerr); // borrowed ref + if (indexerr == NULL) + { + return NULL; } PyErr_SetObject(PyExc_IndexError, indexerr); return NULL; @@ -439,11 +438,10 @@ static PyObject * list_item(PyListObject *a, Py_ssize_t i) { if (!valid_index(i, Py_SIZE(a))) { - if (indexerr == NULL) { - indexerr = PyUnicode_FromString( - "list index out of range"); - if (indexerr == NULL) - return NULL; + PyObject *indexerr = _PyUnicode_FromId(&_indexerr); // borrowed ref + if (indexerr == NULL) + { + return NULL; } PyErr_SetObject(PyExc_IndexError, indexerr); return NULL;