summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorSam Gross <colesbury@gmail.com>2024-10-10 20:21:29 (GMT)
committerGitHub <noreply@github.com>2024-10-10 20:21:29 (GMT)
commit427dcf24de4e06d239745d74d08c4b2e541dca5a (patch)
tree37a7ca3937862498b957402323c1a29195ce11eb /Python
parentbb594e801b6a84823badbb85b88f0fc8b221d7bf (diff)
downloadcpython-427dcf24de4e06d239745d74d08c4b2e541dca5a.zip
cpython-427dcf24de4e06d239745d74d08c4b2e541dca5a.tar.gz
cpython-427dcf24de4e06d239745d74d08c4b2e541dca5a.tar.bz2
gh-125268: Use static string for "1e309" in AST (#125272)
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.
Diffstat (limited to 'Python')
-rw-r--r--Python/Python-ast.c2
-rw-r--r--Python/ast_unparse.c29
2 files changed, 4 insertions, 27 deletions
diff --git a/Python/Python-ast.c b/Python/Python-ast.c
index 89c52b9..38d74b4 100644
--- a/Python/Python-ast.c
+++ b/Python/Python-ast.c
@@ -281,8 +281,6 @@ void _PyAST_Fini(PyInterpreterState *interp)
Py_CLEAR(state->vararg);
Py_CLEAR(state->withitem_type);
- Py_CLEAR(_Py_INTERP_CACHED_OBJECT(interp, str_replace_inf));
-
state->finalized = 1;
state->once = (_PyOnceFlag){0};
}
diff --git a/Python/ast_unparse.c b/Python/ast_unparse.c
index 86f7a58..8017cfc 100644
--- a/Python/ast_unparse.c
+++ b/Python/ast_unparse.c
@@ -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
@@ -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);
@@ -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);
@@ -918,20 +913,6 @@ 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)
{
@@ -939,9 +920,7 @@ expr_as_unicode(expr_ty e, int level)
_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;
}