gh-98417: Store int_max_str_digits on the Interpreter State (GH-98418)

This commit is contained in:
Eric Snow 2022-10-19 14:27:46 -06:00 committed by GitHub
parent 52fcba6512
commit 9c8dde0fa5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 5 deletions

View File

@ -69,6 +69,11 @@ struct atexit_state {
}; };
struct _Py_long_state {
int max_str_digits;
};
/* interpreter state */ /* interpreter state */
/* PyInterpreterState holds the global state for one of the runtime's /* PyInterpreterState holds the global state for one of the runtime's
@ -164,6 +169,7 @@ struct _is {
struct _Py_unicode_state unicode; struct _Py_unicode_state unicode;
struct _Py_float_state float_state; 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 /* Using a cache is very effective since typically only a single slice is
created and then deleted again. */ created and then deleted again. */
PySliceObject *slice_cache; PySliceObject *slice_cache;

View File

@ -1767,7 +1767,7 @@ long_to_decimal_string_internal(PyObject *aa,
if (size_a >= 10 * _PY_LONG_MAX_STR_DIGITS_THRESHOLD if (size_a >= 10 * _PY_LONG_MAX_STR_DIGITS_THRESHOLD
/ (3 * PyLong_SHIFT) + 2) { / (3 * PyLong_SHIFT) + 2) {
PyInterpreterState *interp = _PyInterpreterState_GET(); 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) && if ((max_str_digits > 0) &&
(max_str_digits / (3 * PyLong_SHIFT) <= (size_a - 11) / 10)) { (max_str_digits / (3 * PyLong_SHIFT) <= (size_a - 11) / 10)) {
PyErr_Format(PyExc_ValueError, _MAX_STR_DIGITS_ERROR_FMT_TO_STR, 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) { if (strlen > _PY_LONG_MAX_STR_DIGITS_THRESHOLD) {
PyInterpreterState *interp = _PyInterpreterState_GET(); 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; Py_ssize_t strlen_nosign = strlen - negative;
if ((max_str_digits > 0) && (strlen_nosign > max_str_digits)) { if ((max_str_digits > 0) && (strlen_nosign > max_str_digits)) {
Py_DECREF(scratch); Py_DECREF(scratch);
@ -2578,7 +2578,7 @@ long_from_string_base(const char **str, int base, PyLongObject **res)
* quadratic algorithm. */ * quadratic algorithm. */
if (digits > _PY_LONG_MAX_STR_DIGITS_THRESHOLD) { if (digits > _PY_LONG_MAX_STR_DIGITS_THRESHOLD) {
PyInterpreterState *interp = _PyInterpreterState_GET(); 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)) { if ((max_str_digits > 0) && (digits > max_str_digits)) {
PyErr_Format(PyExc_ValueError, _MAX_STR_DIGITS_ERROR_FMT_TO_INT, PyErr_Format(PyExc_ValueError, _MAX_STR_DIGITS_ERROR_FMT_TO_INT,
max_str_digits, digits); max_str_digits, digits);

View File

@ -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 // Update the sys module for the new configuration
if (_PySys_UpdateConfig(tstate) < 0) { if (_PySys_UpdateConfig(tstate) < 0) {
return -1; return -1;

View File

@ -1717,7 +1717,7 @@ sys_get_int_max_str_digits_impl(PyObject *module)
/*[clinic end generated code: output=0042f5e8ae0e8631 input=8dab13e2023e60d5]*/ /*[clinic end generated code: output=0042f5e8ae0e8631 input=8dab13e2023e60d5]*/
{ {
PyInterpreterState *interp = _PyInterpreterState_GET(); PyInterpreterState *interp = _PyInterpreterState_GET();
return PyLong_FromLong(interp->config.int_max_str_digits); return PyLong_FromLong(interp->long_state.max_str_digits);
} }
/*[clinic input] /*[clinic input]
@ -1734,7 +1734,7 @@ sys_set_int_max_str_digits_impl(PyObject *module, int maxdigits)
{ {
PyThreadState *tstate = _PyThreadState_GET(); PyThreadState *tstate = _PyThreadState_GET();
if ((!maxdigits) || (maxdigits >= _PY_LONG_MAX_STR_DIGITS_THRESHOLD)) { 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; Py_RETURN_NONE;
} else { } else {
PyErr_Format( PyErr_Format(