aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Objects
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2022-02-22 17:23:51 -0700
committerGitHub <noreply@github.com>2022-02-22 17:23:51 -0700
commit1f455361ecfb1892e375bdbee813cdf095b6cfb8 (patch)
treed7def4d5d167965a45c4b0e30bb5a1a0bb5c3b4a /Objects
parentcff4d5c5d29528299ec1ac5b3b3a6f7735577c01 (diff)
downloadcpython-1f455361ecfb1892e375bdbee813cdf095b6cfb8.tar.gz
cpython-1f455361ecfb1892e375bdbee813cdf095b6cfb8.zip
bpo-46765: Replace Locally Cached Strings with Statically Initialized Objects (gh-31366)
https://bugs.python.org/issue46765
Diffstat (limited to 'Objects')
-rw-r--r--Objects/boolobject.c15
-rw-r--r--Objects/classobject.c17
-rw-r--r--Objects/codeobject.c7
-rw-r--r--Objects/listobject.c23
4 files changed, 10 insertions, 52 deletions
diff --git a/Objects/boolobject.c b/Objects/boolobject.c
index 53f81926057..d86958aff9c 100644
--- a/Objects/boolobject.c
+++ b/Objects/boolobject.c
@@ -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 */
diff --git a/Objects/classobject.c b/Objects/classobject.c
index d7ccf31244e..3b1c25394f1 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -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[] = {
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index 9a27f1295b1..f8ef1e6b40c 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -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);
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 1ba1c1b0531..783ae88a17f 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -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]);