Skip to content

[3.13] gh-125268: Use static string for "1e309" in AST (GH-125272) #125280

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 4 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
9,541 changes: 4,776 additions & 4,765 deletions Doc/data/python3.13.abi

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Include/internal/pycore_global_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ struct _Py_interp_cached_objects {
PyObject *interned_strings;

/* AST */
PyObject *str_replace_inf;
PyObject *_unused_str_replace_inf; // kept in 3.13 for ABI compatibility

/* object.__reduce__ */
PyObject *objreduce;
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_global_objects_fini_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct _Py_global_strings {
STRUCT_FOR_STR(json_decoder, "json.decoder")
STRUCT_FOR_STR(kwdefaults, ".kwdefaults")
STRUCT_FOR_STR(list_err, "list index out of range")
STRUCT_FOR_STR(str_replace_inf, "1e309")
STRUCT_FOR_STR(type_params, ".type_params")
STRUCT_FOR_STR(utf_8, "utf-8")
} literals;
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_runtime_init_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Include/internal/pycore_unicodeobject_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Parser/asdl_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -1740,8 +1740,6 @@ def generate_ast_fini(module_state, f):
for s in module_state:
f.write(" Py_CLEAR(state->" + s + ');\n')
f.write(textwrap.dedent("""
Py_CLEAR(_Py_INTERP_CACHED_OBJECT(interp, str_replace_inf));

state->finalized = 1;
state->once = (_PyOnceFlag){0};
}
Expand Down
2 changes: 0 additions & 2 deletions Python/Python-ast.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 4 additions & 25 deletions Python/ast_unparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "pycore_ast.h" // expr_ty
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_runtime.h" // _Py_ID()
#include <float.h> // DBL_MAX_10_EXP
#include <stdbool.h>

/* This limited unparser is used to convert annotations back to strings
Expand All @@ -13,10 +12,6 @@
_Py_DECLARE_STR(dbl_open_br, "{{");
_Py_DECLARE_STR(dbl_close_br, "}}");

/* We would statically initialize this if doing so were simple enough. */
#define _str_replace_inf(interp) \
_Py_INTERP_CACHED_OBJECT(interp, str_replace_inf)

/* Forward declarations for recursion via helper functions. */
static PyObject *
expr_as_unicode(expr_ty e, int level);
Expand Down Expand Up @@ -78,13 +73,13 @@ append_repr(_PyUnicodeWriter *writer, PyObject *obj)
}

if ((PyFloat_CheckExact(obj) && Py_IS_INFINITY(PyFloat_AS_DOUBLE(obj))) ||
PyComplex_CheckExact(obj))
PyComplex_CheckExact(obj))
{
PyInterpreterState *interp = _PyInterpreterState_GET();
_Py_DECLARE_STR(str_replace_inf, "1e309"); // evaluates to inf
PyObject *new_repr = PyUnicode_Replace(
repr,
&_Py_ID(inf),
_str_replace_inf(interp),
&_Py_STR(str_replace_inf),
-1
);
Py_DECREF(repr);
Expand Down Expand Up @@ -918,30 +913,14 @@ append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level)
return -1;
}

static int
maybe_init_static_strings(void)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
if (_str_replace_inf(interp) == NULL) {
PyObject *tmp = PyUnicode_FromFormat("1e%d", 1 + DBL_MAX_10_EXP);
if (tmp == NULL) {
return -1;
}
_str_replace_inf(interp) = tmp;
}
return 0;
}

static PyObject *
expr_as_unicode(expr_ty e, int level)
{
_PyUnicodeWriter writer;
_PyUnicodeWriter_Init(&writer);
writer.min_length = 256;
writer.overallocate = 1;
if (-1 == maybe_init_static_strings() ||
-1 == append_ast_expr(&writer, e, level))
{
if (-1 == append_ast_expr(&writer, e, level)) {
_PyUnicodeWriter_Dealloc(&writer);
return NULL;
}
Expand Down
Loading