Skip to content

Commit

Permalink
gh-98417: Store int_max_str_digits on the Interpreter State (GH-98418)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsnowcurrently authored Oct 19, 2022
1 parent 52fcba6 commit 9c8dde0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
6 changes: 6 additions & 0 deletions Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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(
Expand Down

0 comments on commit 9c8dde0

Please sign in to comment.