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,5 +1,6 @@
#include "Python.h"
#include "pycore_ast.h" // expr_ty
#include "pycore_runtime.h" // _Py_ID()
#include <float.h> // DBL_MAX_10_EXP
#include <stdbool.h>
@ -8,11 +9,10 @@
* See ast.unparse for a full unparser (written in Python)
*/
static PyObject *_str_open_br;
static PyObject *_str_dbl_open_br;
static PyObject *_str_close_br;
static PyObject *_str_dbl_close_br;
static PyObject *_str_inf;
_Py_DECLARE_STR(open_br, "{");
_Py_DECLARE_STR(dbl_open_br, "{{");
_Py_DECLARE_STR(close_br, "}");
_Py_DECLARE_STR(dbl_close_br, "}}");
static PyObject *_str_replace_inf;
/* Forward declarations for recursion via helper functions. */
@ -80,7 +80,7 @@ append_repr(_PyUnicodeWriter *writer, PyObject *obj)
{
PyObject *new_repr = PyUnicode_Replace(
repr,
_str_inf,
&_Py_ID(inf),
_str_replace_inf,
-1
);
@ -575,11 +575,11 @@ escape_braces(PyObject *orig)
{
PyObject *temp;
PyObject *result;
temp = PyUnicode_Replace(orig, _str_open_br, _str_dbl_open_br, -1);
temp = PyUnicode_Replace(orig, &_Py_STR(open_br), &_Py_STR(dbl_open_br), -1);
if (!temp) {
return NULL;
}
result = PyUnicode_Replace(temp, _str_close_br, _str_dbl_close_br, -1);
result = PyUnicode_Replace(temp, &_Py_STR(close_br), &_Py_STR(dbl_close_br), -1);
Py_DECREF(temp);
return result;
}
@ -673,7 +673,7 @@ append_formattedvalue(_PyUnicodeWriter *writer, expr_ty e)
if (!temp_fv_str) {
return -1;
}
if (PyUnicode_Find(temp_fv_str, _str_open_br, 0, 1, 1) == 0) {
if (PyUnicode_Find(temp_fv_str, &_Py_STR(open_br), 0, 1, 1) == 0) {
/* Expression starts with a brace, split it with a space from the outer
one. */
outer_brace = "{ ";
@ -927,26 +927,6 @@ append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level)
static int
maybe_init_static_strings(void)
{
if (!_str_open_br &&
!(_str_open_br = PyUnicode_InternFromString("{"))) {
return -1;
}
if (!_str_dbl_open_br &&
!(_str_dbl_open_br = PyUnicode_InternFromString("{{"))) {
return -1;
}
if (!_str_close_br &&
!(_str_close_br = PyUnicode_InternFromString("}"))) {
return -1;
}
if (!_str_dbl_close_br &&
!(_str_dbl_close_br = PyUnicode_InternFromString("}}"))) {
return -1;
}
if (!_str_inf &&
!(_str_inf = PyUnicode_FromString("inf"))) {
return -1;
}
if (!_str_replace_inf &&
!(_str_replace_inf = PyUnicode_FromFormat("1e%d", 1 + DBL_MAX_10_EXP))) {
return -1;