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

@ -706,7 +706,6 @@ sys_displayhook(PyObject *module, PyObject *o)
{
PyObject *outf;
PyObject *builtins;
static PyObject *newline = NULL;
PyThreadState *tstate = _PyThreadState_GET();
builtins = PyImport_GetModule(&_Py_ID(builtins));
@ -747,12 +746,8 @@ sys_displayhook(PyObject *module, PyObject *o)
return NULL;
}
}
if (newline == NULL) {
newline = PyUnicode_FromString("\n");
if (newline == NULL)
return NULL;
}
if (PyFile_WriteObject(newline, outf, Py_PRINT_RAW) != 0)
_Py_DECLARE_STR(newline, "\n");
if (PyFile_WriteObject(&_Py_STR(newline), outf, Py_PRINT_RAW) != 0)
return NULL;
if (PyObject_SetAttr(builtins, &_Py_ID(_), o) != 0)
return NULL;
@ -946,30 +941,18 @@ sys_intern_impl(PyObject *module, PyObject *s)
/*
* Cached interned string objects used for calling the profile and
* trace functions. Initialized by trace_init().
* trace functions.
*/
static PyObject *whatstrings[8] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static int
trace_init(void)
{
static const char * const whatnames[8] = {
"call", "exception", "line", "return",
"c_call", "c_exception", "c_return",
"opcode"
};
PyObject *name;
int i;
for (i = 0; i < 8; ++i) {
if (whatstrings[i] == NULL) {
name = PyUnicode_InternFromString(whatnames[i]);
if (name == NULL)
return -1;
whatstrings[i] = name;
}
}
return 0;
}
static PyObject *whatstrings[8] = {
&_Py_ID(call),
&_Py_ID(exception),
&_Py_ID(line),
&_Py_ID(return),
&_Py_ID(c_call),
&_Py_ID(c_exception),
&_Py_ID(c_return),
&_Py_ID(opcode),
};
static PyObject *
@ -1050,10 +1033,6 @@ trace_trampoline(PyObject *self, PyFrameObject *frame,
static PyObject *
sys_settrace(PyObject *self, PyObject *args)
{
if (trace_init() == -1) {
return NULL;
}
PyThreadState *tstate = _PyThreadState_GET();
if (args == Py_None) {
if (_PyEval_SetTrace(tstate, NULL, NULL) < 0) {
@ -1099,10 +1078,6 @@ sys_gettrace_impl(PyObject *module)
static PyObject *
sys_setprofile(PyObject *self, PyObject *args)
{
if (trace_init() == -1) {
return NULL;
}
PyThreadState *tstate = _PyThreadState_GET();
if (args == Py_None) {
if (_PyEval_SetProfile(tstate, NULL, NULL) < 0) {