Skip to content

Commit

Permalink
gh-125268: Use static string for "1e309" in AST (#125272)
Browse files Browse the repository at this point in the history
When formatting the AST as a string, infinite values are replaced by
1e309, which evaluates to infinity. The initialization of this string
replacement was not thread-safe in the free threading build.
  • Loading branch information
colesbury authored Oct 10, 2024
1 parent bb594e8 commit 427dcf2
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 32 deletions.
3 changes: 0 additions & 3 deletions Include/internal/pycore_global_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ struct _Py_static_objects {
struct _Py_interp_cached_objects {
PyObject *interned_strings;

/* AST */
PyObject *str_replace_inf;

/* object.__reduce__ */
PyObject *objreduce;
PyObject *type_slots_pname;
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 @@ -48,6 +48,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 @@ -2242,8 +2242,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) && isinf(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

0 comments on commit 427dcf2

Please sign in to comment.