diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2023-02-28 21:16:39 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-28 21:16:39 (GMT) |
commit | 880437d4ec65ef35d505eeaff9dad5c6654dbc1a (patch) | |
tree | 1dee8a4dcc709b6b9f99d025cddbe3fe14ceadd5 /Python/ast_unparse.c | |
parent | f300a1fa4c121f7807cbda4fc8bb26240c69ea74 (diff) | |
download | cpython-880437d4ec65ef35d505eeaff9dad5c6654dbc1a.zip cpython-880437d4ec65ef35d505eeaff9dad5c6654dbc1a.tar.gz cpython-880437d4ec65ef35d505eeaff9dad5c6654dbc1a.tar.bz2 |
gh-100227: Move _str_replace_inf to PyInterpreterState (gh-102333)
https://github.com/python/cpython/issues/100227
Diffstat (limited to 'Python/ast_unparse.c')
-rw-r--r-- | Python/ast_unparse.c | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/Python/ast_unparse.c b/Python/ast_unparse.c index 79b2e2f..8aff045 100644 --- a/Python/ast_unparse.c +++ b/Python/ast_unparse.c @@ -1,5 +1,6 @@ #include "Python.h" #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> @@ -13,7 +14,10 @@ _Py_DECLARE_STR(open_br, "{"); _Py_DECLARE_STR(dbl_open_br, "{{"); _Py_DECLARE_STR(close_br, "}"); _Py_DECLARE_STR(dbl_close_br, "}}"); -#define _str_replace_inf _Py_CACHED_OBJECT(str_replace_inf) + +/* 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 * @@ -78,10 +82,11 @@ append_repr(_PyUnicodeWriter *writer, PyObject *obj) if ((PyFloat_CheckExact(obj) && Py_IS_INFINITY(PyFloat_AS_DOUBLE(obj))) || PyComplex_CheckExact(obj)) { + PyInterpreterState *interp = _PyInterpreterState_GET(); PyObject *new_repr = PyUnicode_Replace( repr, &_Py_ID(inf), - _str_replace_inf, + _str_replace_inf(interp), -1 ); Py_DECREF(repr); @@ -916,9 +921,13 @@ append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level) static int maybe_init_static_strings(void) { - if (!_str_replace_inf && - !(_str_replace_inf = PyUnicode_FromFormat("1e%d", 1 + DBL_MAX_10_EXP))) { - return -1; + 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; } |