summaryrefslogtreecommitdiffstats
path: root/Python/ast_unparse.c
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2022-02-23 00:23:51 (GMT)
committerGitHub <noreply@github.com>2022-02-23 00:23:51 (GMT)
commit1f455361ecfb1892e375bdbee813cdf095b6cfb8 (patch)
treed7def4d5d167965a45c4b0e30bb5a1a0bb5c3b4a /Python/ast_unparse.c
parentcff4d5c5d29528299ec1ac5b3b3a6f7735577c01 (diff)
downloadcpython-1f455361ecfb1892e375bdbee813cdf095b6cfb8.zip
cpython-1f455361ecfb1892e375bdbee813cdf095b6cfb8.tar.gz
cpython-1f455361ecfb1892e375bdbee813cdf095b6cfb8.tar.bz2
bpo-46765: Replace Locally Cached Strings with Statically Initialized Objects (gh-31366)
https://bugs.python.org/issue46765
Diffstat (limited to 'Python/ast_unparse.c')
-rw-r--r--Python/ast_unparse.c38
1 files changed, 9 insertions, 29 deletions
diff --git a/Python/ast_unparse.c b/Python/ast_unparse.c
index 126e904..f148e99 100644
--- a/Python/ast_unparse.c
+++ b/Python/ast_unparse.c
@@ -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;