struct _Py_interp_cached_objects {
PyObject *interned_strings;
- /* AST */
- PyObject *str_replace_inf;
-
/* object.__reduce__ */
PyObject *objreduce;
PyObject *type_slots_pname;
_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));
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;
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"), \
}
_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));
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};
}
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};
}
#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);
}
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);
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)
{
_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;
}