]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-125268: Use static string for "1e309" in AST (#125272)
authorSam Gross <colesbury@gmail.com>
Thu, 10 Oct 2024 20:21:29 +0000 (16:21 -0400)
committerGitHub <noreply@github.com>
Thu, 10 Oct 2024 20:21:29 +0000 (16:21 -0400)
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.

Include/internal/pycore_global_objects.h
Include/internal/pycore_global_objects_fini_generated.h
Include/internal/pycore_global_strings.h
Include/internal/pycore_runtime_init_generated.h
Include/internal/pycore_unicodeobject_generated.h
Parser/asdl_c.py
Python/Python-ast.c
Python/ast_unparse.c

index 913dce6f1ec0fe37d0f5f4aa9a444db87dfed46f..e3f7ac707f0c37ef51094c5d1e48c9978fee4f14 100644 (file)
@@ -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;
index de68ef9325723482a7f52ee705ed5461d9d0c44a..2fd7d5d13a98b29b29fd1b6f98c886be7f2f3466 100644 (file)
@@ -562,6 +562,7 @@ _PyStaticObjects_CheckRefcnt(PyInterpreterState *interp) {
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_STR(json_decoder));
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_STR(kwdefaults));
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_STR(list_err));
+    _PyStaticObject_CheckRefcnt((PyObject *)&_Py_STR(str_replace_inf));
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_STR(type_params));
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_STR(utf_8));
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(CANCELLED));
index 1591cb0a3f114f1821389fc0e6e842ee1b212fbc..fc3871570cc49d96b3559da077325b3d153e54a1 100644 (file)
@@ -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;
index c9d20d0b5aacdbfca8ed137fb62c670bef59bd91..3b80e265b0ca50a67b66ff10f19d1194e0b80225 100644 (file)
@@ -557,6 +557,7 @@ extern "C" {
     INIT_STR(json_decoder, "json.decoder"), \
     INIT_STR(kwdefaults, ".kwdefaults"), \
     INIT_STR(list_err, "list index out of range"), \
+    INIT_STR(str_replace_inf, "1e309"), \
     INIT_STR(type_params, ".type_params"), \
     INIT_STR(utf_8, "utf-8"), \
 }
index d335373e88ee74d141907f7332cb8b5c6fd41ebc..eb2eca06ec4d4f6613a8aec41788ee3444cae6a6 100644 (file)
@@ -2936,6 +2936,10 @@ _PyUnicode_InitStaticStrings(PyInterpreterState *interp) {
     _PyUnicode_InternStatic(interp, &string);
     assert(_PyUnicode_CheckConsistency(string, 1));
     assert(PyUnicode_GET_LENGTH(string) != 1);
+    string = &_Py_STR(str_replace_inf);
+    _PyUnicode_InternStatic(interp, &string);
+    assert(_PyUnicode_CheckConsistency(string, 1));
+    assert(PyUnicode_GET_LENGTH(string) != 1);
     string = &_Py_STR(anon_null);
     _PyUnicode_InternStatic(interp, &string);
     assert(_PyUnicode_CheckConsistency(string, 1));
index f50c28afcfe2055a838c7f78f1d0e20f5ea5702a..32eac3afafa5d5ca239f474fc02304a3e0bb89b7 100755 (executable)
@@ -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};
             }
index 89c52b9dc73cacb47526f8817be38910b84af493..38d74b48d232f8bae9207e4340420becd895f9e7 100644 (file)
@@ -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};
 }
index 86f7a582b981a3093385463215b140c35175d9d0..8017cfc7fcf268940c451dd9cc96c1102d1b4ac1 100644 (file)
@@ -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
 _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;
     }