[Python-checkins] gh-98417: Store int_max_str_digits on the Interpreter State (GH-98418)

miss-islington webhook-mailer at python.org
Wed Oct 19 16:27:51 EDT 2022


https://github.com/python/cpython/commit/9c8dde0fa5309ae9f83a4faa07f062fcd84df4cf
commit: 9c8dde0fa5309ae9f83a4faa07f062fcd84df4cf
branch: main
author: Eric Snow <ericsnowcurrently at gmail.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-10-19T13:27:46-07:00
summary:

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

files:
M Include/internal/pycore_interp.h
M Objects/longobject.c
M Python/pylifecycle.c
M Python/sysmodule.c

diff --git a/Include/internal/pycore_interp.h b/Include/internal/pycore_interp.h
index c11e897305d4..e643c7e9e4ed 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 8b13df4181ec..304fabfad744 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 c550f13bc148..4195a9dbca81 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 2c66415ee3d3..708145651812 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(



More information about the Python-checkins mailing list