From 9c8dde0fa5309ae9f83a4faa07f062fcd84df4cf Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Wed, 19 Oct 2022 14:27:46 -0600 Subject: [PATCH] gh-98417: Store int_max_str_digits on the Interpreter State (GH-98418) --- Include/internal/pycore_interp.h | 6 ++++++ Objects/longobject.c | 6 +++--- Python/pylifecycle.c | 2 ++ Python/sysmodule.c | 4 ++-- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Include/internal/pycore_interp.h b/Include/internal/pycore_interp.h index c11e897305d..e643c7e9e4e 100644 --- a/Include/internal/pycore_interp.h +++ b/Include/internal/pycore_interp.h @@ -69,6 +69,11 @@ struct atexit_state { }; +struct _Py_long_state { + int max_str_digits; +}; + + /* interpreter state */ /* PyInterpreterState holds the global state for one of the runtime's @@ -164,6 +169,7 @@ struct _is { struct _Py_unicode_state unicode; struct _Py_float_state float_state; + struct _Py_long_state long_state; /* Using a cache is very effective since typically only a single slice is created and then deleted again. */ PySliceObject *slice_cache; diff --git a/Objects/longobject.c b/Objects/longobject.c index 8b13df4181e..304fabfad74 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -1767,7 +1767,7 @@ long_to_decimal_string_internal(PyObject *aa, if (size_a >= 10 * _PY_LONG_MAX_STR_DIGITS_THRESHOLD / (3 * PyLong_SHIFT) + 2) { PyInterpreterState *interp = _PyInterpreterState_GET(); - int max_str_digits = interp->config.int_max_str_digits; + int max_str_digits = interp->long_state.max_str_digits; if ((max_str_digits > 0) && (max_str_digits / (3 * PyLong_SHIFT) <= (size_a - 11) / 10)) { PyErr_Format(PyExc_ValueError, _MAX_STR_DIGITS_ERROR_FMT_TO_STR, @@ -1837,7 +1837,7 @@ long_to_decimal_string_internal(PyObject *aa, } if (strlen > _PY_LONG_MAX_STR_DIGITS_THRESHOLD) { PyInterpreterState *interp = _PyInterpreterState_GET(); - int max_str_digits = interp->config.int_max_str_digits; + int max_str_digits = interp->long_state.max_str_digits; Py_ssize_t strlen_nosign = strlen - negative; if ((max_str_digits > 0) && (strlen_nosign > max_str_digits)) { Py_DECREF(scratch); @@ -2578,7 +2578,7 @@ long_from_string_base(const char **str, int base, PyLongObject **res) * quadratic algorithm. */ if (digits > _PY_LONG_MAX_STR_DIGITS_THRESHOLD) { PyInterpreterState *interp = _PyInterpreterState_GET(); - int max_str_digits = interp->config.int_max_str_digits; + int max_str_digits = interp->long_state.max_str_digits; if ((max_str_digits > 0) && (digits > max_str_digits)) { PyErr_Format(PyExc_ValueError, _MAX_STR_DIGITS_ERROR_FMT_TO_INT, max_str_digits, digits); diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index c550f13bc14..4195a9dbca8 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -480,6 +480,8 @@ interpreter_update_config(PyThreadState *tstate, int only_update_path_config) } } + tstate->interp->long_state.max_str_digits = config->int_max_str_digits; + // Update the sys module for the new configuration if (_PySys_UpdateConfig(tstate) < 0) { return -1; diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 2c66415ee3d..70814565181 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1717,7 +1717,7 @@ sys_get_int_max_str_digits_impl(PyObject *module) /*[clinic end generated code: output=0042f5e8ae0e8631 input=8dab13e2023e60d5]*/ { PyInterpreterState *interp = _PyInterpreterState_GET(); - return PyLong_FromLong(interp->config.int_max_str_digits); + return PyLong_FromLong(interp->long_state.max_str_digits); } /*[clinic input] @@ -1734,7 +1734,7 @@ sys_set_int_max_str_digits_impl(PyObject *module, int maxdigits) { PyThreadState *tstate = _PyThreadState_GET(); if ((!maxdigits) || (maxdigits >= _PY_LONG_MAX_STR_DIGITS_THRESHOLD)) { - tstate->interp->config.int_max_str_digits = maxdigits; + tstate->interp->long_state.max_str_digits = maxdigits; Py_RETURN_NONE; } else { PyErr_Format(