Visitar URL original
gh-96512: Modernize int_max_str_digits protection in 3.12 by gpshead · Pull Request #96568 · python/cpython · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Revert "Undo addition to PyConfig to ease backporting."
This reverts commit 1ad88f5.
  • Loading branch information
gpshead committed Sep 4, 2022
commit 25aa726d894645646b36c60d5e14de4efe65576b
12 changes: 12 additions & 0 deletions Doc/c-api/init_config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,18 @@ PyConfig

Default: ``0``.

.. c:member:: int32_t int_max_str_digits

If greater than 0, enable int conversion digit limitations. ``-1`` means
Comment thread
gpshead marked this conversation as resolved.
that :data:`sys.int_info.default_max_str_digits` will be used.

Configured by the :option:`-X int_max_str_digits <-X>` command line
flag or the :envvar:`PYTHONINTMAXSTRDIGITS` environment varable.

Comment thread
gpshead marked this conversation as resolved.
Default: ``-1``.

.. versionadded:: 3.12

.. c:member:: int isolated

If greater than ``0``, enable isolated mode:
Expand Down
1 change: 1 addition & 0 deletions Include/cpython/initconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ typedef struct PyConfig {
wchar_t *check_hash_pycs_mode;
int use_frozen_modules;
int safe_path;
int32_t int_max_str_digits;

/* --- Path configuration inputs ------------ */
int pathconfig_warnings;
Expand Down
2 changes: 0 additions & 2 deletions Include/internal/pycore_initconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ extern void _Py_DumpPathConfig(PyThreadState *tstate);

PyAPI_FUNC(PyObject*) _Py_Get_Getpath_CodeObject(void);

extern int _Py_global_config_int_max_str_digits; // TODO(gpshead): move this into PyConfig in 3.12 after the backports ship.


/* --- Function used for testing ---------------------------------- */

Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
'install_signal_handlers': 1,
'use_hash_seed': 0,
'hash_seed': 0,
'int_max_str_digits': -1,
'faulthandler': 0,
'tracemalloc': 0,
'perf_profiling': 0,
Expand Down
2 changes: 1 addition & 1 deletion Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6196,7 +6196,7 @@ _PyLong_InitTypes(PyInterpreterState *interp)
return _PyStatus_ERR("can't init int info type");
}
}
interp->int_max_str_digits = _Py_global_config_int_max_str_digits;
interp->int_max_str_digits = _PyInterpreterState_GetConfig(interp)->int_max_str_digits;
if (interp->int_max_str_digits == -1) {
interp->int_max_str_digits = _PY_LONG_DEFAULT_MAX_STR_DIGITS;
}
Expand Down
17 changes: 10 additions & 7 deletions Python/initconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -789,14 +789,12 @@ _PyConfig_InitCompatConfig(PyConfig *config)
config->use_frozen_modules = 1;
#endif
config->safe_path = 0;
/* config_init_int_max_str_digits() sets default limit */
Comment thread
gpshead marked this conversation as resolved.
config->int_max_str_digits = -1;
config->_is_python_build = 0;
config->code_debug_ranges = 1;
}

/* Excluded from public struct PyConfig for backporting reasons. */
/* default to unconfigured, _PyLong_InitTypes() does the rest */
int _Py_global_config_int_max_str_digits = -1;


static void
config_init_defaults(PyConfig *config)
Expand Down Expand Up @@ -1021,6 +1019,7 @@ _PyConfig_Copy(PyConfig *config, const PyConfig *config2)
COPY_ATTR(safe_path);
COPY_WSTRLIST(orig_argv);
COPY_ATTR(_is_python_build);
COPY_ATTR(int_max_str_digits);

#undef COPY_ATTR
#undef COPY_WSTR_ATTR
Expand Down Expand Up @@ -1128,6 +1127,7 @@ _PyConfig_AsDict(const PyConfig *config)
SET_ITEM_INT(use_frozen_modules);
SET_ITEM_INT(safe_path);
SET_ITEM_INT(_is_python_build);
SET_ITEM_INT(int_max_str_digits);
Comment thread
gpshead marked this conversation as resolved.

return dict;

Expand Down Expand Up @@ -1781,6 +1781,9 @@ config_init_int_max_str_digits(PyConfig *config)
int maxdigits;
int valid = 0;

/* default to unconfigured, _PyLong_InitTypes() does the rest */
config->int_max_str_digits = -1;
Comment thread
gpshead marked this conversation as resolved.

const char *env = config_get_env(config, "PYTHONINTMAXSTRDIGITS");
if (env) {
if (!_Py_str_to_int(env, &maxdigits)) {
Expand All @@ -1794,7 +1797,7 @@ config_init_int_max_str_digits(PyConfig *config)
STRINGIFY(_PY_LONG_MAX_STR_DIGITS_THRESHOLD)
" or 0 for unlimited.");
}
_Py_global_config_int_max_str_digits = maxdigits;
config->int_max_str_digits = maxdigits;
}

const wchar_t *xoption = config_get_xoption(config, L"int_max_str_digits");
Expand All @@ -1813,7 +1816,7 @@ config_init_int_max_str_digits(PyConfig *config)
#undef _STRINGIFY
#undef STRINGIFY
}
_Py_global_config_int_max_str_digits = maxdigits;
config->int_max_str_digits = maxdigits;
}
Comment thread
gpshead marked this conversation as resolved.
return _PyStatus_OK();
}
Expand Down Expand Up @@ -1881,7 +1884,7 @@ config_read_complex_options(PyConfig *config)
}
}

if (_Py_global_config_int_max_str_digits < 0) {
if (config->int_max_str_digits < 0) {
status = config_init_int_max_str_digits(config);
if (_PyStatus_EXCEPTION(status)) {
return status;
Expand Down
2 changes: 1 addition & 1 deletion Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2777,7 +2777,7 @@ set_flags_from_config(PyInterpreterState *interp, PyObject *flags)
SetFlag(preconfig->utf8_mode);
SetFlag(config->warn_default_encoding);
SetFlagObj(PyBool_FromLong(config->safe_path));
SetFlag(_Py_global_config_int_max_str_digits);
SetFlag(config->int_max_str_digits);
#undef SetFlagObj
#undef SetFlag
return 0;
Expand Down