bpo-46765: Replace Locally Cached Strings with Statically Initialized Objects (gh-31366)

https://bugs.python.org/issue46765
This commit is contained in:
Eric Snow 2022-02-22 17:23:51 -07:00 committed by GitHub
parent cff4d5c5d2
commit 1f455361ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 192 additions and 526 deletions

View file

@ -1,26 +1,15 @@
/* Boolean type, a subtype of int */
#include "Python.h"
#include "pycore_runtime.h" // _Py_ID()
#include "pycore_pyerrors.h" // _Py_FatalRefcountError()
/* We define bool_repr to return "False" or "True" */
static PyObject *false_str = NULL;
static PyObject *true_str = NULL;
static PyObject *
bool_repr(PyObject *self)
{
PyObject *s;
if (self == Py_True)
s = true_str ? true_str :
(true_str = PyUnicode_InternFromString("True"));
else
s = false_str ? false_str :
(false_str = PyUnicode_InternFromString("False"));
Py_XINCREF(s);
return s;
return self == Py_True ? &_Py_ID(True) : &_Py_ID(False);
}
/* Function to return a bool from a C long */

View file

@ -157,13 +157,7 @@ static PyMemberDef method_memberlist[] = {
static PyObject *
method_get_doc(PyMethodObject *im, void *context)
{
static PyObject *docstr;
if (docstr == NULL) {
docstr= PyUnicode_InternFromString("__doc__");
if (docstr == NULL)
return NULL;
}
return PyObject_GetAttr(im->im_func, docstr);
return PyObject_GetAttr(im->im_func, &_Py_ID(__doc__));
}
static PyGetSetDef method_getset[] = {
@ -405,13 +399,8 @@ static PyMemberDef instancemethod_memberlist[] = {
static PyObject *
instancemethod_get_doc(PyObject *self, void *context)
{
static PyObject *docstr;
if (docstr == NULL) {
docstr = PyUnicode_InternFromString("__doc__");
if (docstr == NULL)
return NULL;
}
return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self),
&_Py_ID(__doc__));
}
static PyGetSetDef instancemethod_getset[] = {

View file

@ -553,16 +553,11 @@ PyCode_New(int argcount, int kwonlyargcount,
PyCodeObject *
PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
{
PyObject *emptystring = NULL;
PyObject *nulltuple = NULL;
PyObject *filename_ob = NULL;
PyObject *funcname_ob = NULL;
PyCodeObject *result = NULL;
emptystring = PyBytes_FromString("");
if (emptystring == NULL) {
goto failed;
}
nulltuple = PyTuple_New(0);
if (nulltuple == NULL) {
goto failed;
@ -576,6 +571,7 @@ PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
goto failed;
}
#define emptystring (PyObject *)&_Py_SINGLETON(bytes_empty)
struct _PyCodeConstructor con = {
.filename = filename_ob,
.name = funcname_ob,
@ -594,7 +590,6 @@ PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
result = _PyCode_New(&con);
failed:
Py_XDECREF(emptystring);
Py_XDECREF(nulltuple);
Py_XDECREF(funcname_ob);
Py_XDECREF(filename_ob);

View file

@ -15,7 +15,7 @@ class list "PyListObject *" "&PyList_Type"
#include "clinic/listobject.c.h"
static PyObject *indexerr = NULL;
_Py_DECLARE_STR(list_err, "list index out of range");
#if PyList_MAXFREELIST > 0
static struct _Py_list_state *
@ -125,10 +125,6 @@ _PyList_Fini(PyInterpreterState *interp)
struct _Py_list_state *state = &interp->list;
state->numfree = -1;
#endif
if (_Py_IsMainInterpreter(interp)) {
Py_CLEAR(indexerr);
}
}
/* Print summary info about the state of the optimized allocator */
@ -238,13 +234,8 @@ 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;
}
PyErr_SetObject(PyExc_IndexError, indexerr);
_Py_DECLARE_STR(list_err, "list index out of range");
PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
return NULL;
}
return ((PyListObject *)op) -> ob_item[i];
@ -452,13 +443,7 @@ 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;
}
PyErr_SetObject(PyExc_IndexError, indexerr);
PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
return NULL;
}
Py_INCREF(a->ob_item[i]);