FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-100227: Move _str_replace_inf to PyInterpreterState by ericsnowcurrently · Pull Request #102333 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) .h  (1) .py  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
7 changes: 4 additions & 3 deletions Include/internal/pycore_global_objects.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ extern "C" {
_PyRuntime.cached_objects.NAME

struct _Py_cached_objects {
PyObject *str_replace_inf;

PyObject *interned_strings;
};

Expand Down Expand Up @@ -67,11 +65,14 @@ struct _Py_static_objects {
(interp)->cached_objects.NAME

struct _Py_interp_cached_objects {
int _not_set;
/* AST */
PyObject *str_replace_inf;

/* object.__reduce__ */
PyObject *objreduce;
PyObject *type_slots_pname;
pytype_slotdef *type_slots_ptrs[MAX_EQUIV];

};

#define _Py_INTERP_STATIC_OBJECT(interp, NAME) \
Expand Down
4 changes: 1 addition & 3 deletions Parser/asdl_c.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1484,9 +1484,7 @@ def generate_ast_fini(module_state, f):
for s in module_state:
f.write(" Py_CLEAR(state->" + s + ');\n')
f.write(textwrap.dedent("""
if (_PyInterpreterState_Get() == _PyInterpreterState_Main()) {
Py_CLEAR(_Py_CACHED_OBJECT(str_replace_inf));
}
Py_CLEAR(_Py_INTERP_CACHED_OBJECT(interp, str_replace_inf));

#if !defined(NDEBUG)
state->initialized = -1;
Expand Down
4 changes: 1 addition & 3 deletions Python/Python-ast.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 14 additions & 5 deletions Python/ast_unparse.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -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>
Expand All @@ -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 *
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down

Back | FazBrowse Home | New Git URL