Skip to content

Commit 880437d

Browse files
gh-100227: Move _str_replace_inf to PyInterpreterState (gh-102333)
#100227
1 parent f300a1f commit 880437d

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

Include/internal/pycore_global_objects.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ extern "C" {
2727
_PyRuntime.cached_objects.NAME
2828

2929
struct _Py_cached_objects {
30-
PyObject *str_replace_inf;
31-
3230
PyObject *interned_strings;
3331
};
3432

@@ -67,11 +65,14 @@ struct _Py_static_objects {
6765
(interp)->cached_objects.NAME
6866

6967
struct _Py_interp_cached_objects {
70-
int _not_set;
68+
/* AST */
69+
PyObject *str_replace_inf;
70+
7171
/* object.__reduce__ */
7272
PyObject *objreduce;
7373
PyObject *type_slots_pname;
7474
pytype_slotdef *type_slots_ptrs[MAX_EQUIV];
75+
7576
};
7677

7778
#define _Py_INTERP_STATIC_OBJECT(interp, NAME) \

Parser/asdl_c.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1484,9 +1484,7 @@ def generate_ast_fini(module_state, f):
14841484
for s in module_state:
14851485
f.write(" Py_CLEAR(state->" + s + ');\n')
14861486
f.write(textwrap.dedent("""
1487-
if (_PyInterpreterState_Get() == _PyInterpreterState_Main()) {
1488-
Py_CLEAR(_Py_CACHED_OBJECT(str_replace_inf));
1489-
}
1487+
Py_CLEAR(_Py_INTERP_CACHED_OBJECT(interp, str_replace_inf));
14901488
14911489
#if !defined(NDEBUG)
14921490
state->initialized = -1;

Python/Python-ast.c

+1-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/ast_unparse.c

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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. */
1923
static 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)
916921
static int
917922
maybe_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

Comments
 (0)