Skip to content

Commit 9c8dde0

Browse files
gh-98417: Store int_max_str_digits on the Interpreter State (GH-98418)
1 parent 52fcba6 commit 9c8dde0

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

Diff for: Include/internal/pycore_interp.h

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ struct atexit_state {
6969
};
7070

7171

72+
struct _Py_long_state {
73+
int max_str_digits;
74+
};
75+
76+
7277
/* interpreter state */
7378

7479
/* PyInterpreterState holds the global state for one of the runtime's
@@ -164,6 +169,7 @@ struct _is {
164169

165170
struct _Py_unicode_state unicode;
166171
struct _Py_float_state float_state;
172+
struct _Py_long_state long_state;
167173
/* Using a cache is very effective since typically only a single slice is
168174
created and then deleted again. */
169175
PySliceObject *slice_cache;

Diff for: Objects/longobject.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,7 @@ long_to_decimal_string_internal(PyObject *aa,
17671767
if (size_a >= 10 * _PY_LONG_MAX_STR_DIGITS_THRESHOLD
17681768
/ (3 * PyLong_SHIFT) + 2) {
17691769
PyInterpreterState *interp = _PyInterpreterState_GET();
1770-
int max_str_digits = interp->config.int_max_str_digits;
1770+
int max_str_digits = interp->long_state.max_str_digits;
17711771
if ((max_str_digits > 0) &&
17721772
(max_str_digits / (3 * PyLong_SHIFT) <= (size_a - 11) / 10)) {
17731773
PyErr_Format(PyExc_ValueError, _MAX_STR_DIGITS_ERROR_FMT_TO_STR,
@@ -1837,7 +1837,7 @@ long_to_decimal_string_internal(PyObject *aa,
18371837
}
18381838
if (strlen > _PY_LONG_MAX_STR_DIGITS_THRESHOLD) {
18391839
PyInterpreterState *interp = _PyInterpreterState_GET();
1840-
int max_str_digits = interp->config.int_max_str_digits;
1840+
int max_str_digits = interp->long_state.max_str_digits;
18411841
Py_ssize_t strlen_nosign = strlen - negative;
18421842
if ((max_str_digits > 0) && (strlen_nosign > max_str_digits)) {
18431843
Py_DECREF(scratch);
@@ -2578,7 +2578,7 @@ long_from_string_base(const char **str, int base, PyLongObject **res)
25782578
* quadratic algorithm. */
25792579
if (digits > _PY_LONG_MAX_STR_DIGITS_THRESHOLD) {
25802580
PyInterpreterState *interp = _PyInterpreterState_GET();
2581-
int max_str_digits = interp->config.int_max_str_digits;
2581+
int max_str_digits = interp->long_state.max_str_digits;
25822582
if ((max_str_digits > 0) && (digits > max_str_digits)) {
25832583
PyErr_Format(PyExc_ValueError, _MAX_STR_DIGITS_ERROR_FMT_TO_INT,
25842584
max_str_digits, digits);

Diff for: Python/pylifecycle.c

+2
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,8 @@ interpreter_update_config(PyThreadState *tstate, int only_update_path_config)
480480
}
481481
}
482482

483+
tstate->interp->long_state.max_str_digits = config->int_max_str_digits;
484+
483485
// Update the sys module for the new configuration
484486
if (_PySys_UpdateConfig(tstate) < 0) {
485487
return -1;

Diff for: Python/sysmodule.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1717,7 +1717,7 @@ sys_get_int_max_str_digits_impl(PyObject *module)
17171717
/*[clinic end generated code: output=0042f5e8ae0e8631 input=8dab13e2023e60d5]*/
17181718
{
17191719
PyInterpreterState *interp = _PyInterpreterState_GET();
1720-
return PyLong_FromLong(interp->config.int_max_str_digits);
1720+
return PyLong_FromLong(interp->long_state.max_str_digits);
17211721
}
17221722

17231723
/*[clinic input]
@@ -1734,7 +1734,7 @@ sys_set_int_max_str_digits_impl(PyObject *module, int maxdigits)
17341734
{
17351735
PyThreadState *tstate = _PyThreadState_GET();
17361736
if ((!maxdigits) || (maxdigits >= _PY_LONG_MAX_STR_DIGITS_THRESHOLD)) {
1737-
tstate->interp->config.int_max_str_digits = maxdigits;
1737+
tstate->interp->long_state.max_str_digits = maxdigits;
17381738
Py_RETURN_NONE;
17391739
} else {
17401740
PyErr_Format(

0 commit comments

Comments
 (0)