8000 bpo-46765: Replace Locally Cached Strings with Statically Initialized Objects by ericsnowcurrently · Pull Request #31366 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-46765: Replace Locally Cached Strings with Statically Initialized Objects #31366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use _Py_ID() in ast_unparse.c.
  • Loading branch information
ericsnowcurrently committed Feb 16, 2022
commit c7c8faf46d4970ea480095a14d5627f79f1f0a49
5 changes: 5 additions & 0 deletions Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ struct _Py_global_strings {
STRUCT_FOR_STR(anon_module, "<module>")
STRUCT_FOR_STR(anon_setcomp, "<setcomp>")
STRUCT_FOR_STR(anon_string, "<string>")
STRUCT_FOR_STR(close_br, "}")
STRUCT_FOR_STR(comma_sep, ", ")
STRUCT_FOR_STR(dbl_close_br, "}}")
STRUCT_FOR_STR(dbl_open_br, "{{")
STRUCT_FOR_STR(dbl_percent, "%%")
STRUCT_FOR_STR(dot, ".")
STRUCT_FOR_STR(dot_locals, ".<locals>")
STRUCT_FOR_STR(empty, "")
STRUCT_FOR_STR(list_err, "list index out of range")
STRUCT_FOR_STR(open_br, "{")
STRUCT_FOR_STR(percent, "%")
} literals;

Expand Down Expand Up @@ -249,6 +253,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(getattr)
STRUCT_FOR_ID(ignore)
STRUCT_FOR_ID(importlib)
STRUCT_FOR_ID(inf)
STRUCT_FOR_ID(intersection)
STRUCT_FOR_ID(isatty)
STRUCT_FOR_ID(items)
Expand Down
5 changes: 5 additions & 0 deletions Include/internal/pycore_runtime_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -651,12 +651,16 @@ extern "C" {
INIT_STR(anon_module, "<module>"), \
INIT_STR(anon_setcomp, "<setcomp>"), \
INIT_STR(anon_string, "<string>"), \
INIT_STR(close_br, "}"), \
INIT_STR(comma_sep, ", "), \
INIT_STR(dbl_close_br, "}}"), \
INIT_STR(dbl_open_br, "{{"), \
INIT_STR(dbl_percent, "%%"), \
INIT_STR(dot, "."), \
INIT_STR(dot_locals, ".<locals>"), \
INIT_STR(empty, ""), \
INIT_STR(list_err, "list index out of range"), \
INIT_STR(open_br, "{"), \
INIT_STR(percent, "%"), \
}, \
.identifiers = { \
Expand Down Expand Up @@ -864,6 +868,7 @@ extern "C" {
INIT_ID(getattr), \
INIT_ID(ignore), \
INIT_ID(importlib), \
INIT_ID(inf), \
INIT_ID(intersection), \
INIT_ID(isatty), \
INIT_ID(items), \
Expand Down
38 changes: 9 additions & 29 deletions Python/ast_unparse.c
Original file line number Diff line number Diff line change
@@ -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>

Expand All @@ -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. */
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 = "{ ";
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 0 additions & 10 deletions Tools/c-analyzer/cpython/globals-to-fix.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -281,16 +281,6 @@ Objects/sliceobject.c - _Py_EllipsisObject -

# manually cached PyUnicodeObject
# XXX This should have been found by the analyzer but wasn't:
Python/ast_unparse.c - _str_close_br -
# XXX This should have been found by the analyzer but wasn't:
Python/ast_unparse.c - _str_dbl_close_br -
# XXX This should have been found by the analyzer but wasn't:
Python/ast_unparse.c - _str_dbl_open_br -
# XXX This should have been found by the analyzer but wasn't:
Python/ast_unparse.c - _str_inf -
# XXX This should have been found by the analyzer but wasn't:
Python/ast_unparse.c - _str_open_br -
# XXX This should have been found by the analyzer but wasn't:
Python/ast_unparse.c - _str_replace_inf -
# XXX This should have been found by the analyzer but wasn't:
Python/compile.c - __annotations__ -
Expand Down
0