11#include "Python.h"
22#include "pycore_ast.h" // expr_ty
3+ #include "pycore_pystate.h" // _PyInterpreterState_GET()
34#include "pycore_runtime.h" // _Py_ID()
45#include <float.h> // DBL_MAX_10_EXP
56#include <stdbool.h>
@@ -13,7 +14,10 @@ _Py_DECLARE_STR(open_br, "{");
1314_Py_DECLARE_STR (dbl_open_br , "{{" );
1415_Py_DECLARE_STR (close_br , "}" );
1516_Py_DECLARE_STR (dbl_close_br , "}}" );
16- #define _str_replace_inf _Py_CACHED_OBJECT(str_replace_inf)
17+
18+ /* We would statically initialize this if doing so were simple enough. */
19+ #define _str_replace_inf (interp ) \
20+ _Py_INTERP_CACHED_OBJECT(interp, str_replace_inf)
1721
1822/* Forward declarations for recursion via helper functions. */
1923static PyObject *
@@ -78,10 +82,11 @@ append_repr(_PyUnicodeWriter *writer, PyObject *obj)
7882 if ((PyFloat_CheckExact (obj ) && Py_IS_INFINITY (PyFloat_AS_DOUBLE (obj ))) ||
7983 PyComplex_CheckExact (obj ))
8084 {
85+ PyInterpreterState * interp = _PyInterpreterState_GET ();
8186 PyObject * new_repr = PyUnicode_Replace (
8287 repr ,
8388 & _Py_ID (inf ),
84- _str_replace_inf ,
89+ _str_replace_inf ( interp ) ,
8590 -1
8691 );
8792 Py_DECREF (repr );
@@ -916,9 +921,13 @@ append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level)
916921static int
917922maybe_init_static_strings (void )
918923{
919- if (!_str_replace_inf &&
920- !(_str_replace_inf = PyUnicode_FromFormat ("1e%d" , 1 + DBL_MAX_10_EXP ))) {
921- return -1 ;
924+ PyInterpreterState * interp = _PyInterpreterState_GET ();
925+ if (_str_replace_inf (interp ) == NULL ) {
926+ PyObject * tmp = PyUnicode_FromFormat ("1e%d" , 1 + DBL_MAX_10_EXP );
927+ if (tmp == NULL ) {
928+ return -1 ;
929+ }
930+ _str_replace_inf (interp ) = tmp ;
922931 }
923932 return 0 ;
924933}
0 commit comments