aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/src/Python/initconfig.c
diff options
context:
space:
mode:
authorshadchin <shadchin@yandex-team.com>2024-02-12 07:53:52 +0300
committershadchin <shadchin@yandex-team.com>2024-02-12 08:07:36 +0300
commitce1b7ca3171f9158180640c6a02a74b4afffedea (patch)
treee47c1e8391b1b0128262c1e9b1e6ed4c8fff2348 /contrib/tools/python3/src/Python/initconfig.c
parent57350d96f030db90f220ce50ee591d5c5d403df7 (diff)
downloadydb-ce1b7ca3171f9158180640c6a02a74b4afffedea.tar.gz
Update Python from 3.11.8 to 3.12.2
Diffstat (limited to 'contrib/tools/python3/src/Python/initconfig.c')
-rw-r--r--contrib/tools/python3/src/Python/initconfig.c136
1 files changed, 105 insertions, 31 deletions
diff --git a/contrib/tools/python3/src/Python/initconfig.c b/contrib/tools/python3/src/Python/initconfig.c
index 7332879446..a1256a542a 100644
--- a/contrib/tools/python3/src/Python/initconfig.c
+++ b/contrib/tools/python3/src/Python/initconfig.c
@@ -5,7 +5,7 @@
#include "pycore_interp.h" // _PyInterpreterState.runtime
#include "pycore_long.h" // _PY_LONG_MAX_STR_DIGITS_THRESHOLD
#include "pycore_pathconfig.h" // _Py_path_config
-#include "pycore_pyerrors.h" // _PyErr_Fetch()
+#include "pycore_pyerrors.h" // _PyErr_GetRaisedException()
#include "pycore_pylifecycle.h" // _Py_PreInitializeFromConfig()
#include "pycore_pymem.h" // _PyMem_SetDefaultAllocator()
#include "pycore_pystate.h" // _PyThreadState_GET()
@@ -119,12 +119,24 @@ The following implementation-specific options are available:\n\
files are desired as well as suppressing the extra visual location indicators \n\
when the interpreter displays tracebacks.\n\
\n\
+-X perf: activate support for the Linux \"perf\" profiler by activating the \"perf\"\n\
+ trampoline. When this option is activated, the Linux \"perf\" profiler will be \n\
+ able to report Python calls. This option is only available on some platforms and will \n\
+ do nothing if is not supported on the current system. The default value is \"off\".\n\
+\n\
-X frozen_modules=[on|off]: whether or not frozen modules should be used.\n\
The default is \"on\" (or \"off\" if you are running a local build).\n\
\n\
-X int_max_str_digits=number: limit the size of int<->str conversions.\n\
This helps avoid denial of service attacks when parsing untrusted data.\n\
- The default is sys.int_info.default_max_str_digits. 0 disables.";
+ The default is sys.int_info.default_max_str_digits. 0 disables."
+
+#ifdef Py_STATS
+"\n\
+\n\
+-X pystats: Enable pystats collection at startup."
+#endif
+;
/* Envvars that don't have equivalent command-line options are listed first */
static const char usage_envvars[] =
@@ -191,7 +203,7 @@ int Py_UTF8Mode = 0;
int Py_DebugFlag = 0; /* Needed by parser.c */
int Py_VerboseFlag = 0; /* Needed by import.c */
int Py_QuietFlag = 0; /* Needed by sysmodule.c */
-int Py_InteractiveFlag = 0; /* Needed by Py_FdIsInteractive() below */
+int Py_InteractiveFlag = 0; /* Previously, was used by Py_FdIsInteractive() */
int Py_InspectFlag = 0; /* Needed to determine whether to exit at SystemExit */
int Py_OptimizeFlag = 0; /* Needed by compile.c */
int Py_NoSiteFlag = 0; /* Suppress 'import site' */
@@ -212,6 +224,8 @@ int Py_LegacyWindowsStdioFlag = 0; /* Uses FileIO instead of WindowsConsoleIO */
static PyObject *
_Py_GetGlobalVariablesAsDict(void)
{
+_Py_COMP_DIAG_PUSH
+_Py_COMP_DIAG_IGNORE_DEPR_DECLS
PyObject *dict, *obj;
dict = PyDict_New();
@@ -236,7 +250,7 @@ _Py_GetGlobalVariablesAsDict(void)
#define FROM_STRING(STR) \
((STR != NULL) ? \
PyUnicode_FromString(STR) \
- : (Py_INCREF(Py_None), Py_None))
+ : Py_NewRef(Py_None))
#define SET_ITEM_STR(VAR) \
SET_ITEM(#VAR, FROM_STRING(VAR))
@@ -278,15 +292,19 @@ fail:
#undef SET_ITEM
#undef SET_ITEM_INT
#undef SET_ITEM_STR
+_Py_COMP_DIAG_POP
}
char*
Py_GETENV(const char *name)
{
+_Py_COMP_DIAG_PUSH
+_Py_COMP_DIAG_IGNORE_DEPR_DECLS
if (Py_IgnoreEnvironmentFlag) {
return NULL;
}
return getenv(name);
+_Py_COMP_DIAG_POP
}
/* --- PyStatus ----------------------------------------------- */
@@ -548,8 +566,11 @@ Py_SetStandardStreamEncoding(const char *encoding, const char *errors)
}
#ifdef MS_WINDOWS
if (_Py_StandardStreamEncoding) {
+_Py_COMP_DIAG_PUSH
+_Py_COMP_DIAG_IGNORE_DEPR_DECLS
/* Overriding the stream encoding implies legacy streams */
Py_LegacyWindowsStdioFlag = 1;
+_Py_COMP_DIAG_POP
}
#endif
@@ -583,17 +604,13 @@ _Py_ClearStandardStreamEncoding(void)
/* --- Py_GetArgcArgv() ------------------------------------------- */
-/* For Py_GetArgcArgv(); set by _Py_SetArgcArgv() */
-static PyWideStringList orig_argv = {.length = 0, .items = NULL};
-
-
void
_Py_ClearArgcArgv(void)
{
PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
- _PyWideStringList_Clear(&orig_argv);
+ _PyWideStringList_Clear(&_PyRuntime.orig_argv);
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
}
@@ -608,7 +625,9 @@ _Py_SetArgcArgv(Py_ssize_t argc, wchar_t * const *argv)
PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
- res = _PyWideStringList_Copy(&orig_argv, &argv_list);
+ // XXX _PyRuntime.orig_argv only gets cleared by Py_Main(),
+ // so it it currently leaks for embedders.
+ res = _PyWideStringList_Copy(&_PyRuntime.orig_argv, &argv_list);
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
return res;
@@ -619,8 +638,8 @@ _Py_SetArgcArgv(Py_ssize_t argc, wchar_t * const *argv)
void
Py_GetArgcArgv(int *argc, wchar_t ***argv)
{
- *argc = (int)orig_argv.length;
- *argv = orig_argv.items;
+ *argc = (int)_PyRuntime.orig_argv.length;
+ *argv = _PyRuntime.orig_argv.items;
}
@@ -683,6 +702,7 @@ config_check_consistency(const PyConfig *config)
assert(config->pathconfig_warnings >= 0);
assert(config->_is_python_build >= 0);
assert(config->safe_path >= 0);
+ assert(config->int_max_str_digits >= 0);
// config->use_frozen_modules is initialized later
// by _PyConfig_InitImportConfig().
return 1;
@@ -747,6 +767,7 @@ _PyConfig_InitCompatConfig(PyConfig *config)
config->use_hash_seed = -1;
config->faulthandler = -1;
config->tracemalloc = -1;
+ config->perf_profiling = -1;
config->module_search_paths_set = 0;
config->parse_argv = 0;
config->site_import = -1;
@@ -766,7 +787,6 @@ _PyConfig_InitCompatConfig(PyConfig *config)
config->check_hash_pycs_mode = NULL;
config->pathconfig_warnings = -1;
config->_init_main = 1;
- config->_isolated_interpreter = 0;
#ifdef MS_WINDOWS
config->legacy_windows_stdio = -1;
#endif
@@ -776,14 +796,11 @@ _PyConfig_InitCompatConfig(PyConfig *config)
config->use_frozen_modules = 1;
#endif
config->safe_path = 0;
+ 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)
@@ -835,6 +852,8 @@ PyConfig_InitIsolatedConfig(PyConfig *config)
config->use_hash_seed = 0;
config->faulthandler = 0;
config->tracemalloc = 0;
+ config->perf_profiling = 0;
+ config->int_max_str_digits = _PY_LONG_DEFAULT_MAX_STR_DIGITS;
config->safe_path = 1;
config->pathconfig_warnings = 0;
#ifdef MS_WINDOWS
@@ -946,6 +965,7 @@ _PyConfig_Copy(PyConfig *config, const PyConfig *config2)
COPY_ATTR(_install_importlib);
COPY_ATTR(faulthandler);
COPY_ATTR(tracemalloc);
+ COPY_ATTR(perf_profiling);
COPY_ATTR(import_time);
COPY_ATTR(code_debug_ranges);
COPY_ATTR(show_ref_count);
@@ -1001,11 +1021,11 @@ _PyConfig_Copy(PyConfig *config, const PyConfig *config2)
COPY_WSTR_ATTR(check_hash_pycs_mode);
COPY_ATTR(pathconfig_warnings);
COPY_ATTR(_init_main);
- COPY_ATTR(_isolated_interpreter);
COPY_ATTR(use_frozen_modules);
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
@@ -1041,7 +1061,7 @@ _PyConfig_AsDict(const PyConfig *config)
#define FROM_WSTRING(STR) \
((STR != NULL) ? \
PyUnicode_FromWideChar(STR, -1) \
- : (Py_INCREF(Py_None), Py_None))
+ : Py_NewRef(Py_None))
#define SET_ITEM_WSTR(ATTR) \
SET_ITEM(#ATTR, FROM_WSTRING(config->ATTR))
#define SET_ITEM_WSTRLIST(LIST) \
@@ -1056,6 +1076,7 @@ _PyConfig_AsDict(const PyConfig *config)
SET_ITEM_UINT(hash_seed);
SET_ITEM_INT(faulthandler);
SET_ITEM_INT(tracemalloc);
+ SET_ITEM_INT(perf_profiling);
SET_ITEM_INT(import_time);
SET_ITEM_INT(code_debug_ranges);
SET_ITEM_INT(show_ref_count);
@@ -1107,11 +1128,11 @@ _PyConfig_AsDict(const PyConfig *config)
SET_ITEM_WSTR(check_hash_pycs_mode);
SET_ITEM_INT(pathconfig_warnings);
SET_ITEM_INT(_init_main);
- SET_ITEM_INT(_isolated_interpreter);
SET_ITEM_WSTRLIST(orig_argv);
SET_ITEM_INT(use_frozen_modules);
SET_ITEM_INT(safe_path);
SET_ITEM_INT(_is_python_build);
+ SET_ITEM_INT(int_max_str_digits);
return dict;
@@ -1301,6 +1322,12 @@ _PyConfig_FromDict(PyConfig *config, PyObject *dict)
} \
CHECK_VALUE(#KEY, config->KEY >= 0); \
} while (0)
+#define GET_INT(KEY) \
+ do { \
+ if (config_dict_get_int(dict, #KEY, &config->KEY) < 0) { \
+ return -1; \
+ } \
+ } while (0)
#define GET_WSTR(KEY) \
do { \
if (config_dict_get_wstr(dict, #KEY, config, &config->KEY) < 0) { \
@@ -1337,6 +1364,7 @@ _PyConfig_FromDict(PyConfig *config, PyObject *dict)
CHECK_VALUE("hash_seed", config->hash_seed <= MAX_HASH_SEED);
GET_UINT(faulthandler);
GET_UINT(tracemalloc);
+ GET_UINT(perf_profiling);
GET_UINT(import_time);
GET_UINT(code_debug_ranges);
GET_UINT(show_ref_count);
@@ -1394,13 +1422,14 @@ _PyConfig_FromDict(PyConfig *config, PyObject *dict)
GET_UINT(_install_importlib);
GET_UINT(_init_main);
- GET_UINT(_isolated_interpreter);
GET_UINT(use_frozen_modules);
GET_UINT(safe_path);
GET_UINT(_is_python_build);
+ GET_INT(int_max_str_digits);
#undef CHECK_VALUE
#undef GET_UINT
+#undef GET_INT
#undef GET_WSTR
#undef GET_WSTR_OPT
return 0;
@@ -1458,6 +1487,8 @@ config_get_env_dup(PyConfig *config,
static void
config_get_global_vars(PyConfig *config)
{
+_Py_COMP_DIAG_PUSH
+_Py_COMP_DIAG_IGNORE_DEPR_DECLS
if (config->_config_init != _PyConfig_INIT_COMPAT) {
/* Python and Isolated configuration ignore global variables */
return;
@@ -1493,6 +1524,7 @@ config_get_global_vars(PyConfig *config)
#undef COPY_FLAG
#undef COPY_NOT_FLAG
+_Py_COMP_DIAG_POP
}
@@ -1500,6 +1532,8 @@ config_get_global_vars(PyConfig *config)
static void
config_set_global_vars(const PyConfig *config)
{
+_Py_COMP_DIAG_PUSH
+_Py_COMP_DIAG_IGNORE_DEPR_DECLS
#define COPY_FLAG(ATTR, VAR) \
if (config->ATTR != -1) { \
VAR = config->ATTR; \
@@ -1534,6 +1568,7 @@ config_set_global_vars(const PyConfig *config)
#undef COPY_FLAG
#undef COPY_NOT_FLAG
+_Py_COMP_DIAG_POP
}
@@ -1687,6 +1722,26 @@ config_read_env_vars(PyConfig *config)
return _PyStatus_OK();
}
+static PyStatus
+config_init_perf_profiling(PyConfig *config)
+{
+ int active = 0;
+ const char *env = config_get_env(config, "PYTHONPERFSUPPORT");
+ if (env) {
+ if (_Py_str_to_int(env, &active) != 0) {
+ active = 0;
+ }
+ if (active) {
+ config->perf_profiling = 1;
+ }
+ }
+ const wchar_t *xoption = config_get_xoption(config, L"perf");
+ if (xoption) {
+ config->perf_profiling = 1;
+ }
+ return _PyStatus_OK();
+
+}
static PyStatus
config_init_tracemalloc(PyConfig *config)
@@ -1739,7 +1794,7 @@ config_init_int_max_str_digits(PyConfig *config)
const char *env = config_get_env(config, "PYTHONINTMAXSTRDIGITS");
if (env) {
- int valid = 0;
+ bool valid = 0;
if (!_Py_str_to_int(env, &maxdigits)) {
valid = ((maxdigits == 0) || (maxdigits >= _PY_LONG_MAX_STR_DIGITS_THRESHOLD));
}
@@ -1751,13 +1806,13 @@ 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");
if (xoption) {
const wchar_t *sep = wcschr(xoption, L'=');
- int valid = 0;
+ bool valid = 0;
if (sep) {
if (!config_wstr_to_int(sep + 1, &maxdigits)) {
valid = ((maxdigits == 0) || (maxdigits >= _PY_LONG_MAX_STR_DIGITS_THRESHOLD));
@@ -1771,7 +1826,10 @@ 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;
+ }
+ if (config->int_max_str_digits < 0) {
+ config->int_max_str_digits = _PY_LONG_DEFAULT_MAX_STR_DIGITS;
}
return _PyStatus_OK();
}
@@ -1831,7 +1889,15 @@ config_read_complex_options(PyConfig *config)
return status;
}
}
- if (_Py_global_config_int_max_str_digits < 0) {
+
+ if (config->perf_profiling < 0) {
+ status = config_init_perf_profiling(config);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+ }
+
+ if (config->int_max_str_digits < 0) {
status = config_init_int_max_str_digits(config);
if (_PyStatus_EXCEPTION(status)) {
return status;
@@ -2129,6 +2195,12 @@ config_read(PyConfig *config, int compute_path_config)
config->show_ref_count = 1;
}
+#ifdef Py_STATS
+ if (config_get_xoption(config, L"pystats")) {
+ _py_stats = &_py_stats_struct;
+ }
+#endif
+
status = config_read_complex_options(config);
if (_PyStatus_EXCEPTION(status)) {
return status;
@@ -2153,6 +2225,9 @@ config_read(PyConfig *config, int compute_path_config)
if (config->tracemalloc < 0) {
config->tracemalloc = 0;
}
+ if (config->perf_profiling < 0) {
+ config->perf_profiling = 0;
+ }
if (config->use_hash_seed < 0) {
config->use_hash_seed = 0;
config->hash_seed = 0;
@@ -2280,13 +2355,13 @@ config_usage(int error, const wchar_t* program)
}
static void
-config_envvars_usage()
+config_envvars_usage(void)
{
printf(usage_envvars, (wint_t)DELIM, (wint_t)DELIM, PYTHONHOMEHELP);
}
static void
-config_xoptions_usage()
+config_xoptions_usage(void)
{
puts(usage_xoptions);
}
@@ -3068,8 +3143,7 @@ init_dump_ascii_wstr(const wchar_t *str)
void
_Py_DumpPathConfig(PyThreadState *tstate)
{
- PyObject *exc_type, *exc_value, *exc_tb;
- _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
+ PyObject *exc = _PyErr_GetRaisedException(tstate);
PySys_WriteStderr("Python path configuration:\n");
@@ -3127,5 +3201,5 @@ _Py_DumpPathConfig(PyThreadState *tstate)
PySys_WriteStderr(" ]\n");
}
- _PyErr_Restore(tstate, exc_type, exc_value, exc_tb);
+ _PyErr_SetRaisedException(tstate, exc);
}