2018-08-01 12:56:14 -03:00
|
|
|
#include "Python.h"
|
2020-04-14 23:01:58 -03:00
|
|
|
#include "pycore_fileutils.h" // _Py_HasFileSystemDefaultEncodeErrors
|
|
|
|
#include "pycore_getopt.h" // _PyOS_GetOpt()
|
|
|
|
#include "pycore_initconfig.h" // _PyStatus_OK()
|
2020-04-14 12:52:15 -03:00
|
|
|
#include "pycore_interp.h" // _PyInterpreterState.runtime
|
2022-09-02 13:35:08 -03:00
|
|
|
#include "pycore_long.h" // _PY_LONG_MAX_STR_DIGITS_THRESHOLD
|
2020-04-14 23:01:58 -03:00
|
|
|
#include "pycore_pathconfig.h" // _Py_path_config
|
2023-02-28 07:50:52 -04:00
|
|
|
#include "pycore_pyerrors.h" // _PyErr_GetRaisedException()
|
2020-04-14 23:01:58 -03:00
|
|
|
#include "pycore_pylifecycle.h" // _Py_PreInitializeFromConfig()
|
2020-04-14 21:57:50 -03:00
|
|
|
#include "pycore_pymem.h" // _PyMem_SetDefaultAllocator()
|
2020-04-14 12:52:15 -03:00
|
|
|
#include "pycore_pystate.h" // _PyThreadState_GET()
|
2023-09-06 12:54:59 -03:00
|
|
|
#include "pycore_pystats.h" // _Py_StatsOn()
|
2024-09-02 18:25:08 -03:00
|
|
|
#include "pycore_sysmodule.h" // _PySys_SetIntMaxStrDigits()
|
2020-04-14 23:01:58 -03:00
|
|
|
|
|
|
|
#include "osdefs.h" // DELIM
|
2021-10-13 14:25:53 -03:00
|
|
|
|
2020-04-14 12:52:15 -03:00
|
|
|
#include <locale.h> // setlocale()
|
2021-10-13 14:25:53 -03:00
|
|
|
#include <stdlib.h> // getenv()
|
2019-03-01 11:25:19 -04:00
|
|
|
#if defined(MS_WINDOWS) || defined(__CYGWIN__)
|
|
|
|
# ifdef HAVE_IO_H
|
|
|
|
# include <io.h>
|
|
|
|
# endif
|
|
|
|
# ifdef HAVE_FCNTL_H
|
2020-04-14 12:52:15 -03:00
|
|
|
# include <fcntl.h> // O_BINARY
|
2019-03-01 11:25:19 -04:00
|
|
|
# endif
|
2018-08-29 08:25:36 -03:00
|
|
|
#endif
|
|
|
|
|
2024-04-02 17:35:52 -03:00
|
|
|
#include "config_common.h"
|
|
|
|
|
2024-09-02 18:25:08 -03:00
|
|
|
/* --- PyConfig setters ------------------------------------------- */
|
|
|
|
|
|
|
|
typedef PyObject* (*config_sys_flag_setter) (int value);
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
config_sys_flag_long(int value)
|
|
|
|
{
|
|
|
|
return PyLong_FromLong(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
config_sys_flag_not(int value)
|
|
|
|
{
|
|
|
|
value = (!value);
|
|
|
|
return config_sys_flag_long(value);
|
|
|
|
}
|
2024-04-02 17:35:52 -03:00
|
|
|
|
2023-09-30 14:24:06 -03:00
|
|
|
/* --- PyConfig spec ---------------------------------------------- */
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
PyConfig_MEMBER_INT = 0,
|
|
|
|
PyConfig_MEMBER_UINT = 1,
|
|
|
|
PyConfig_MEMBER_ULONG = 2,
|
2024-03-06 05:29:27 -04:00
|
|
|
PyConfig_MEMBER_BOOL = 3,
|
2023-09-30 14:24:06 -03:00
|
|
|
|
|
|
|
PyConfig_MEMBER_WSTR = 10,
|
|
|
|
PyConfig_MEMBER_WSTR_OPT = 11,
|
|
|
|
PyConfig_MEMBER_WSTR_LIST = 12,
|
|
|
|
} PyConfigMemberType;
|
|
|
|
|
2024-09-02 18:25:08 -03:00
|
|
|
typedef enum {
|
|
|
|
// Option which cannot be get or set by PyConfig_Get() and PyConfig_Set()
|
|
|
|
PyConfig_MEMBER_INIT_ONLY = 0,
|
|
|
|
|
|
|
|
// Option which cannot be set by PyConfig_Set()
|
|
|
|
PyConfig_MEMBER_READ_ONLY = 1,
|
|
|
|
|
|
|
|
// Public option: can be get and set by PyConfig_Get() and PyConfig_Set()
|
|
|
|
PyConfig_MEMBER_PUBLIC = 2,
|
|
|
|
} PyConfigMemberVisibility;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
const char *attr;
|
|
|
|
int flag_index;
|
|
|
|
config_sys_flag_setter flag_setter;
|
|
|
|
} PyConfigSysSpec;
|
|
|
|
|
2023-09-30 14:24:06 -03:00
|
|
|
typedef struct {
|
|
|
|
const char *name;
|
|
|
|
size_t offset;
|
|
|
|
PyConfigMemberType type;
|
2024-09-02 18:25:08 -03:00
|
|
|
PyConfigMemberVisibility visibility;
|
|
|
|
PyConfigSysSpec sys;
|
2023-09-30 14:24:06 -03:00
|
|
|
} PyConfigSpec;
|
|
|
|
|
2024-09-02 18:25:08 -03:00
|
|
|
#define SPEC(MEMBER, TYPE, VISIBILITY, sys) \
|
|
|
|
{#MEMBER, offsetof(PyConfig, MEMBER), \
|
|
|
|
PyConfig_MEMBER_##TYPE, PyConfig_MEMBER_##VISIBILITY, sys}
|
|
|
|
|
|
|
|
#define SYS_ATTR(name) {name, -1, NULL}
|
|
|
|
#define SYS_FLAG_SETTER(index, setter) {NULL, index, setter}
|
|
|
|
#define SYS_FLAG(index) SYS_FLAG_SETTER(index, NULL)
|
|
|
|
#define NO_SYS SYS_ATTR(NULL)
|
2023-09-30 14:24:06 -03:00
|
|
|
|
2024-03-06 05:29:27 -04:00
|
|
|
// Update _test_embed_set_config when adding new members
|
2023-09-30 14:24:06 -03:00
|
|
|
static const PyConfigSpec PYCONFIG_SPEC[] = {
|
2024-09-02 18:25:08 -03:00
|
|
|
// --- Public options -----------
|
|
|
|
|
|
|
|
SPEC(argv, WSTR_LIST, PUBLIC, SYS_ATTR("argv")),
|
|
|
|
SPEC(base_exec_prefix, WSTR_OPT, PUBLIC, SYS_ATTR("base_exec_prefix")),
|
|
|
|
SPEC(base_executable, WSTR_OPT, PUBLIC, SYS_ATTR("_base_executable")),
|
|
|
|
SPEC(base_prefix, WSTR_OPT, PUBLIC, SYS_ATTR("base_prefix")),
|
|
|
|
SPEC(bytes_warning, UINT, PUBLIC, SYS_FLAG(9)),
|
|
|
|
SPEC(exec_prefix, WSTR_OPT, PUBLIC, SYS_ATTR("exec_prefix")),
|
|
|
|
SPEC(executable, WSTR_OPT, PUBLIC, SYS_ATTR("executable")),
|
|
|
|
SPEC(inspect, BOOL, PUBLIC, SYS_FLAG(1)),
|
|
|
|
SPEC(int_max_str_digits, UINT, PUBLIC, NO_SYS),
|
|
|
|
SPEC(interactive, BOOL, PUBLIC, SYS_FLAG(2)),
|
|
|
|
SPEC(module_search_paths, WSTR_LIST, PUBLIC, SYS_ATTR("path")),
|
|
|
|
SPEC(optimization_level, UINT, PUBLIC, SYS_FLAG(3)),
|
|
|
|
SPEC(parser_debug, BOOL, PUBLIC, SYS_FLAG(0)),
|
|
|
|
SPEC(platlibdir, WSTR, PUBLIC, SYS_ATTR("platlibdir")),
|
|
|
|
SPEC(prefix, WSTR_OPT, PUBLIC, SYS_ATTR("prefix")),
|
|
|
|
SPEC(pycache_prefix, WSTR_OPT, PUBLIC, SYS_ATTR("pycache_prefix")),
|
|
|
|
SPEC(quiet, BOOL, PUBLIC, SYS_FLAG(10)),
|
|
|
|
SPEC(stdlib_dir, WSTR_OPT, PUBLIC, SYS_ATTR("_stdlib_dir")),
|
|
|
|
SPEC(use_environment, BOOL, PUBLIC, SYS_FLAG_SETTER(7, config_sys_flag_not)),
|
|
|
|
SPEC(verbose, UINT, PUBLIC, SYS_FLAG(8)),
|
|
|
|
SPEC(warnoptions, WSTR_LIST, PUBLIC, SYS_ATTR("warnoptions")),
|
|
|
|
SPEC(write_bytecode, BOOL, PUBLIC, SYS_FLAG_SETTER(4, config_sys_flag_not)),
|
|
|
|
SPEC(xoptions, WSTR_LIST, PUBLIC, SYS_ATTR("_xoptions")),
|
|
|
|
|
|
|
|
// --- Read-only options -----------
|
|
|
|
|
|
|
|
#ifdef Py_STATS
|
|
|
|
SPEC(_pystats, BOOL, READ_ONLY, NO_SYS),
|
2023-09-30 14:24:06 -03:00
|
|
|
#endif
|
2024-09-02 18:25:08 -03:00
|
|
|
SPEC(buffered_stdio, BOOL, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(check_hash_pycs_mode, WSTR, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(code_debug_ranges, BOOL, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(configure_c_stdio, BOOL, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(cpu_count, INT, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(dev_mode, BOOL, READ_ONLY, NO_SYS), // sys.flags.dev_mode
|
|
|
|
SPEC(dump_refs, BOOL, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(dump_refs_file, WSTR_OPT, READ_ONLY, NO_SYS),
|
2024-03-11 12:02:58 -03:00
|
|
|
#ifdef Py_GIL_DISABLED
|
2024-09-02 18:25:08 -03:00
|
|
|
SPEC(enable_gil, INT, READ_ONLY, NO_SYS),
|
2024-11-04 15:13:32 -04:00
|
|
|
SPEC(tlbc_enabled, INT, READ_ONLY, NO_SYS),
|
2024-03-11 12:02:58 -03:00
|
|
|
#endif
|
2024-09-02 18:25:08 -03:00
|
|
|
SPEC(faulthandler, BOOL, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(filesystem_encoding, WSTR, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(filesystem_errors, WSTR, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(hash_seed, ULONG, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(home, WSTR_OPT, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(import_time, BOOL, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(install_signal_handlers, BOOL, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(isolated, BOOL, READ_ONLY, NO_SYS), // sys.flags.isolated
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
SPEC(legacy_windows_stdio, BOOL, READ_ONLY, NO_SYS),
|
2023-10-14 18:32:57 -03:00
|
|
|
#endif
|
2024-09-02 18:25:08 -03:00
|
|
|
SPEC(malloc_stats, BOOL, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(orig_argv, WSTR_LIST, READ_ONLY, SYS_ATTR("orig_argv")),
|
|
|
|
SPEC(parse_argv, BOOL, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(pathconfig_warnings, BOOL, READ_ONLY, NO_SYS),
|
2024-09-27 20:50:16 -03:00
|
|
|
SPEC(perf_profiling, UINT, READ_ONLY, NO_SYS),
|
2024-09-02 18:25:08 -03:00
|
|
|
SPEC(program_name, WSTR, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(run_command, WSTR_OPT, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(run_filename, WSTR_OPT, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(run_module, WSTR_OPT, READ_ONLY, NO_SYS),
|
2023-10-14 18:32:57 -03:00
|
|
|
#ifdef Py_DEBUG
|
2024-09-02 18:25:08 -03:00
|
|
|
SPEC(run_presite, WSTR_OPT, READ_ONLY, NO_SYS),
|
|
|
|
#endif
|
|
|
|
SPEC(safe_path, BOOL, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(show_ref_count, BOOL, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(site_import, BOOL, READ_ONLY, NO_SYS), // sys.flags.no_site
|
|
|
|
SPEC(skip_source_first_line, BOOL, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(stdio_encoding, WSTR, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(stdio_errors, WSTR, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(tracemalloc, UINT, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(use_frozen_modules, BOOL, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(use_hash_seed, BOOL, READ_ONLY, NO_SYS),
|
|
|
|
SPEC(user_site_directory, BOOL, READ_ONLY, NO_SYS), // sys.flags.no_user_site
|
|
|
|
SPEC(warn_default_encoding, BOOL, READ_ONLY, NO_SYS),
|
|
|
|
|
|
|
|
// --- Init-only options -----------
|
|
|
|
|
|
|
|
SPEC(_config_init, UINT, INIT_ONLY, NO_SYS),
|
|
|
|
SPEC(_init_main, BOOL, INIT_ONLY, NO_SYS),
|
|
|
|
SPEC(_install_importlib, BOOL, INIT_ONLY, NO_SYS),
|
|
|
|
SPEC(_is_python_build, BOOL, INIT_ONLY, NO_SYS),
|
|
|
|
SPEC(module_search_paths_set, BOOL, INIT_ONLY, NO_SYS),
|
|
|
|
SPEC(pythonpath_env, WSTR_OPT, INIT_ONLY, NO_SYS),
|
|
|
|
SPEC(sys_path_0, WSTR_OPT, INIT_ONLY, NO_SYS),
|
|
|
|
|
|
|
|
// Array terminator
|
|
|
|
{NULL, 0, 0, 0, NO_SYS},
|
|
|
|
};
|
|
|
|
|
|
|
|
#undef SPEC
|
|
|
|
#define SPEC(MEMBER, TYPE, VISIBILITY) \
|
|
|
|
{#MEMBER, offsetof(PyPreConfig, MEMBER), PyConfig_MEMBER_##TYPE, \
|
|
|
|
PyConfig_MEMBER_##VISIBILITY, NO_SYS}
|
|
|
|
|
|
|
|
static const PyConfigSpec PYPRECONFIG_SPEC[] = {
|
|
|
|
// --- Read-only options -----------
|
|
|
|
|
|
|
|
SPEC(allocator, INT, READ_ONLY),
|
|
|
|
SPEC(coerce_c_locale, BOOL, READ_ONLY),
|
|
|
|
SPEC(coerce_c_locale_warn, BOOL, READ_ONLY),
|
|
|
|
SPEC(configure_locale, BOOL, READ_ONLY),
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
SPEC(legacy_windows_fs_encoding, BOOL, READ_ONLY),
|
2023-09-30 14:24:06 -03:00
|
|
|
#endif
|
2024-09-02 18:25:08 -03:00
|
|
|
SPEC(utf8_mode, BOOL, READ_ONLY),
|
|
|
|
|
|
|
|
// --- Init-only options -----------
|
|
|
|
// Members already present in PYCONFIG_SPEC
|
|
|
|
|
|
|
|
SPEC(_config_init, INT, INIT_ONLY),
|
|
|
|
SPEC(dev_mode, BOOL, INIT_ONLY),
|
|
|
|
SPEC(isolated, BOOL, INIT_ONLY),
|
|
|
|
SPEC(parse_argv, BOOL, INIT_ONLY),
|
|
|
|
SPEC(use_environment, BOOL, INIT_ONLY),
|
|
|
|
|
|
|
|
// Array terminator
|
|
|
|
{NULL, 0, 0, 0, NO_SYS},
|
2023-09-30 14:24:06 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
#undef SPEC
|
2024-09-02 18:25:08 -03:00
|
|
|
#undef SYS_ATTR
|
|
|
|
#undef SYS_FLAG_SETTER
|
|
|
|
#undef SYS_FLAG
|
|
|
|
#undef NO_SYS
|
|
|
|
|
|
|
|
|
|
|
|
// Forward declarations
|
|
|
|
static PyObject*
|
|
|
|
config_get(const PyConfig *config, const PyConfigSpec *spec,
|
|
|
|
int use_sys);
|
2023-09-30 14:24:06 -03:00
|
|
|
|
|
|
|
|
2019-03-01 11:25:19 -04:00
|
|
|
/* --- Command line options --------------------------------------- */
|
|
|
|
|
|
|
|
/* Short usage message (with %s for argv0) */
|
|
|
|
static const char usage_line[] =
|
|
|
|
"usage: %ls [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
|
|
|
|
|
2022-06-01 06:50:01 -03:00
|
|
|
/* Long help message */
|
|
|
|
/* Lines sorted by option name; keep in sync with usage_envvars* below */
|
|
|
|
static const char usage_help[] = "\
|
|
|
|
Options (and corresponding environment variables):\n\
|
2024-03-13 10:03:13 -03:00
|
|
|
-b : issue warnings about converting bytes/bytearray to str and comparing\n\
|
|
|
|
bytes/bytearray with str or bytes with int. (-bb: issue errors)\n\
|
2019-03-01 11:25:19 -04:00
|
|
|
-B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x\n\
|
|
|
|
-c cmd : program passed in as string (terminates option list)\n\
|
2020-12-02 13:56:17 -04:00
|
|
|
-d : turn on parser debugging output (for experts only, only works on\n\
|
|
|
|
debug builds); also PYTHONDEBUG=x\n\
|
2019-03-01 11:25:19 -04:00
|
|
|
-E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
|
2022-05-26 11:31:39 -03:00
|
|
|
-h : print this help message and exit (also -? or --help)\n\
|
2019-03-01 11:25:19 -04:00
|
|
|
-i : inspect interactively after running script; forces a prompt even\n\
|
|
|
|
if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
|
|
|
|
-I : isolate Python from the user's environment (implies -E and -s)\n\
|
|
|
|
-m mod : run library module as a script (terminates option list)\n\
|
|
|
|
-O : remove assert and __debug__-dependent statements; add .opt-1 before\n\
|
|
|
|
.pyc extension; also PYTHONOPTIMIZE=x\n\
|
|
|
|
-OO : do -O changes and also discard docstrings; add .opt-2 before\n\
|
|
|
|
.pyc extension\n\
|
2024-02-23 05:27:07 -04:00
|
|
|
-P : don't prepend a potentially unsafe path to sys.path; also\n\
|
|
|
|
PYTHONSAFEPATH\n\
|
2019-03-01 11:25:19 -04:00
|
|
|
-q : don't print version and copyright messages on interactive startup\n\
|
2024-03-19 14:26:32 -03:00
|
|
|
-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE=x\n\
|
2019-03-01 11:25:19 -04:00
|
|
|
-S : don't imply 'import site' on initialization\n\
|
|
|
|
-u : force the stdout and stderr streams to be unbuffered;\n\
|
|
|
|
this option has no effect on stdin; also PYTHONUNBUFFERED=x\n\
|
|
|
|
-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
|
|
|
|
can be supplied multiple times to increase verbosity\n\
|
|
|
|
-V : print the Python version number and exit (also --version)\n\
|
|
|
|
when given twice, print more information about the build\n\
|
|
|
|
-W arg : warning control; arg is action:message:category:module:lineno\n\
|
|
|
|
also PYTHONWARNINGS=arg\n\
|
|
|
|
-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
|
2022-06-01 06:50:01 -03:00
|
|
|
-X opt : set implementation-specific option\n\
|
2019-03-01 11:25:19 -04:00
|
|
|
--check-hash-based-pycs always|default|never:\n\
|
2022-06-01 06:50:01 -03:00
|
|
|
control how Python invalidates hash-based .pyc files\n\
|
2024-02-23 05:27:07 -04:00
|
|
|
--help-env: print help about Python environment variables and exit\n\
|
|
|
|
--help-xoptions: print help about implementation-specific -X options and exit\n\
|
|
|
|
--help-all: print complete help information and exit\n\
|
|
|
|
\n\
|
2022-06-01 06:50:01 -03:00
|
|
|
Arguments:\n\
|
2019-03-01 11:25:19 -04:00
|
|
|
file : program read from script file\n\
|
|
|
|
- : program read from stdin (default; interactive mode if a tty)\n\
|
2022-06-01 06:50:01 -03:00
|
|
|
arg ...: arguments passed to program in sys.argv[1:]\n\
|
2019-03-01 11:25:19 -04:00
|
|
|
";
|
2022-06-01 06:50:01 -03:00
|
|
|
|
|
|
|
static const char usage_xoptions[] = "\
|
|
|
|
The following implementation-specific options are available:\n\
|
2024-03-19 14:26:32 -03:00
|
|
|
-X cpu_count=N: override the return value of os.cpu_count();\n\
|
|
|
|
-X cpu_count=default cancels overriding; also PYTHON_CPU_COUNT\n\
|
|
|
|
-X dev : enable Python Development Mode; also PYTHONDEVMODE\n\
|
|
|
|
-X faulthandler: dump the Python traceback on fatal errors;\n\
|
|
|
|
also PYTHONFAULTHANDLER\n\
|
|
|
|
-X frozen_modules=[on|off]: whether to use frozen modules; the default is \"on\"\n\
|
|
|
|
for installed Python and \"off\" for a local build;\n\
|
|
|
|
also PYTHON_FROZEN_MODULES\n\
|
2024-03-19 18:30:25 -03:00
|
|
|
"
|
|
|
|
#ifdef Py_GIL_DISABLED
|
|
|
|
"-X gil=[0|1]: enable (1) or disable (0) the GIL; also PYTHON_GIL\n"
|
|
|
|
#endif
|
|
|
|
"\
|
2024-03-19 14:26:32 -03:00
|
|
|
-X importtime: show how long each import takes; also PYTHONPROFILEIMPORTTIME\n\
|
|
|
|
-X int_max_str_digits=N: limit the size of int<->str conversions;\n\
|
|
|
|
0 disables the limit; also PYTHONINTMAXSTRDIGITS\n\
|
|
|
|
-X no_debug_ranges: don't include extra location information in code objects;\n\
|
|
|
|
also PYTHONNODEBUGRANGES\n\
|
|
|
|
-X perf: support the Linux \"perf\" profiler; also PYTHONPERFSUPPORT=1\n\
|
2024-03-13 16:15:44 -03:00
|
|
|
"
|
2023-10-14 18:32:57 -03:00
|
|
|
#ifdef Py_DEBUG
|
2024-03-19 14:26:32 -03:00
|
|
|
"-X presite=MOD: import this module before site; also PYTHON_PRESITE\n"
|
2023-10-14 18:32:57 -03:00
|
|
|
#endif
|
2024-03-13 16:15:44 -03:00
|
|
|
"\
|
2024-03-19 14:26:32 -03:00
|
|
|
-X pycache_prefix=PATH: write .pyc files to a parallel tree instead of to the\n\
|
|
|
|
code tree; also PYTHONPYCACHEPREFIX\n\
|
2024-03-13 16:15:44 -03:00
|
|
|
"
|
|
|
|
#ifdef Py_STATS
|
2024-03-19 14:26:32 -03:00
|
|
|
"-X pystats: enable pystats collection at startup; also PYTHONSTATS\n"
|
2024-03-13 16:15:44 -03:00
|
|
|
#endif
|
|
|
|
"\
|
|
|
|
-X showrefcount: output the total reference count and number of used\n\
|
|
|
|
memory blocks when the program finishes or after each statement in\n\
|
2024-11-04 15:13:32 -04:00
|
|
|
the interactive interpreter; only works on debug builds\n"
|
|
|
|
#ifdef Py_GIL_DISABLED
|
|
|
|
"-X tlbc=[0|1]: enable (1) or disable (0) thread-local bytecode. Also\n\
|
|
|
|
PYTHON_TLBC\n"
|
|
|
|
#endif
|
|
|
|
"\
|
|
|
|
-X tracemalloc[=N]: trace Python memory allocations; N sets a traceback limit\n \
|
2024-03-19 14:26:32 -03:00
|
|
|
of N frames (default: 1); also PYTHONTRACEMALLOC=N\n\
|
|
|
|
-X utf8[=0|1]: enable (1) or disable (0) UTF-8 mode; also PYTHONUTF8\n\
|
|
|
|
-X warn_default_encoding: enable opt-in EncodingWarning for 'encoding=None';\n\
|
|
|
|
also PYTHONWARNDEFAULTENCODING\
|
|
|
|
";
|
2022-06-01 06:50:01 -03:00
|
|
|
|
|
|
|
/* Envvars that don't have equivalent command-line options are listed first */
|
|
|
|
static const char usage_envvars[] =
|
|
|
|
"Environment variables that change behavior:\n"
|
2024-02-23 05:27:07 -04:00
|
|
|
"PYTHONSTARTUP : file executed on interactive startup (no default)\n"
|
|
|
|
"PYTHONPATH : '%lc'-separated list of directories prefixed to the\n"
|
|
|
|
" default module search path. The result is sys.path.\n"
|
|
|
|
"PYTHONHOME : alternate <prefix> directory (or <prefix>%lc<exec_prefix>).\n"
|
|
|
|
" The default module search path uses %s.\n"
|
2024-03-19 14:26:32 -03:00
|
|
|
"PYTHONPLATLIBDIR: override sys.platlibdir\n"
|
|
|
|
"PYTHONCASEOK : ignore case in 'import' statements (Windows)\n"
|
|
|
|
"PYTHONIOENCODING: encoding[:errors] used for stdin/stdout/stderr\n"
|
2024-02-23 05:27:07 -04:00
|
|
|
"PYTHONHASHSEED : if this variable is set to 'random', a random value is used\n"
|
|
|
|
" to seed the hashes of str and bytes objects. It can also be\n"
|
|
|
|
" set to an integer in the range [0,4294967295] to get hash\n"
|
|
|
|
" values with a predictable seed.\n"
|
|
|
|
"PYTHONMALLOC : set the Python memory allocators and/or install debug hooks\n"
|
|
|
|
" on Python memory allocators. Use PYTHONMALLOC=debug to\n"
|
|
|
|
" install debug hooks.\n"
|
2024-06-18 14:12:58 -03:00
|
|
|
"PYTHONMALLOCSTATS: print memory allocator statistics\n"
|
2019-03-01 11:25:19 -04:00
|
|
|
"PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale\n"
|
2024-02-23 05:27:07 -04:00
|
|
|
" coercion behavior. Use PYTHONCOERCECLOCALE=warn to request\n"
|
|
|
|
" display of locale coercion and locale compatibility warnings\n"
|
|
|
|
" on stderr.\n"
|
2019-03-01 11:25:19 -04:00
|
|
|
"PYTHONBREAKPOINT: if this variable is set to 0, it disables the default\n"
|
2024-02-23 05:27:07 -04:00
|
|
|
" debugger. It can be set to the callable of your debugger of\n"
|
|
|
|
" choice.\n"
|
|
|
|
"PYTHON_COLORS : if this variable is set to 1, the interpreter will colorize\n"
|
|
|
|
" various kinds of output. Setting it to 0 deactivates\n"
|
|
|
|
" this behavior.\n"
|
|
|
|
"PYTHON_HISTORY : the location of a .python_history file.\n"
|
2024-06-18 14:12:58 -03:00
|
|
|
"PYTHONASYNCIODEBUG: enable asyncio debug mode\n"
|
|
|
|
#ifdef Py_TRACE_REFS
|
|
|
|
"PYTHONDUMPREFS : dump objects and reference counts still alive after shutdown\n"
|
|
|
|
"PYTHONDUMPREFSFILE: dump objects and reference counts to the specified file\n"
|
|
|
|
#endif
|
|
|
|
#ifdef __APPLE__
|
|
|
|
"PYTHONEXECUTABLE: set sys.argv[0] to this value (macOS only)\n"
|
|
|
|
#endif
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
"PYTHONLEGACYWINDOWSFSENCODING: use legacy \"mbcs\" encoding for file system\n"
|
|
|
|
"PYTHONLEGACYWINDOWSSTDIO: use legacy Windows stdio\n"
|
|
|
|
#endif
|
|
|
|
"PYTHONUSERBASE : defines the user base directory (site.USER_BASE)\n"
|
|
|
|
"PYTHON_BASIC_REPL: use the traditional parser-based REPL\n"
|
2024-02-23 05:27:07 -04:00
|
|
|
"\n"
|
|
|
|
"These variables have equivalent command-line options (see --help for details):\n"
|
2024-03-19 14:26:32 -03:00
|
|
|
"PYTHON_CPU_COUNT: override the return value of os.cpu_count() (-X cpu_count)\n"
|
2024-02-23 05:27:07 -04:00
|
|
|
"PYTHONDEBUG : enable parser debug mode (-d)\n"
|
2024-03-19 14:26:32 -03:00
|
|
|
"PYTHONDEVMODE : enable Python Development Mode (-X dev)\n"
|
2024-02-23 05:27:07 -04:00
|
|
|
"PYTHONDONTWRITEBYTECODE: don't write .pyc files (-B)\n"
|
2024-03-13 17:59:16 -03:00
|
|
|
"PYTHONFAULTHANDLER: dump the Python traceback on fatal errors (-X faulthandler)\n"
|
2024-03-19 14:26:32 -03:00
|
|
|
"PYTHON_FROZEN_MODULES: whether to use frozen modules; the default is \"on\"\n"
|
|
|
|
" for installed Python and \"off\" for a local build\n"
|
2024-03-13 17:59:16 -03:00
|
|
|
" (-X frozen_modules)\n"
|
|
|
|
#ifdef Py_GIL_DISABLED
|
|
|
|
"PYTHON_GIL : when set to 0, disables the GIL (-X gil)\n"
|
|
|
|
#endif
|
2024-02-23 05:27:07 -04:00
|
|
|
"PYTHONINSPECT : inspect interactively after running script (-i)\n"
|
2024-03-19 14:26:32 -03:00
|
|
|
"PYTHONINTMAXSTRDIGITS: limit the size of int<->str conversions;\n"
|
|
|
|
" 0 disables the limit (-X int_max_str_digits=N)\n"
|
|
|
|
"PYTHONNODEBUGRANGES: don't include extra location information in code objects\n"
|
|
|
|
" (-X no_debug_ranges)\n"
|
2024-02-23 05:27:07 -04:00
|
|
|
"PYTHONNOUSERSITE: disable user site directory (-s)\n"
|
|
|
|
"PYTHONOPTIMIZE : enable level 1 optimizations (-O)\n"
|
2024-03-14 07:09:19 -03:00
|
|
|
"PYTHONPERFSUPPORT: support the Linux \"perf\" profiler (-X perf)\n"
|
2024-06-18 14:12:58 -03:00
|
|
|
"PYTHON_PERF_JIT_SUPPORT: enable Linux \"perf\" profiler support with JIT\n"
|
|
|
|
" (-X perf_jit)\n"
|
2024-03-13 16:15:44 -03:00
|
|
|
#ifdef Py_DEBUG
|
2024-03-19 14:26:32 -03:00
|
|
|
"PYTHON_PRESITE: import this module before site (-X presite)\n"
|
2024-03-13 16:15:44 -03:00
|
|
|
#endif
|
2024-03-14 07:09:19 -03:00
|
|
|
"PYTHONPROFILEIMPORTTIME: show how long each import takes (-X importtime)\n"
|
2024-03-13 17:59:16 -03:00
|
|
|
"PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files\n"
|
|
|
|
" (-X pycache_prefix)\n"
|
2024-02-23 05:27:07 -04:00
|
|
|
"PYTHONSAFEPATH : don't prepend a potentially unsafe path to sys.path.\n"
|
2023-09-06 12:54:59 -03:00
|
|
|
#ifdef Py_STATS
|
2024-03-13 17:59:16 -03:00
|
|
|
"PYTHONSTATS : turns on statistics gathering (-X pystats)\n"
|
2023-09-06 12:54:59 -03:00
|
|
|
#endif
|
2024-11-04 15:13:32 -04:00
|
|
|
#ifdef Py_GIL_DISABLED
|
|
|
|
"PYTHON_TLBC : when set to 0, disables thread-local bytecode (-X tlbc)\n"
|
|
|
|
#endif
|
2024-03-14 07:09:19 -03:00
|
|
|
"PYTHONTRACEMALLOC: trace Python memory allocations (-X tracemalloc)\n"
|
2024-03-13 16:15:44 -03:00
|
|
|
"PYTHONUNBUFFERED: disable stdout/stderr buffering (-u)\n"
|
2024-03-19 14:26:32 -03:00
|
|
|
"PYTHONUTF8 : control the UTF-8 mode (-X utf8)\n"
|
2024-03-13 16:15:44 -03:00
|
|
|
"PYTHONVERBOSE : trace import statements (-v)\n"
|
2024-03-13 17:59:16 -03:00
|
|
|
"PYTHONWARNDEFAULTENCODING: enable opt-in EncodingWarning for 'encoding=None'\n"
|
|
|
|
" (-X warn_default_encoding)\n"
|
2024-03-19 14:26:32 -03:00
|
|
|
"PYTHONWARNINGS : warning control (-W)\n"
|
2023-09-06 12:54:59 -03:00
|
|
|
;
|
2019-03-01 11:25:19 -04:00
|
|
|
|
|
|
|
#if defined(MS_WINDOWS)
|
|
|
|
# define PYTHONHOMEHELP "<prefix>\\python{major}{minor}"
|
|
|
|
#else
|
|
|
|
# define PYTHONHOMEHELP "<prefix>/lib/pythonX.X"
|
|
|
|
#endif
|
2018-08-01 12:56:14 -03:00
|
|
|
|
|
|
|
|
2019-03-01 11:25:19 -04:00
|
|
|
/* --- Global configuration variables ----------------------------- */
|
2018-08-29 08:25:36 -03:00
|
|
|
|
2018-08-01 12:56:14 -03:00
|
|
|
/* UTF-8 mode (PEP 540): if equals to 1, use the UTF-8 encoding, and change
|
2019-05-22 23:12:27 -03:00
|
|
|
stdin and stdout error handler to "surrogateescape". */
|
|
|
|
int Py_UTF8Mode = 0;
|
2018-08-01 12:56:14 -03:00
|
|
|
int Py_DebugFlag = 0; /* Needed by parser.c */
|
|
|
|
int Py_VerboseFlag = 0; /* Needed by import.c */
|
|
|
|
int Py_QuietFlag = 0; /* Needed by sysmodule.c */
|
2022-06-17 10:19:28 -03:00
|
|
|
int Py_InteractiveFlag = 0; /* Previously, was used by Py_FdIsInteractive() */
|
2018-08-01 12:56:14 -03:00
|
|
|
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' */
|
|
|
|
int Py_BytesWarningFlag = 0; /* Warn on str(bytes) and str(buffer) */
|
|
|
|
int Py_FrozenFlag = 0; /* Needed by getpath.c */
|
|
|
|
int Py_IgnoreEnvironmentFlag = 0; /* e.g. PYTHONPATH, PYTHONHOME */
|
|
|
|
int Py_DontWriteBytecodeFlag = 0; /* Suppress writing bytecode files (*.pyc) */
|
|
|
|
int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
|
|
|
|
int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */
|
|
|
|
int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */
|
|
|
|
int Py_IsolatedFlag = 0; /* for -I, isolate from user's env */
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
int Py_LegacyWindowsFSEncodingFlag = 0; /* Uses mbcs instead of utf-8 */
|
|
|
|
int Py_LegacyWindowsStdioFlag = 0; /* Uses FileIO instead of WindowsConsoleIO */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2019-03-25 19:19:57 -03:00
|
|
|
static PyObject *
|
2018-11-13 19:24:28 -04:00
|
|
|
_Py_GetGlobalVariablesAsDict(void)
|
|
|
|
{
|
2022-06-17 11:12:00 -03:00
|
|
|
_Py_COMP_DIAG_PUSH
|
|
|
|
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
|
2018-11-13 19:24:28 -04:00
|
|
|
PyObject *dict, *obj;
|
|
|
|
|
|
|
|
dict = PyDict_New();
|
|
|
|
if (dict == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SET_ITEM(KEY, EXPR) \
|
|
|
|
do { \
|
|
|
|
obj = (EXPR); \
|
|
|
|
if (obj == NULL) { \
|
|
|
|
return NULL; \
|
|
|
|
} \
|
|
|
|
int res = PyDict_SetItemString(dict, (KEY), obj); \
|
|
|
|
Py_DECREF(obj); \
|
|
|
|
if (res < 0) { \
|
|
|
|
goto fail; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#define SET_ITEM_INT(VAR) \
|
|
|
|
SET_ITEM(#VAR, PyLong_FromLong(VAR))
|
|
|
|
#define FROM_STRING(STR) \
|
|
|
|
((STR != NULL) ? \
|
|
|
|
PyUnicode_FromString(STR) \
|
2022-11-10 04:03:39 -04:00
|
|
|
: Py_NewRef(Py_None))
|
2018-11-13 19:24:28 -04:00
|
|
|
#define SET_ITEM_STR(VAR) \
|
|
|
|
SET_ITEM(#VAR, FROM_STRING(VAR))
|
|
|
|
|
|
|
|
SET_ITEM_STR(Py_FileSystemDefaultEncoding);
|
|
|
|
SET_ITEM_INT(Py_HasFileSystemDefaultEncoding);
|
|
|
|
SET_ITEM_STR(Py_FileSystemDefaultEncodeErrors);
|
|
|
|
SET_ITEM_INT(_Py_HasFileSystemDefaultEncodeErrors);
|
|
|
|
|
|
|
|
SET_ITEM_INT(Py_UTF8Mode);
|
|
|
|
SET_ITEM_INT(Py_DebugFlag);
|
|
|
|
SET_ITEM_INT(Py_VerboseFlag);
|
|
|
|
SET_ITEM_INT(Py_QuietFlag);
|
|
|
|
SET_ITEM_INT(Py_InteractiveFlag);
|
|
|
|
SET_ITEM_INT(Py_InspectFlag);
|
|
|
|
|
|
|
|
SET_ITEM_INT(Py_OptimizeFlag);
|
|
|
|
SET_ITEM_INT(Py_NoSiteFlag);
|
|
|
|
SET_ITEM_INT(Py_BytesWarningFlag);
|
|
|
|
SET_ITEM_INT(Py_FrozenFlag);
|
|
|
|
SET_ITEM_INT(Py_IgnoreEnvironmentFlag);
|
|
|
|
SET_ITEM_INT(Py_DontWriteBytecodeFlag);
|
|
|
|
SET_ITEM_INT(Py_NoUserSiteDirectory);
|
|
|
|
SET_ITEM_INT(Py_UnbufferedStdioFlag);
|
|
|
|
SET_ITEM_INT(Py_HashRandomizationFlag);
|
|
|
|
SET_ITEM_INT(Py_IsolatedFlag);
|
|
|
|
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
SET_ITEM_INT(Py_LegacyWindowsFSEncodingFlag);
|
|
|
|
SET_ITEM_INT(Py_LegacyWindowsStdioFlag);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return dict;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
Py_DECREF(dict);
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
#undef FROM_STRING
|
|
|
|
#undef SET_ITEM
|
|
|
|
#undef SET_ITEM_INT
|
|
|
|
#undef SET_ITEM_STR
|
2022-06-17 11:12:00 -03:00
|
|
|
_Py_COMP_DIAG_POP
|
2018-11-13 19:24:28 -04:00
|
|
|
}
|
|
|
|
|
2021-10-12 22:39:50 -03:00
|
|
|
char*
|
|
|
|
Py_GETENV(const char *name)
|
|
|
|
{
|
2022-06-17 11:12:00 -03:00
|
|
|
_Py_COMP_DIAG_PUSH
|
|
|
|
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
|
2021-10-12 22:39:50 -03:00
|
|
|
if (Py_IgnoreEnvironmentFlag) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return getenv(name);
|
2022-06-17 11:12:00 -03:00
|
|
|
_Py_COMP_DIAG_POP
|
2021-10-12 22:39:50 -03:00
|
|
|
}
|
2018-11-13 19:24:28 -04:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
/* --- PyStatus ----------------------------------------------- */
|
2019-05-17 18:54:00 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus PyStatus_Ok(void)
|
|
|
|
{ return _PyStatus_OK(); }
|
2019-05-17 18:54:00 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus PyStatus_Error(const char *err_msg)
|
2019-05-17 18:54:00 -03:00
|
|
|
{
|
2020-11-04 19:45:56 -04:00
|
|
|
assert(err_msg != NULL);
|
2019-05-27 11:39:22 -03:00
|
|
|
return (PyStatus){._type = _PyStatus_TYPE_ERROR,
|
2020-11-04 19:45:56 -04:00
|
|
|
.err_msg = err_msg};
|
2019-05-17 18:54:00 -03:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus PyStatus_NoMemory(void)
|
|
|
|
{ return PyStatus_Error("memory allocation failed"); }
|
2019-05-17 18:54:00 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus PyStatus_Exit(int exitcode)
|
|
|
|
{ return _PyStatus_EXIT(exitcode); }
|
2019-05-17 18:54:00 -03:00
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
int PyStatus_IsError(PyStatus status)
|
|
|
|
{ return _PyStatus_IS_ERROR(status); }
|
2019-05-17 18:54:00 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
int PyStatus_IsExit(PyStatus status)
|
|
|
|
{ return _PyStatus_IS_EXIT(status); }
|
2019-05-17 18:54:00 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
int PyStatus_Exception(PyStatus status)
|
|
|
|
{ return _PyStatus_EXCEPTION(status); }
|
2019-05-17 18:54:00 -03:00
|
|
|
|
2023-09-01 07:43:30 -03:00
|
|
|
void
|
2020-11-04 19:45:56 -04:00
|
|
|
_PyErr_SetFromPyStatus(PyStatus status)
|
|
|
|
{
|
|
|
|
if (!_PyStatus_IS_ERROR(status)) {
|
|
|
|
PyErr_Format(PyExc_SystemError,
|
2023-09-01 07:43:30 -03:00
|
|
|
"_PyErr_SetFromPyStatus() status is not an error");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *err_msg = status.err_msg;
|
|
|
|
if (err_msg == NULL || strlen(err_msg) == 0) {
|
|
|
|
PyErr_Format(PyExc_SystemError,
|
|
|
|
"_PyErr_SetFromPyStatus() status has no error message");
|
|
|
|
return;
|
2020-11-04 19:45:56 -04:00
|
|
|
}
|
2023-09-01 07:43:30 -03:00
|
|
|
|
|
|
|
if (strcmp(err_msg, _PyStatus_NO_MEMORY_ERRMSG) == 0) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *func = status.func;
|
|
|
|
if (func) {
|
|
|
|
PyErr_Format(PyExc_RuntimeError, "%s: %s", func, err_msg);
|
2020-11-04 19:45:56 -04:00
|
|
|
}
|
|
|
|
else {
|
2023-09-01 07:43:30 -03:00
|
|
|
PyErr_Format(PyExc_RuntimeError, "%s", err_msg);
|
2020-11-04 19:45:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 18:54:00 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
/* --- PyWideStringList ------------------------------------------------ */
|
2019-03-15 11:08:05 -03:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
int
|
2019-05-27 11:39:22 -03:00
|
|
|
_PyWideStringList_CheckConsistency(const PyWideStringList *list)
|
2019-03-15 11:08:05 -03:00
|
|
|
{
|
|
|
|
assert(list->length >= 0);
|
|
|
|
if (list->length != 0) {
|
|
|
|
assert(list->items != NULL);
|
|
|
|
}
|
|
|
|
for (Py_ssize_t i = 0; i < list->length; i++) {
|
|
|
|
assert(list->items[i] != NULL);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif /* Py_DEBUG */
|
|
|
|
|
2019-03-01 11:25:19 -04:00
|
|
|
|
2018-08-01 12:56:14 -03:00
|
|
|
void
|
2019-05-27 11:39:22 -03:00
|
|
|
_PyWideStringList_Clear(PyWideStringList *list)
|
2018-08-01 12:56:14 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
assert(_PyWideStringList_CheckConsistency(list));
|
2019-03-15 11:08:05 -03:00
|
|
|
for (Py_ssize_t i=0; i < list->length; i++) {
|
|
|
|
PyMem_RawFree(list->items[i]);
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
2019-03-15 11:08:05 -03:00
|
|
|
PyMem_RawFree(list->items);
|
|
|
|
list->length = 0;
|
|
|
|
list->items = NULL;
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-15 11:08:05 -03:00
|
|
|
int
|
2019-05-27 11:39:22 -03:00
|
|
|
_PyWideStringList_Copy(PyWideStringList *list, const PyWideStringList *list2)
|
2018-08-01 12:56:14 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
assert(_PyWideStringList_CheckConsistency(list));
|
|
|
|
assert(_PyWideStringList_CheckConsistency(list2));
|
2019-03-15 11:08:05 -03:00
|
|
|
|
|
|
|
if (list2->length == 0) {
|
2019-05-27 11:39:22 -03:00
|
|
|
_PyWideStringList_Clear(list);
|
2019-03-15 11:08:05 -03:00
|
|
|
return 0;
|
2018-08-24 20:34:56 -03:00
|
|
|
}
|
2019-03-15 11:08:05 -03:00
|
|
|
|
2019-09-29 20:40:17 -03:00
|
|
|
PyWideStringList copy = _PyWideStringList_INIT;
|
2019-03-15 11:08:05 -03:00
|
|
|
|
|
|
|
size_t size = list2->length * sizeof(list2->items[0]);
|
|
|
|
copy.items = PyMem_RawMalloc(size);
|
|
|
|
if (copy.items == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Py_ssize_t i=0; i < list2->length; i++) {
|
|
|
|
wchar_t *item = _PyMem_RawWcsdup(list2->items[i]);
|
|
|
|
if (item == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
_PyWideStringList_Clear(©);
|
2019-03-15 11:08:05 -03:00
|
|
|
return -1;
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
2019-03-15 11:08:05 -03:00
|
|
|
copy.items[i] = item;
|
|
|
|
copy.length = i + 1;
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
2019-03-15 11:08:05 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
_PyWideStringList_Clear(list);
|
2019-03-15 11:08:05 -03:00
|
|
|
*list = copy;
|
|
|
|
return 0;
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus
|
2019-08-23 12:57:54 -03:00
|
|
|
PyWideStringList_Insert(PyWideStringList *list,
|
|
|
|
Py_ssize_t index, const wchar_t *item)
|
2019-03-01 11:25:19 -04:00
|
|
|
{
|
2019-08-23 12:57:54 -03:00
|
|
|
Py_ssize_t len = list->length;
|
|
|
|
if (len == PY_SSIZE_T_MAX) {
|
2019-07-21 17:12:33 -03:00
|
|
|
/* length+1 would overflow */
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_NO_MEMORY();
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
2019-08-23 12:57:54 -03:00
|
|
|
if (index < 0) {
|
|
|
|
return _PyStatus_ERR("PyWideStringList_Insert index must be >= 0");
|
|
|
|
}
|
|
|
|
if (index > len) {
|
|
|
|
index = len;
|
|
|
|
}
|
2019-03-15 11:08:05 -03:00
|
|
|
|
|
|
|
wchar_t *item2 = _PyMem_RawWcsdup(item);
|
|
|
|
if (item2 == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_NO_MEMORY();
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
|
2019-08-23 12:57:54 -03:00
|
|
|
size_t size = (len + 1) * sizeof(list->items[0]);
|
2019-03-15 11:08:05 -03:00
|
|
|
wchar_t **items2 = (wchar_t **)PyMem_RawRealloc(list->items, size);
|
|
|
|
if (items2 == NULL) {
|
|
|
|
PyMem_RawFree(item2);
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_NO_MEMORY();
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
2019-03-15 11:08:05 -03:00
|
|
|
|
2019-08-23 12:57:54 -03:00
|
|
|
if (index < len) {
|
|
|
|
memmove(&items2[index + 1],
|
|
|
|
&items2[index],
|
|
|
|
(len - index) * sizeof(items2[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
items2[index] = item2;
|
2019-03-15 11:08:05 -03:00
|
|
|
list->items = items2;
|
|
|
|
list->length++;
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2019-03-15 11:08:05 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-23 12:57:54 -03:00
|
|
|
PyStatus
|
|
|
|
PyWideStringList_Append(PyWideStringList *list, const wchar_t *item)
|
|
|
|
{
|
|
|
|
return PyWideStringList_Insert(list, list->length, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus
|
|
|
|
_PyWideStringList_Extend(PyWideStringList *list, const PyWideStringList *list2)
|
2019-03-15 11:08:05 -03:00
|
|
|
{
|
|
|
|
for (Py_ssize_t i = 0; i < list2->length; i++) {
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status = PyWideStringList_Append(list, list2->items[i]);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-03-15 11:08:05 -03:00
|
|
|
}
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-26 12:58:50 -03:00
|
|
|
static int
|
2019-05-27 11:39:22 -03:00
|
|
|
_PyWideStringList_Find(PyWideStringList *list, const wchar_t *item)
|
2019-03-26 12:58:50 -03:00
|
|
|
{
|
|
|
|
for (Py_ssize_t i = 0; i < list->length; i++) {
|
|
|
|
if (wcscmp(list->items[i], item) == 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-01 11:25:19 -04:00
|
|
|
PyObject*
|
2019-05-27 11:39:22 -03:00
|
|
|
_PyWideStringList_AsList(const PyWideStringList *list)
|
2019-03-01 11:25:19 -04:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
assert(_PyWideStringList_CheckConsistency(list));
|
2019-03-01 11:25:19 -04:00
|
|
|
|
2019-03-15 11:08:05 -03:00
|
|
|
PyObject *pylist = PyList_New(list->length);
|
2019-03-01 11:25:19 -04:00
|
|
|
if (pylist == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-03-15 11:08:05 -03:00
|
|
|
for (Py_ssize_t i = 0; i < list->length; i++) {
|
|
|
|
PyObject *item = PyUnicode_FromWideChar(list->items[i], -1);
|
|
|
|
if (item == NULL) {
|
2019-03-01 11:25:19 -04:00
|
|
|
Py_DECREF(pylist);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-03-15 11:08:05 -03:00
|
|
|
PyList_SET_ITEM(pylist, i, item);
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
return pylist;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-02 18:25:08 -03:00
|
|
|
static PyObject*
|
|
|
|
_PyWideStringList_AsTuple(const PyWideStringList *list)
|
|
|
|
{
|
|
|
|
assert(_PyWideStringList_CheckConsistency(list));
|
|
|
|
|
|
|
|
PyObject *tuple = PyTuple_New(list->length);
|
|
|
|
if (tuple == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Py_ssize_t i = 0; i < list->length; i++) {
|
|
|
|
PyObject *item = PyUnicode_FromWideChar(list->items[i], -1);
|
|
|
|
if (item == NULL) {
|
|
|
|
Py_DECREF(tuple);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyTuple_SET_ITEM(tuple, i, item);
|
|
|
|
}
|
|
|
|
return tuple;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-01 11:25:19 -04:00
|
|
|
/* --- Py_GetArgcArgv() ------------------------------------------- */
|
|
|
|
|
|
|
|
void
|
|
|
|
_Py_ClearArgcArgv(void)
|
|
|
|
{
|
|
|
|
PyMemAllocatorEx old_alloc;
|
|
|
|
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
2022-12-07 18:56:31 -04:00
|
|
|
_PyWideStringList_Clear(&_PyRuntime.orig_argv);
|
2019-03-01 11:25:19 -04:00
|
|
|
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-05 20:44:31 -04:00
|
|
|
static int
|
2019-03-15 11:08:05 -03:00
|
|
|
_Py_SetArgcArgv(Py_ssize_t argc, wchar_t * const *argv)
|
2019-03-01 11:25:19 -04:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
const PyWideStringList argv_list = {.length = argc, .items = (wchar_t **)argv};
|
2019-03-01 11:25:19 -04:00
|
|
|
int res;
|
|
|
|
|
|
|
|
PyMemAllocatorEx old_alloc;
|
|
|
|
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
|
2022-12-07 18:56:31 -04:00
|
|
|
// XXX _PyRuntime.orig_argv only gets cleared by Py_Main(),
|
2023-07-01 16:47:14 -03:00
|
|
|
// so it currently leaks for embedders.
|
2022-12-07 18:56:31 -04:00
|
|
|
res = _PyWideStringList_Copy(&_PyRuntime.orig_argv, &argv_list);
|
2019-03-01 11:25:19 -04:00
|
|
|
|
|
|
|
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-02 11:49:54 -04:00
|
|
|
// _PyConfig_Write() calls _Py_SetArgcArgv() with PyConfig.orig_argv.
|
2019-03-01 11:25:19 -04:00
|
|
|
void
|
|
|
|
Py_GetArgcArgv(int *argc, wchar_t ***argv)
|
|
|
|
{
|
2022-12-07 18:56:31 -04:00
|
|
|
*argc = (int)_PyRuntime.orig_argv.length;
|
|
|
|
*argv = _PyRuntime.orig_argv.items;
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
/* --- PyConfig ---------------------------------------------- */
|
2019-03-01 11:25:19 -04:00
|
|
|
|
2020-11-05 13:12:33 -04:00
|
|
|
#define MAX_HASH_SEED 4294967295UL
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
static int
|
|
|
|
config_check_consistency(const PyConfig *config)
|
|
|
|
{
|
|
|
|
/* Check config consistency */
|
|
|
|
assert(config->isolated >= 0);
|
|
|
|
assert(config->use_environment >= 0);
|
|
|
|
assert(config->dev_mode >= 0);
|
|
|
|
assert(config->install_signal_handlers >= 0);
|
|
|
|
assert(config->use_hash_seed >= 0);
|
|
|
|
assert(config->hash_seed <= MAX_HASH_SEED);
|
|
|
|
assert(config->faulthandler >= 0);
|
|
|
|
assert(config->tracemalloc >= 0);
|
|
|
|
assert(config->import_time >= 0);
|
2021-12-02 06:43:37 -04:00
|
|
|
assert(config->code_debug_ranges >= 0);
|
2020-11-05 13:12:33 -04:00
|
|
|
assert(config->show_ref_count >= 0);
|
|
|
|
assert(config->dump_refs >= 0);
|
|
|
|
assert(config->malloc_stats >= 0);
|
|
|
|
assert(config->site_import >= 0);
|
|
|
|
assert(config->bytes_warning >= 0);
|
2021-03-29 00:28:14 -03:00
|
|
|
assert(config->warn_default_encoding >= 0);
|
2020-11-05 13:12:33 -04:00
|
|
|
assert(config->inspect >= 0);
|
|
|
|
assert(config->interactive >= 0);
|
|
|
|
assert(config->optimization_level >= 0);
|
|
|
|
assert(config->parser_debug >= 0);
|
|
|
|
assert(config->write_bytecode >= 0);
|
|
|
|
assert(config->verbose >= 0);
|
|
|
|
assert(config->quiet >= 0);
|
|
|
|
assert(config->user_site_directory >= 0);
|
|
|
|
assert(config->parse_argv >= 0);
|
|
|
|
assert(config->configure_c_stdio >= 0);
|
|
|
|
assert(config->buffered_stdio >= 0);
|
|
|
|
assert(_PyWideStringList_CheckConsistency(&config->orig_argv));
|
|
|
|
assert(_PyWideStringList_CheckConsistency(&config->argv));
|
|
|
|
/* sys.argv must be non-empty: empty argv is replaced with [''] */
|
|
|
|
assert(config->argv.length >= 1);
|
|
|
|
assert(_PyWideStringList_CheckConsistency(&config->xoptions));
|
|
|
|
assert(_PyWideStringList_CheckConsistency(&config->warnoptions));
|
|
|
|
assert(_PyWideStringList_CheckConsistency(&config->module_search_paths));
|
|
|
|
assert(config->module_search_paths_set >= 0);
|
|
|
|
assert(config->filesystem_encoding != NULL);
|
|
|
|
assert(config->filesystem_errors != NULL);
|
|
|
|
assert(config->stdio_encoding != NULL);
|
|
|
|
assert(config->stdio_errors != NULL);
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
assert(config->legacy_windows_stdio >= 0);
|
|
|
|
#endif
|
|
|
|
/* -c and -m options are exclusive */
|
|
|
|
assert(!(config->run_command != NULL && config->run_module != NULL));
|
|
|
|
assert(config->check_hash_pycs_mode != NULL);
|
|
|
|
assert(config->_install_importlib >= 0);
|
|
|
|
assert(config->pathconfig_warnings >= 0);
|
2022-05-05 20:34:11 -03:00
|
|
|
assert(config->_is_python_build >= 0);
|
|
|
|
assert(config->safe_path >= 0);
|
2022-10-03 17:55:45 -03:00
|
|
|
assert(config->int_max_str_digits >= 0);
|
2023-10-10 07:00:09 -03:00
|
|
|
// cpu_count can be -1 if the user doesn't override it.
|
|
|
|
assert(config->cpu_count != 0);
|
2022-05-05 20:34:11 -03:00
|
|
|
// config->use_frozen_modules is initialized later
|
|
|
|
// by _PyConfig_InitImportConfig().
|
2023-09-06 12:54:59 -03:00
|
|
|
#ifdef Py_STATS
|
|
|
|
assert(config->_pystats >= 0);
|
|
|
|
#endif
|
2020-11-05 13:12:33 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-09-27 23:28:35 -03:00
|
|
|
|
2018-08-01 12:56:14 -03:00
|
|
|
/* Free memory allocated in config, but don't clear all attributes */
|
|
|
|
void
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig_Clear(PyConfig *config)
|
2018-08-01 12:56:14 -03:00
|
|
|
{
|
|
|
|
#define CLEAR(ATTR) \
|
|
|
|
do { \
|
|
|
|
PyMem_RawFree(ATTR); \
|
|
|
|
ATTR = NULL; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
CLEAR(config->pycache_prefix);
|
2019-05-27 11:39:22 -03:00
|
|
|
CLEAR(config->pythonpath_env);
|
2018-08-01 12:56:14 -03:00
|
|
|
CLEAR(config->home);
|
|
|
|
CLEAR(config->program_name);
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
_PyWideStringList_Clear(&config->argv);
|
|
|
|
_PyWideStringList_Clear(&config->warnoptions);
|
|
|
|
_PyWideStringList_Clear(&config->xoptions);
|
|
|
|
_PyWideStringList_Clear(&config->module_search_paths);
|
|
|
|
config->module_search_paths_set = 0;
|
2021-09-28 15:18:28 -03:00
|
|
|
CLEAR(config->stdlib_dir);
|
2018-08-01 12:56:14 -03:00
|
|
|
|
|
|
|
CLEAR(config->executable);
|
2019-06-29 14:34:11 -03:00
|
|
|
CLEAR(config->base_executable);
|
2018-08-01 12:56:14 -03:00
|
|
|
CLEAR(config->prefix);
|
|
|
|
CLEAR(config->base_prefix);
|
|
|
|
CLEAR(config->exec_prefix);
|
|
|
|
CLEAR(config->base_exec_prefix);
|
2020-06-08 12:28:11 -03:00
|
|
|
CLEAR(config->platlibdir);
|
2023-10-02 16:59:05 -03:00
|
|
|
CLEAR(config->sys_path_0);
|
2018-08-29 06:47:29 -03:00
|
|
|
|
2018-08-29 08:25:36 -03:00
|
|
|
CLEAR(config->filesystem_encoding);
|
|
|
|
CLEAR(config->filesystem_errors);
|
2018-08-29 06:47:29 -03:00
|
|
|
CLEAR(config->stdio_encoding);
|
|
|
|
CLEAR(config->stdio_errors);
|
2019-03-05 20:44:31 -04:00
|
|
|
CLEAR(config->run_command);
|
|
|
|
CLEAR(config->run_module);
|
|
|
|
CLEAR(config->run_filename);
|
2019-05-02 00:51:56 -03:00
|
|
|
CLEAR(config->check_hash_pycs_mode);
|
2023-10-14 18:32:57 -03:00
|
|
|
#ifdef Py_DEBUG
|
|
|
|
CLEAR(config->run_presite);
|
|
|
|
#endif
|
2020-06-15 11:27:47 -03:00
|
|
|
|
2020-06-29 19:49:03 -03:00
|
|
|
_PyWideStringList_Clear(&config->orig_argv);
|
2018-08-01 12:56:14 -03:00
|
|
|
#undef CLEAR
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-01 07:06:16 -03:00
|
|
|
void
|
2019-05-27 11:39:22 -03:00
|
|
|
_PyConfig_InitCompatConfig(PyConfig *config)
|
2019-05-17 14:01:14 -03:00
|
|
|
{
|
2019-05-17 22:21:27 -03:00
|
|
|
memset(config, 0, sizeof(*config));
|
|
|
|
|
2019-05-22 18:58:50 -03:00
|
|
|
config->_config_init = (int)_PyConfig_INIT_COMPAT;
|
2019-05-17 22:21:27 -03:00
|
|
|
config->isolated = -1;
|
|
|
|
config->use_environment = -1;
|
|
|
|
config->dev_mode = -1;
|
|
|
|
config->install_signal_handlers = 1;
|
|
|
|
config->use_hash_seed = -1;
|
|
|
|
config->faulthandler = -1;
|
|
|
|
config->tracemalloc = -1;
|
2022-08-30 14:11:18 -03:00
|
|
|
config->perf_profiling = -1;
|
2019-05-27 11:39:22 -03:00
|
|
|
config->module_search_paths_set = 0;
|
2019-05-17 22:21:27 -03:00
|
|
|
config->parse_argv = 0;
|
|
|
|
config->site_import = -1;
|
|
|
|
config->bytes_warning = -1;
|
2021-03-29 00:28:14 -03:00
|
|
|
config->warn_default_encoding = 0;
|
2019-05-17 22:21:27 -03:00
|
|
|
config->inspect = -1;
|
|
|
|
config->interactive = -1;
|
|
|
|
config->optimization_level = -1;
|
|
|
|
config->parser_debug= -1;
|
|
|
|
config->write_bytecode = -1;
|
|
|
|
config->verbose = -1;
|
|
|
|
config->quiet = -1;
|
|
|
|
config->user_site_directory = -1;
|
|
|
|
config->configure_c_stdio = 0;
|
|
|
|
config->buffered_stdio = -1;
|
|
|
|
config->_install_importlib = 1;
|
|
|
|
config->check_hash_pycs_mode = NULL;
|
|
|
|
config->pathconfig_warnings = -1;
|
|
|
|
config->_init_main = 1;
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
config->legacy_windows_stdio = -1;
|
|
|
|
#endif
|
2022-05-03 19:20:13 -03:00
|
|
|
#ifdef Py_DEBUG
|
|
|
|
config->use_frozen_modules = 0;
|
|
|
|
#else
|
|
|
|
config->use_frozen_modules = 1;
|
|
|
|
#endif
|
2022-05-05 20:34:11 -03:00
|
|
|
config->safe_path = 0;
|
2022-10-03 17:55:45 -03:00
|
|
|
config->int_max_str_digits = -1;
|
2021-12-02 20:08:42 -04:00
|
|
|
config->_is_python_build = 0;
|
2021-12-02 06:43:37 -04:00
|
|
|
config->code_debug_ranges = 1;
|
2023-10-10 07:00:09 -03:00
|
|
|
config->cpu_count = -1;
|
2024-03-11 12:02:58 -03:00
|
|
|
#ifdef Py_GIL_DISABLED
|
|
|
|
config->enable_gil = _PyConfig_GIL_DEFAULT;
|
2024-11-04 15:13:32 -04:00
|
|
|
config->tlbc_enabled = 1;
|
2024-03-11 12:02:58 -03:00
|
|
|
#endif
|
2019-05-17 22:21:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-01 07:06:16 -03:00
|
|
|
static void
|
2019-05-27 11:39:22 -03:00
|
|
|
config_init_defaults(PyConfig *config)
|
2019-05-17 22:21:27 -03:00
|
|
|
{
|
2019-10-01 07:06:16 -03:00
|
|
|
_PyConfig_InitCompatConfig(config);
|
2019-05-17 22:21:27 -03:00
|
|
|
|
|
|
|
config->isolated = 0;
|
|
|
|
config->use_environment = 1;
|
|
|
|
config->site_import = 1;
|
|
|
|
config->bytes_warning = 0;
|
|
|
|
config->inspect = 0;
|
|
|
|
config->interactive = 0;
|
|
|
|
config->optimization_level = 0;
|
|
|
|
config->parser_debug= 0;
|
|
|
|
config->write_bytecode = 1;
|
|
|
|
config->verbose = 0;
|
|
|
|
config->quiet = 0;
|
|
|
|
config->user_site_directory = 1;
|
|
|
|
config->buffered_stdio = 1;
|
|
|
|
config->pathconfig_warnings = 1;
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
config->legacy_windows_stdio = 0;
|
|
|
|
#endif
|
2019-05-17 14:01:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-01 07:06:16 -03:00
|
|
|
void
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig_InitPythonConfig(PyConfig *config)
|
2019-05-17 14:01:14 -03:00
|
|
|
{
|
2019-10-01 07:06:16 -03:00
|
|
|
config_init_defaults(config);
|
2019-05-17 14:01:14 -03:00
|
|
|
|
2019-05-22 18:58:50 -03:00
|
|
|
config->_config_init = (int)_PyConfig_INIT_PYTHON;
|
2019-05-17 14:01:14 -03:00
|
|
|
config->configure_c_stdio = 1;
|
|
|
|
config->parse_argv = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-01 07:06:16 -03:00
|
|
|
void
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig_InitIsolatedConfig(PyConfig *config)
|
2019-05-17 14:01:14 -03:00
|
|
|
{
|
2019-10-01 07:06:16 -03:00
|
|
|
config_init_defaults(config);
|
2019-05-17 14:01:14 -03:00
|
|
|
|
2019-05-22 18:58:50 -03:00
|
|
|
config->_config_init = (int)_PyConfig_INIT_ISOLATED;
|
2019-05-17 14:01:14 -03:00
|
|
|
config->isolated = 1;
|
|
|
|
config->use_environment = 0;
|
2019-05-17 22:21:27 -03:00
|
|
|
config->user_site_directory = 0;
|
2019-05-17 14:01:14 -03:00
|
|
|
config->dev_mode = 0;
|
|
|
|
config->install_signal_handlers = 0;
|
|
|
|
config->use_hash_seed = 0;
|
|
|
|
config->tracemalloc = 0;
|
2022-08-30 14:11:18 -03:00
|
|
|
config->perf_profiling = 0;
|
2022-10-03 17:55:45 -03:00
|
|
|
config->int_max_str_digits = _PY_LONG_DEFAULT_MAX_STR_DIGITS;
|
2022-05-05 20:34:11 -03:00
|
|
|
config->safe_path = 1;
|
2019-05-17 14:01:14 -03:00
|
|
|
config->pathconfig_warnings = 0;
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
config->legacy_windows_stdio = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-01 10:22:52 -03:00
|
|
|
/* Copy str into *config_str (duplicate the string) */
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus
|
|
|
|
PyConfig_SetString(PyConfig *config, wchar_t **config_str, const wchar_t *str)
|
2019-05-01 10:22:52 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status = _Py_PreInitializeFromConfig(config, NULL);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-05-20 06:02:00 -03:00
|
|
|
}
|
|
|
|
|
2019-05-01 10:22:52 -03:00
|
|
|
wchar_t *str2;
|
|
|
|
if (str != NULL) {
|
|
|
|
str2 = _PyMem_RawWcsdup(str);
|
|
|
|
if (str2 == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_NO_MEMORY();
|
2019-05-01 10:22:52 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
str2 = NULL;
|
|
|
|
}
|
|
|
|
PyMem_RawFree(*config_str);
|
|
|
|
*config_str = str2;
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2019-05-01 10:22:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
|
|
|
config_set_bytes_string(PyConfig *config, wchar_t **config_str,
|
|
|
|
const char *str, const char *decode_err_msg)
|
2019-05-01 10:22:52 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status = _Py_PreInitializeFromConfig(config, NULL);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-05-02 16:25:34 -03:00
|
|
|
}
|
|
|
|
|
2019-05-01 10:22:52 -03:00
|
|
|
wchar_t *str2;
|
|
|
|
if (str != NULL) {
|
|
|
|
size_t len;
|
|
|
|
str2 = Py_DecodeLocale(str, &len);
|
|
|
|
if (str2 == NULL) {
|
|
|
|
if (len == (size_t)-2) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR(decode_err_msg);
|
2019-05-01 10:22:52 -03:00
|
|
|
}
|
|
|
|
else {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_NO_MEMORY();
|
2019-05-01 10:22:52 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
str2 = NULL;
|
|
|
|
}
|
|
|
|
PyMem_RawFree(*config_str);
|
|
|
|
*config_str = str2;
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2019-05-01 10:22:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
#define CONFIG_SET_BYTES_STR(config, config_str, str, NAME) \
|
|
|
|
config_set_bytes_string(config, config_str, str, "cannot decode " NAME)
|
2019-05-02 15:56:30 -03:00
|
|
|
|
|
|
|
|
2019-05-02 16:25:34 -03:00
|
|
|
/* Decode str using Py_DecodeLocale() and set the result into *config_str.
|
|
|
|
Pre-initialize Python if needed to ensure that encodings are properly
|
|
|
|
configured. */
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus
|
|
|
|
PyConfig_SetBytesString(PyConfig *config, wchar_t **config_str,
|
2020-10-30 21:02:09 -03:00
|
|
|
const char *str)
|
2019-05-01 10:22:52 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
return CONFIG_SET_BYTES_STR(config, config_str, str, "string");
|
2019-05-01 10:22:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-03 09:33:49 -03:00
|
|
|
static inline void*
|
|
|
|
config_get_spec_member(const PyConfig *config, const PyConfigSpec *spec)
|
|
|
|
{
|
|
|
|
return (char *)config + spec->offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void*
|
|
|
|
preconfig_get_spec_member(const PyPreConfig *preconfig, const PyConfigSpec *spec)
|
|
|
|
{
|
|
|
|
return (char *)preconfig + spec->offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus
|
|
|
|
_PyConfig_Copy(PyConfig *config, const PyConfig *config2)
|
2018-08-01 12:56:14 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyConfig_Clear(config);
|
2018-08-01 12:56:14 -03:00
|
|
|
|
2023-09-30 14:24:06 -03:00
|
|
|
PyStatus status;
|
|
|
|
const PyConfigSpec *spec = PYCONFIG_SPEC;
|
|
|
|
for (; spec->name != NULL; spec++) {
|
2024-09-03 09:33:49 -03:00
|
|
|
void *member = config_get_spec_member(config, spec);
|
|
|
|
const void *member2 = config_get_spec_member((PyConfig*)config2, spec);
|
2023-09-30 14:24:06 -03:00
|
|
|
switch (spec->type) {
|
|
|
|
case PyConfig_MEMBER_INT:
|
|
|
|
case PyConfig_MEMBER_UINT:
|
2024-03-06 05:29:27 -04:00
|
|
|
case PyConfig_MEMBER_BOOL:
|
2023-09-30 14:24:06 -03:00
|
|
|
{
|
|
|
|
*(int*)member = *(int*)member2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PyConfig_MEMBER_ULONG:
|
|
|
|
{
|
|
|
|
*(unsigned long*)member = *(unsigned long*)member2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PyConfig_MEMBER_WSTR:
|
|
|
|
case PyConfig_MEMBER_WSTR_OPT:
|
|
|
|
{
|
|
|
|
const wchar_t *str = *(const wchar_t**)member2;
|
|
|
|
status = PyConfig_SetString(config, (wchar_t**)member, str);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PyConfig_MEMBER_WSTR_LIST:
|
|
|
|
{
|
|
|
|
if (_PyWideStringList_Copy((PyWideStringList*)member,
|
|
|
|
(const PyWideStringList*)member2) < 0) {
|
|
|
|
return _PyStatus_NO_MEMORY();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
Py_UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-05 13:12:33 -04:00
|
|
|
PyObject *
|
|
|
|
_PyConfig_AsDict(const PyConfig *config)
|
2019-03-26 12:58:50 -03:00
|
|
|
{
|
2020-07-07 19:20:37 -03:00
|
|
|
PyObject *dict = PyDict_New();
|
2019-03-26 12:58:50 -03:00
|
|
|
if (dict == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-09-30 14:24:06 -03:00
|
|
|
const PyConfigSpec *spec = PYCONFIG_SPEC;
|
|
|
|
for (; spec->name != NULL; spec++) {
|
2024-09-02 18:25:08 -03:00
|
|
|
PyObject *obj = config_get(config, spec, 0);
|
2023-09-30 14:24:06 -03:00
|
|
|
if (obj == NULL) {
|
|
|
|
Py_DECREF(dict);
|
|
|
|
return NULL;
|
|
|
|
}
|
2024-09-02 18:25:08 -03:00
|
|
|
|
2023-09-30 14:24:06 -03:00
|
|
|
int res = PyDict_SetItemString(dict, spec->name, obj);
|
|
|
|
Py_DECREF(obj);
|
|
|
|
if (res < 0) {
|
|
|
|
Py_DECREF(dict);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 12:58:50 -03:00
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-05 13:12:33 -04:00
|
|
|
static void
|
|
|
|
config_dict_invalid_value(const char *name)
|
|
|
|
{
|
|
|
|
PyErr_Format(PyExc_ValueError, "invalid config value: %s", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
config_dict_get_int(PyObject *dict, const char *name, int *result)
|
|
|
|
{
|
|
|
|
PyObject *item = config_dict_get(dict, name);
|
|
|
|
if (item == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2023-08-24 20:01:30 -03:00
|
|
|
int value = PyLong_AsInt(item);
|
2023-08-23 18:12:08 -03:00
|
|
|
Py_DECREF(item);
|
2020-11-05 13:12:33 -04:00
|
|
|
if (value == -1 && PyErr_Occurred()) {
|
|
|
|
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
|
|
|
|
config_dict_invalid_type(name);
|
|
|
|
}
|
2020-11-24 08:07:32 -04:00
|
|
|
else if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
|
2020-11-05 13:12:33 -04:00
|
|
|
config_dict_invalid_value(name);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*result = value;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
config_dict_get_ulong(PyObject *dict, const char *name, unsigned long *result)
|
|
|
|
{
|
|
|
|
PyObject *item = config_dict_get(dict, name);
|
|
|
|
if (item == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
unsigned long value = PyLong_AsUnsignedLong(item);
|
2023-08-23 18:12:08 -03:00
|
|
|
Py_DECREF(item);
|
2020-11-05 13:12:33 -04:00
|
|
|
if (value == (unsigned long)-1 && PyErr_Occurred()) {
|
2020-11-24 08:07:32 -04:00
|
|
|
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
|
|
|
|
config_dict_invalid_type(name);
|
|
|
|
}
|
|
|
|
else if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
|
|
|
|
config_dict_invalid_value(name);
|
|
|
|
}
|
2020-11-05 13:12:33 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*result = value;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
config_dict_get_wstr(PyObject *dict, const char *name, PyConfig *config,
|
|
|
|
wchar_t **result)
|
|
|
|
{
|
|
|
|
PyObject *item = config_dict_get(dict, name);
|
|
|
|
if (item == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2023-08-23 18:12:08 -03:00
|
|
|
|
2020-11-05 13:12:33 -04:00
|
|
|
PyStatus status;
|
|
|
|
if (item == Py_None) {
|
|
|
|
status = PyConfig_SetString(config, result, NULL);
|
|
|
|
}
|
|
|
|
else if (!PyUnicode_Check(item)) {
|
|
|
|
config_dict_invalid_type(name);
|
2023-08-23 18:12:08 -03:00
|
|
|
goto error;
|
2020-11-05 13:12:33 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
wchar_t *wstr = PyUnicode_AsWideCharString(item, NULL);
|
|
|
|
if (wstr == NULL) {
|
2023-08-23 18:12:08 -03:00
|
|
|
goto error;
|
2020-11-05 13:12:33 -04:00
|
|
|
}
|
|
|
|
status = PyConfig_SetString(config, result, wstr);
|
|
|
|
PyMem_Free(wstr);
|
|
|
|
}
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
PyErr_NoMemory();
|
2023-08-23 18:12:08 -03:00
|
|
|
goto error;
|
2020-11-05 13:12:33 -04:00
|
|
|
}
|
2023-08-23 18:12:08 -03:00
|
|
|
Py_DECREF(item);
|
2020-11-05 13:12:33 -04:00
|
|
|
return 0;
|
2023-08-23 18:12:08 -03:00
|
|
|
|
|
|
|
error:
|
|
|
|
Py_DECREF(item);
|
|
|
|
return -1;
|
2020-11-05 13:12:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
config_dict_get_wstrlist(PyObject *dict, const char *name, PyConfig *config,
|
|
|
|
PyWideStringList *result)
|
|
|
|
{
|
|
|
|
PyObject *list = config_dict_get(dict, name);
|
|
|
|
if (list == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-09-02 18:25:08 -03:00
|
|
|
int is_list = PyList_CheckExact(list);
|
|
|
|
if (!is_list && !PyTuple_CheckExact(list)) {
|
2023-08-23 18:12:08 -03:00
|
|
|
Py_DECREF(list);
|
2020-11-05 13:12:33 -04:00
|
|
|
config_dict_invalid_type(name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyWideStringList wstrlist = _PyWideStringList_INIT;
|
2024-09-02 18:25:08 -03:00
|
|
|
Py_ssize_t len = is_list ? PyList_GET_SIZE(list) : PyTuple_GET_SIZE(list);
|
|
|
|
for (Py_ssize_t i=0; i < len; i++) {
|
|
|
|
PyObject *item = is_list ? PyList_GET_ITEM(list, i) : PyTuple_GET_ITEM(list, i);
|
2020-11-05 13:12:33 -04:00
|
|
|
|
|
|
|
if (item == Py_None) {
|
|
|
|
config_dict_invalid_value(name);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
else if (!PyUnicode_Check(item)) {
|
|
|
|
config_dict_invalid_type(name);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
wchar_t *wstr = PyUnicode_AsWideCharString(item, NULL);
|
|
|
|
if (wstr == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
PyStatus status = PyWideStringList_Append(&wstrlist, wstr);
|
|
|
|
PyMem_Free(wstr);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_PyWideStringList_Copy(result, &wstrlist) < 0) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
_PyWideStringList_Clear(&wstrlist);
|
2023-08-23 18:12:08 -03:00
|
|
|
Py_DECREF(list);
|
2020-11-05 13:12:33 -04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
_PyWideStringList_Clear(&wstrlist);
|
2023-08-23 18:12:08 -03:00
|
|
|
Py_DECREF(list);
|
2020-11-05 13:12:33 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-02 18:25:08 -03:00
|
|
|
static int
|
|
|
|
config_dict_get_xoptions(PyObject *dict, const char *name, PyConfig *config,
|
|
|
|
PyWideStringList *result)
|
|
|
|
{
|
|
|
|
PyObject *xoptions = config_dict_get(dict, name);
|
|
|
|
if (xoptions == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PyDict_CheckExact(xoptions)) {
|
|
|
|
Py_DECREF(xoptions);
|
|
|
|
config_dict_invalid_type(name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_ssize_t pos = 0;
|
|
|
|
PyObject *key, *value;
|
|
|
|
PyWideStringList wstrlist = _PyWideStringList_INIT;
|
|
|
|
while (PyDict_Next(xoptions, &pos, &key, &value)) {
|
|
|
|
PyObject *item;
|
|
|
|
|
|
|
|
if (value != Py_True) {
|
|
|
|
item = PyUnicode_FromFormat("%S=%S", key, value);
|
|
|
|
if (item == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
item = Py_NewRef(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t *wstr = PyUnicode_AsWideCharString(item, NULL);
|
|
|
|
Py_DECREF(item);
|
|
|
|
if (wstr == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyStatus status = PyWideStringList_Append(&wstrlist, wstr);
|
|
|
|
PyMem_Free(wstr);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_PyWideStringList_Copy(result, &wstrlist) < 0) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
_PyWideStringList_Clear(&wstrlist);
|
|
|
|
Py_DECREF(xoptions);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
_PyWideStringList_Clear(&wstrlist);
|
|
|
|
Py_DECREF(xoptions);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-05 13:12:33 -04:00
|
|
|
int
|
|
|
|
_PyConfig_FromDict(PyConfig *config, PyObject *dict)
|
|
|
|
{
|
|
|
|
if (!PyDict_Check(dict)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "dict expected");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-09-30 14:24:06 -03:00
|
|
|
const PyConfigSpec *spec = PYCONFIG_SPEC;
|
|
|
|
for (; spec->name != NULL; spec++) {
|
|
|
|
char *member = (char *)config + spec->offset;
|
|
|
|
switch (spec->type) {
|
|
|
|
case PyConfig_MEMBER_INT:
|
|
|
|
case PyConfig_MEMBER_UINT:
|
2024-03-06 05:29:27 -04:00
|
|
|
case PyConfig_MEMBER_BOOL:
|
2023-09-30 14:24:06 -03:00
|
|
|
{
|
|
|
|
int value;
|
|
|
|
if (config_dict_get_int(dict, spec->name, &value) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2024-03-06 05:29:27 -04:00
|
|
|
if (spec->type == PyConfig_MEMBER_BOOL
|
|
|
|
|| spec->type == PyConfig_MEMBER_UINT)
|
|
|
|
{
|
|
|
|
if (value < 0) {
|
|
|
|
config_dict_invalid_value(spec->name);
|
|
|
|
return -1;
|
|
|
|
}
|
2023-09-30 14:24:06 -03:00
|
|
|
}
|
|
|
|
*(int*)member = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PyConfig_MEMBER_ULONG:
|
|
|
|
{
|
|
|
|
if (config_dict_get_ulong(dict, spec->name,
|
|
|
|
(unsigned long*)member) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PyConfig_MEMBER_WSTR:
|
|
|
|
{
|
|
|
|
wchar_t **wstr = (wchar_t**)member;
|
|
|
|
if (config_dict_get_wstr(dict, spec->name, config, wstr) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (*wstr == NULL) {
|
|
|
|
config_dict_invalid_value(spec->name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PyConfig_MEMBER_WSTR_OPT:
|
|
|
|
{
|
|
|
|
wchar_t **wstr = (wchar_t**)member;
|
|
|
|
if (config_dict_get_wstr(dict, spec->name, config, wstr) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PyConfig_MEMBER_WSTR_LIST:
|
|
|
|
{
|
2024-09-02 18:25:08 -03:00
|
|
|
if (strcmp(spec->name, "xoptions") == 0) {
|
|
|
|
if (config_dict_get_xoptions(dict, spec->name, config,
|
|
|
|
(PyWideStringList*)member) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (config_dict_get_wstrlist(dict, spec->name, config,
|
|
|
|
(PyWideStringList*)member) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2023-09-30 14:24:06 -03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
Py_UNREACHABLE();
|
|
|
|
}
|
2020-11-05 13:12:33 -04:00
|
|
|
}
|
|
|
|
|
2023-09-30 14:24:06 -03:00
|
|
|
if (!(config->_config_init == _PyConfig_INIT_COMPAT
|
|
|
|
|| config->_config_init == _PyConfig_INIT_PYTHON
|
|
|
|
|| config->_config_init == _PyConfig_INIT_ISOLATED))
|
|
|
|
{
|
|
|
|
config_dict_invalid_value("_config_init");
|
2020-11-05 13:12:33 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-09-30 14:24:06 -03:00
|
|
|
if (config->hash_seed > MAX_HASH_SEED) {
|
|
|
|
config_dict_invalid_value("hash_seed");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-11-05 13:12:33 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-25 20:03:15 -03:00
|
|
|
static const char*
|
2019-05-27 11:39:22 -03:00
|
|
|
config_get_env(const PyConfig *config, const char *name)
|
2018-08-01 12:56:14 -03:00
|
|
|
{
|
2019-03-25 22:31:11 -03:00
|
|
|
return _Py_GetEnv(config->use_environment, name);
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-26 12:58:50 -03:00
|
|
|
/* Get a copy of the environment variable as wchar_t*.
|
|
|
|
Return 0 on success, but *dest can be NULL.
|
|
|
|
Return -1 on memory allocation failure. Return -2 on decoding error. */
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
|
|
|
config_get_env_dup(PyConfig *config,
|
|
|
|
wchar_t **dest,
|
|
|
|
wchar_t *wname, char *name,
|
|
|
|
const char *decode_err_msg)
|
2018-08-01 12:56:14 -03:00
|
|
|
{
|
2019-05-01 10:22:52 -03:00
|
|
|
assert(*dest == NULL);
|
2019-03-25 22:31:11 -03:00
|
|
|
assert(config->use_environment >= 0);
|
2018-08-01 12:56:14 -03:00
|
|
|
|
2019-03-25 22:31:11 -03:00
|
|
|
if (!config->use_environment) {
|
2018-08-01 12:56:14 -03:00
|
|
|
*dest = NULL;
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
const wchar_t *var = _wgetenv(wname);
|
|
|
|
if (!var || var[0] == '\0') {
|
|
|
|
*dest = NULL;
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
return PyConfig_SetString(config, dest, var);
|
2018-08-01 12:56:14 -03:00
|
|
|
#else
|
|
|
|
const char *var = getenv(name);
|
|
|
|
if (!var || var[0] == '\0') {
|
|
|
|
*dest = NULL;
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
return config_set_bytes_string(config, dest, var, decode_err_msg);
|
2018-08-01 12:56:14 -03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-01 10:22:52 -03:00
|
|
|
#define CONFIG_GET_ENV_DUP(CONFIG, DEST, WNAME, NAME) \
|
2019-05-27 11:39:22 -03:00
|
|
|
config_get_env_dup(CONFIG, DEST, WNAME, NAME, "cannot decode " NAME)
|
2019-05-01 10:22:52 -03:00
|
|
|
|
|
|
|
|
2019-03-26 12:58:50 -03:00
|
|
|
static void
|
2019-05-27 11:39:22 -03:00
|
|
|
config_get_global_vars(PyConfig *config)
|
2018-08-01 12:56:14 -03:00
|
|
|
{
|
2022-06-17 11:12:00 -03:00
|
|
|
_Py_COMP_DIAG_PUSH
|
|
|
|
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
|
2019-05-22 18:58:50 -03:00
|
|
|
if (config->_config_init != _PyConfig_INIT_COMPAT) {
|
|
|
|
/* Python and Isolated configuration ignore global variables */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-01 12:56:14 -03:00
|
|
|
#define COPY_FLAG(ATTR, VALUE) \
|
|
|
|
if (config->ATTR == -1) { \
|
|
|
|
config->ATTR = VALUE; \
|
|
|
|
}
|
|
|
|
#define COPY_NOT_FLAG(ATTR, VALUE) \
|
|
|
|
if (config->ATTR == -1) { \
|
|
|
|
config->ATTR = !(VALUE); \
|
|
|
|
}
|
|
|
|
|
2019-03-25 22:31:11 -03:00
|
|
|
COPY_FLAG(isolated, Py_IsolatedFlag);
|
|
|
|
COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag);
|
2018-08-01 12:56:14 -03:00
|
|
|
COPY_FLAG(bytes_warning, Py_BytesWarningFlag);
|
|
|
|
COPY_FLAG(inspect, Py_InspectFlag);
|
|
|
|
COPY_FLAG(interactive, Py_InteractiveFlag);
|
|
|
|
COPY_FLAG(optimization_level, Py_OptimizeFlag);
|
|
|
|
COPY_FLAG(parser_debug, Py_DebugFlag);
|
|
|
|
COPY_FLAG(verbose, Py_VerboseFlag);
|
|
|
|
COPY_FLAG(quiet, Py_QuietFlag);
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
COPY_FLAG(legacy_windows_stdio, Py_LegacyWindowsStdioFlag);
|
|
|
|
#endif
|
2019-05-16 12:38:16 -03:00
|
|
|
COPY_NOT_FLAG(pathconfig_warnings, Py_FrozenFlag);
|
2018-08-01 12:56:14 -03:00
|
|
|
|
|
|
|
COPY_NOT_FLAG(buffered_stdio, Py_UnbufferedStdioFlag);
|
|
|
|
COPY_NOT_FLAG(site_import, Py_NoSiteFlag);
|
|
|
|
COPY_NOT_FLAG(write_bytecode, Py_DontWriteBytecodeFlag);
|
|
|
|
COPY_NOT_FLAG(user_site_directory, Py_NoUserSiteDirectory);
|
|
|
|
|
|
|
|
#undef COPY_FLAG
|
|
|
|
#undef COPY_NOT_FLAG
|
2022-06-17 11:12:00 -03:00
|
|
|
_Py_COMP_DIAG_POP
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Set Py_xxx global configuration variables from 'config' configuration. */
|
2019-03-26 12:58:50 -03:00
|
|
|
static void
|
2019-05-27 11:39:22 -03:00
|
|
|
config_set_global_vars(const PyConfig *config)
|
2018-08-01 12:56:14 -03:00
|
|
|
{
|
2022-06-17 11:12:00 -03:00
|
|
|
_Py_COMP_DIAG_PUSH
|
|
|
|
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
|
2018-08-01 12:56:14 -03:00
|
|
|
#define COPY_FLAG(ATTR, VAR) \
|
|
|
|
if (config->ATTR != -1) { \
|
|
|
|
VAR = config->ATTR; \
|
|
|
|
}
|
|
|
|
#define COPY_NOT_FLAG(ATTR, VAR) \
|
|
|
|
if (config->ATTR != -1) { \
|
|
|
|
VAR = !config->ATTR; \
|
|
|
|
}
|
|
|
|
|
2019-03-25 22:31:11 -03:00
|
|
|
COPY_FLAG(isolated, Py_IsolatedFlag);
|
|
|
|
COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag);
|
2018-08-01 12:56:14 -03:00
|
|
|
COPY_FLAG(bytes_warning, Py_BytesWarningFlag);
|
|
|
|
COPY_FLAG(inspect, Py_InspectFlag);
|
|
|
|
COPY_FLAG(interactive, Py_InteractiveFlag);
|
|
|
|
COPY_FLAG(optimization_level, Py_OptimizeFlag);
|
|
|
|
COPY_FLAG(parser_debug, Py_DebugFlag);
|
|
|
|
COPY_FLAG(verbose, Py_VerboseFlag);
|
|
|
|
COPY_FLAG(quiet, Py_QuietFlag);
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
COPY_FLAG(legacy_windows_stdio, Py_LegacyWindowsStdioFlag);
|
|
|
|
#endif
|
2019-05-16 12:38:16 -03:00
|
|
|
COPY_NOT_FLAG(pathconfig_warnings, Py_FrozenFlag);
|
2018-08-01 12:56:14 -03:00
|
|
|
|
|
|
|
COPY_NOT_FLAG(buffered_stdio, Py_UnbufferedStdioFlag);
|
|
|
|
COPY_NOT_FLAG(site_import, Py_NoSiteFlag);
|
|
|
|
COPY_NOT_FLAG(write_bytecode, Py_DontWriteBytecodeFlag);
|
|
|
|
COPY_NOT_FLAG(user_site_directory, Py_NoUserSiteDirectory);
|
|
|
|
|
|
|
|
/* Random or non-zero hash seed */
|
|
|
|
Py_HashRandomizationFlag = (config->use_hash_seed == 0 ||
|
|
|
|
config->hash_seed != 0);
|
|
|
|
|
|
|
|
#undef COPY_FLAG
|
|
|
|
#undef COPY_NOT_FLAG
|
2022-06-17 11:12:00 -03:00
|
|
|
_Py_COMP_DIAG_POP
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const wchar_t*
|
2019-05-27 11:39:22 -03:00
|
|
|
config_get_xoption(const PyConfig *config, wchar_t *name)
|
2018-08-01 12:56:14 -03:00
|
|
|
{
|
2019-03-15 11:08:05 -03:00
|
|
|
return _Py_get_xoption(&config->xoptions, name);
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
|
2021-09-14 20:31:45 -03:00
|
|
|
static const wchar_t*
|
|
|
|
config_get_xoption_value(const PyConfig *config, wchar_t *name)
|
|
|
|
{
|
|
|
|
const wchar_t *xoption = config_get_xoption(config, name);
|
|
|
|
if (xoption == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
const wchar_t *sep = wcschr(xoption, L'=');
|
|
|
|
return sep ? sep + 1 : L"";
|
|
|
|
}
|
|
|
|
|
2018-08-01 12:56:14 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
|
|
|
config_init_hash_seed(PyConfig *config)
|
2018-08-01 12:56:14 -03:00
|
|
|
{
|
2022-04-20 14:26:40 -03:00
|
|
|
static_assert(sizeof(_Py_HashSecret_t) == sizeof(_Py_HashSecret.uc),
|
|
|
|
"_Py_HashSecret_t has wrong size");
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
const char *seed_text = config_get_env(config, "PYTHONHASHSEED");
|
2018-08-01 12:56:14 -03:00
|
|
|
|
|
|
|
/* Convert a text seed to a numeric one */
|
|
|
|
if (seed_text && strcmp(seed_text, "random") != 0) {
|
|
|
|
const char *endptr = seed_text;
|
|
|
|
unsigned long seed;
|
|
|
|
errno = 0;
|
|
|
|
seed = strtoul(seed_text, (char **)&endptr, 10);
|
|
|
|
if (*endptr != '\0'
|
2020-11-05 13:12:33 -04:00
|
|
|
|| seed > MAX_HASH_SEED
|
2018-08-01 12:56:14 -03:00
|
|
|
|| (errno == ERANGE && seed == ULONG_MAX))
|
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("PYTHONHASHSEED must be \"random\" "
|
2019-05-01 00:35:33 -03:00
|
|
|
"or an integer in range [0; 4294967295]");
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
/* Use a specific hash */
|
|
|
|
config->use_hash_seed = 1;
|
|
|
|
config->hash_seed = seed;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Use a random hash */
|
|
|
|
config->use_hash_seed = 0;
|
|
|
|
config->hash_seed = 0;
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
config_wstr_to_int(const wchar_t *wstr, int *result)
|
|
|
|
{
|
|
|
|
const wchar_t *endptr = wstr;
|
|
|
|
errno = 0;
|
|
|
|
long value = wcstol(wstr, (wchar_t **)&endptr, 10);
|
|
|
|
if (*endptr != '\0' || errno == ERANGE) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (value < INT_MIN || value > INT_MAX) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = (int)value;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-03-11 12:02:58 -03:00
|
|
|
static PyStatus
|
|
|
|
config_read_gil(PyConfig *config, size_t len, wchar_t first_char)
|
|
|
|
{
|
|
|
|
if (len == 1 && first_char == L'0') {
|
2024-09-05 20:53:47 -03:00
|
|
|
#ifdef Py_GIL_DISABLED
|
2024-03-11 12:02:58 -03:00
|
|
|
config->enable_gil = _PyConfig_GIL_DISABLE;
|
2024-09-05 20:53:47 -03:00
|
|
|
#else
|
|
|
|
return _PyStatus_ERR("Disabling the GIL is not supported by this build");
|
|
|
|
#endif
|
2024-03-11 12:02:58 -03:00
|
|
|
}
|
|
|
|
else if (len == 1 && first_char == L'1') {
|
2024-09-05 20:53:47 -03:00
|
|
|
#ifdef Py_GIL_DISABLED
|
2024-03-11 12:02:58 -03:00
|
|
|
config->enable_gil = _PyConfig_GIL_ENABLE;
|
2024-09-05 20:53:47 -03:00
|
|
|
#else
|
|
|
|
return _PyStatus_OK();
|
|
|
|
#endif
|
2024-03-11 12:02:58 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return _PyStatus_ERR("PYTHON_GIL / -X gil must be \"0\" or \"1\"");
|
|
|
|
}
|
|
|
|
return _PyStatus_OK();
|
|
|
|
}
|
2018-08-01 12:56:14 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
|
|
|
config_read_env_vars(PyConfig *config)
|
2018-08-01 12:56:14 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2019-03-25 22:31:11 -03:00
|
|
|
int use_env = config->use_environment;
|
2019-03-05 07:32:09 -04:00
|
|
|
|
2018-08-01 12:56:14 -03:00
|
|
|
/* Get environment variables */
|
2019-03-25 20:03:15 -03:00
|
|
|
_Py_get_env_flag(use_env, &config->parser_debug, "PYTHONDEBUG");
|
|
|
|
_Py_get_env_flag(use_env, &config->verbose, "PYTHONVERBOSE");
|
|
|
|
_Py_get_env_flag(use_env, &config->optimization_level, "PYTHONOPTIMIZE");
|
|
|
|
_Py_get_env_flag(use_env, &config->inspect, "PYTHONINSPECT");
|
2018-08-01 12:56:14 -03:00
|
|
|
|
|
|
|
int dont_write_bytecode = 0;
|
2019-03-25 20:03:15 -03:00
|
|
|
_Py_get_env_flag(use_env, &dont_write_bytecode, "PYTHONDONTWRITEBYTECODE");
|
2018-08-01 12:56:14 -03:00
|
|
|
if (dont_write_bytecode) {
|
|
|
|
config->write_bytecode = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int no_user_site_directory = 0;
|
2019-03-25 20:03:15 -03:00
|
|
|
_Py_get_env_flag(use_env, &no_user_site_directory, "PYTHONNOUSERSITE");
|
2018-08-01 12:56:14 -03:00
|
|
|
if (no_user_site_directory) {
|
|
|
|
config->user_site_directory = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int unbuffered_stdio = 0;
|
2019-03-25 20:03:15 -03:00
|
|
|
_Py_get_env_flag(use_env, &unbuffered_stdio, "PYTHONUNBUFFERED");
|
2018-08-01 12:56:14 -03:00
|
|
|
if (unbuffered_stdio) {
|
|
|
|
config->buffered_stdio = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MS_WINDOWS
|
2019-03-25 20:03:15 -03:00
|
|
|
_Py_get_env_flag(use_env, &config->legacy_windows_stdio,
|
2020-11-01 18:07:23 -04:00
|
|
|
"PYTHONLEGACYWINDOWSSTDIO");
|
2018-08-01 12:56:14 -03:00
|
|
|
#endif
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
if (config_get_env(config, "PYTHONDUMPREFS")) {
|
2018-08-01 12:56:14 -03:00
|
|
|
config->dump_refs = 1;
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
if (config_get_env(config, "PYTHONMALLOCSTATS")) {
|
2018-08-01 12:56:14 -03:00
|
|
|
config->malloc_stats = 1;
|
|
|
|
}
|
|
|
|
|
2021-08-17 12:52:50 -03:00
|
|
|
if (config->dump_refs_file == NULL) {
|
|
|
|
status = CONFIG_GET_ENV_DUP(config, &config->dump_refs_file,
|
|
|
|
L"PYTHONDUMPREFSFILE", "PYTHONDUMPREFSFILE");
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
if (config->pythonpath_env == NULL) {
|
|
|
|
status = CONFIG_GET_ENV_DUP(config, &config->pythonpath_env,
|
|
|
|
L"PYTHONPATH", "PYTHONPATH");
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-03-19 23:11:38 -03:00
|
|
|
}
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
|
2020-06-08 12:28:11 -03:00
|
|
|
if(config->platlibdir == NULL) {
|
|
|
|
status = CONFIG_GET_ENV_DUP(config, &config->platlibdir,
|
|
|
|
L"PYTHONPLATLIBDIR", "PYTHONPLATLIBDIR");
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 12:56:14 -03:00
|
|
|
if (config->use_hash_seed < 0) {
|
2019-05-27 11:39:22 -03:00
|
|
|
status = config_init_hash_seed(config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 20:34:11 -03:00
|
|
|
if (config_get_env(config, "PYTHONSAFEPATH")) {
|
|
|
|
config->safe_path = 1;
|
|
|
|
}
|
|
|
|
|
2024-03-11 12:02:58 -03:00
|
|
|
const char *gil = config_get_env(config, "PYTHON_GIL");
|
|
|
|
if (gil != NULL) {
|
|
|
|
size_t len = strlen(gil);
|
|
|
|
status = config_read_gil(config, len, gil[0]);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
|
2023-10-10 07:00:09 -03:00
|
|
|
static PyStatus
|
|
|
|
config_init_cpu_count(PyConfig *config)
|
|
|
|
{
|
|
|
|
const char *env = config_get_env(config, "PYTHON_CPU_COUNT");
|
|
|
|
if (env) {
|
|
|
|
int cpu_count = -1;
|
|
|
|
if (strcmp(env, "default") == 0) {
|
|
|
|
cpu_count = -1;
|
|
|
|
}
|
|
|
|
else if (_Py_str_to_int(env, &cpu_count) < 0 || cpu_count < 1) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
config->cpu_count = cpu_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
const wchar_t *xoption = config_get_xoption(config, L"cpu_count");
|
|
|
|
if (xoption) {
|
|
|
|
int cpu_count = -1;
|
|
|
|
const wchar_t *sep = wcschr(xoption, L'=');
|
|
|
|
if (sep) {
|
|
|
|
if (wcscmp(sep + 1, L"default") == 0) {
|
|
|
|
cpu_count = -1;
|
|
|
|
}
|
|
|
|
else if (config_wstr_to_int(sep + 1, &cpu_count) < 0 || cpu_count < 1) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
config->cpu_count = cpu_count;
|
|
|
|
}
|
|
|
|
return _PyStatus_OK();
|
|
|
|
|
|
|
|
error:
|
|
|
|
return _PyStatus_ERR("-X cpu_count=n option: n is missing or an invalid number, "
|
|
|
|
"n must be greater than 0");
|
|
|
|
}
|
|
|
|
|
2024-11-04 15:13:32 -04:00
|
|
|
static PyStatus
|
|
|
|
config_init_tlbc(PyConfig *config)
|
|
|
|
{
|
|
|
|
#ifdef Py_GIL_DISABLED
|
|
|
|
const char *env = config_get_env(config, "PYTHON_TLBC");
|
|
|
|
if (env) {
|
|
|
|
int enabled;
|
|
|
|
if (_Py_str_to_int(env, &enabled) < 0 || (enabled < 0) || (enabled > 1)) {
|
|
|
|
return _PyStatus_ERR(
|
|
|
|
"PYTHON_TLBC=N: N is missing or invalid");
|
|
|
|
}
|
|
|
|
config->tlbc_enabled = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
const wchar_t *xoption = config_get_xoption(config, L"tlbc");
|
|
|
|
if (xoption) {
|
|
|
|
int enabled;
|
|
|
|
const wchar_t *sep = wcschr(xoption, L'=');
|
|
|
|
if (!sep || (config_wstr_to_int(sep + 1, &enabled) < 0) || (enabled < 0) || (enabled > 1)) {
|
|
|
|
return _PyStatus_ERR(
|
|
|
|
"-X tlbc=n: n is missing or invalid");
|
|
|
|
}
|
|
|
|
config->tlbc_enabled = enabled;
|
|
|
|
}
|
|
|
|
return _PyStatus_OK();
|
|
|
|
#else
|
|
|
|
return _PyStatus_OK();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-08-30 14:11:18 -03:00
|
|
|
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;
|
|
|
|
}
|
2024-05-07 08:47:21 -03:00
|
|
|
env = config_get_env(config, "PYTHON_PERF_JIT_SUPPORT");
|
2024-05-04 22:07:29 -03:00
|
|
|
if (env) {
|
|
|
|
if (_Py_str_to_int(env, &active) != 0) {
|
|
|
|
active = 0;
|
|
|
|
}
|
|
|
|
if (active) {
|
|
|
|
config->perf_profiling = 2;
|
|
|
|
}
|
|
|
|
}
|
2024-05-07 08:47:21 -03:00
|
|
|
xoption = config_get_xoption(config, L"perf_jit");
|
2024-05-04 22:07:29 -03:00
|
|
|
if (xoption) {
|
|
|
|
config->perf_profiling = 2;
|
|
|
|
}
|
|
|
|
|
2022-08-30 14:11:18 -03:00
|
|
|
return _PyStatus_OK();
|
|
|
|
|
|
|
|
}
|
2018-08-01 12:56:14 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
|
|
|
config_init_tracemalloc(PyConfig *config)
|
2018-08-01 12:56:14 -03:00
|
|
|
{
|
|
|
|
int nframe;
|
|
|
|
int valid;
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
const char *env = config_get_env(config, "PYTHONTRACEMALLOC");
|
2018-08-01 12:56:14 -03:00
|
|
|
if (env) {
|
2019-03-05 07:32:09 -04:00
|
|
|
if (!_Py_str_to_int(env, &nframe)) {
|
2018-08-01 12:56:14 -03:00
|
|
|
valid = (nframe >= 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
valid = 0;
|
|
|
|
}
|
|
|
|
if (!valid) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("PYTHONTRACEMALLOC: invalid number of frames");
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
config->tracemalloc = nframe;
|
|
|
|
}
|
|
|
|
|
|
|
|
const wchar_t *xoption = config_get_xoption(config, L"tracemalloc");
|
|
|
|
if (xoption) {
|
|
|
|
const wchar_t *sep = wcschr(xoption, L'=');
|
|
|
|
if (sep) {
|
|
|
|
if (!config_wstr_to_int(sep + 1, &nframe)) {
|
|
|
|
valid = (nframe >= 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
valid = 0;
|
|
|
|
}
|
|
|
|
if (!valid) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_ERR("-X tracemalloc=NFRAME: "
|
|
|
|
"invalid number of frames");
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* -X tracemalloc behaves as -X tracemalloc=1 */
|
|
|
|
nframe = 1;
|
|
|
|
}
|
|
|
|
config->tracemalloc = nframe;
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
|
2022-09-02 13:35:08 -03:00
|
|
|
static PyStatus
|
|
|
|
config_init_int_max_str_digits(PyConfig *config)
|
|
|
|
{
|
|
|
|
int maxdigits;
|
|
|
|
|
|
|
|
const char *env = config_get_env(config, "PYTHONINTMAXSTRDIGITS");
|
|
|
|
if (env) {
|
2022-10-03 17:55:45 -03:00
|
|
|
bool valid = 0;
|
2022-09-02 13:35:08 -03:00
|
|
|
if (!_Py_str_to_int(env, &maxdigits)) {
|
|
|
|
valid = ((maxdigits == 0) || (maxdigits >= _PY_LONG_MAX_STR_DIGITS_THRESHOLD));
|
|
|
|
}
|
|
|
|
if (!valid) {
|
|
|
|
#define STRINGIFY(VAL) _STRINGIFY(VAL)
|
|
|
|
#define _STRINGIFY(VAL) #VAL
|
|
|
|
return _PyStatus_ERR(
|
|
|
|
"PYTHONINTMAXSTRDIGITS: invalid limit; must be >= "
|
|
|
|
STRINGIFY(_PY_LONG_MAX_STR_DIGITS_THRESHOLD)
|
|
|
|
" or 0 for unlimited.");
|
|
|
|
}
|
2022-10-03 17:55:45 -03:00
|
|
|
config->int_max_str_digits = maxdigits;
|
2022-09-02 13:35:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
const wchar_t *xoption = config_get_xoption(config, L"int_max_str_digits");
|
|
|
|
if (xoption) {
|
|
|
|
const wchar_t *sep = wcschr(xoption, L'=');
|
2022-10-03 17:55:45 -03:00
|
|
|
bool valid = 0;
|
2022-09-02 13:35:08 -03:00
|
|
|
if (sep) {
|
|
|
|
if (!config_wstr_to_int(sep + 1, &maxdigits)) {
|
|
|
|
valid = ((maxdigits == 0) || (maxdigits >= _PY_LONG_MAX_STR_DIGITS_THRESHOLD));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!valid) {
|
|
|
|
return _PyStatus_ERR(
|
|
|
|
"-X int_max_str_digits: invalid limit; must be >= "
|
|
|
|
STRINGIFY(_PY_LONG_MAX_STR_DIGITS_THRESHOLD)
|
|
|
|
" or 0 for unlimited.");
|
|
|
|
#undef _STRINGIFY
|
|
|
|
#undef STRINGIFY
|
|
|
|
}
|
2022-10-03 17:55:45 -03:00
|
|
|
config->int_max_str_digits = maxdigits;
|
|
|
|
}
|
|
|
|
if (config->int_max_str_digits < 0) {
|
|
|
|
config->int_max_str_digits = _PY_LONG_DEFAULT_MAX_STR_DIGITS;
|
2022-09-02 13:35:08 -03:00
|
|
|
}
|
|
|
|
return _PyStatus_OK();
|
|
|
|
}
|
2018-08-01 12:56:14 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
|
|
|
config_init_pycache_prefix(PyConfig *config)
|
2018-08-01 12:56:14 -03:00
|
|
|
{
|
|
|
|
assert(config->pycache_prefix == NULL);
|
|
|
|
|
|
|
|
const wchar_t *xoption = config_get_xoption(config, L"pycache_prefix");
|
|
|
|
if (xoption) {
|
|
|
|
const wchar_t *sep = wcschr(xoption, L'=');
|
|
|
|
if (sep && wcslen(sep) > 1) {
|
|
|
|
config->pycache_prefix = _PyMem_RawWcsdup(sep + 1);
|
|
|
|
if (config->pycache_prefix == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_NO_MEMORY();
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2019-05-01 10:22:52 -03:00
|
|
|
// PYTHONPYCACHEPREFIX env var ignored
|
|
|
|
// if "-X pycache_prefix=" option is used
|
2018-08-01 12:56:14 -03:00
|
|
|
config->pycache_prefix = NULL;
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
|
2019-05-01 10:22:52 -03:00
|
|
|
return CONFIG_GET_ENV_DUP(config, &config->pycache_prefix,
|
|
|
|
L"PYTHONPYCACHEPREFIX",
|
|
|
|
"PYTHONPYCACHEPREFIX");
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-14 18:32:57 -03:00
|
|
|
#ifdef Py_DEBUG
|
|
|
|
static PyStatus
|
|
|
|
config_init_run_presite(PyConfig *config)
|
|
|
|
{
|
|
|
|
assert(config->run_presite == NULL);
|
|
|
|
|
|
|
|
const wchar_t *xoption = config_get_xoption(config, L"presite");
|
|
|
|
if (xoption) {
|
|
|
|
const wchar_t *sep = wcschr(xoption, L'=');
|
|
|
|
if (sep && wcslen(sep) > 1) {
|
|
|
|
config->run_presite = _PyMem_RawWcsdup(sep + 1);
|
|
|
|
if (config->run_presite == NULL) {
|
|
|
|
return _PyStatus_NO_MEMORY();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// PYTHON_PRESITE env var ignored
|
|
|
|
// if "-X presite=" option is used
|
|
|
|
config->run_presite = NULL;
|
|
|
|
}
|
|
|
|
return _PyStatus_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
return CONFIG_GET_ENV_DUP(config, &config->run_presite,
|
|
|
|
L"PYTHON_PRESITE",
|
|
|
|
"PYTHON_PRESITE");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
|
|
|
config_read_complex_options(PyConfig *config)
|
2018-08-01 12:56:14 -03:00
|
|
|
{
|
|
|
|
/* More complex options configured by env var and -X option */
|
|
|
|
if (config->faulthandler < 0) {
|
2019-05-27 11:39:22 -03:00
|
|
|
if (config_get_env(config, "PYTHONFAULTHANDLER")
|
2018-08-01 12:56:14 -03:00
|
|
|
|| config_get_xoption(config, L"faulthandler")) {
|
|
|
|
config->faulthandler = 1;
|
|
|
|
}
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
if (config_get_env(config, "PYTHONPROFILEIMPORTTIME")
|
2018-08-01 12:56:14 -03:00
|
|
|
|| config_get_xoption(config, L"importtime")) {
|
|
|
|
config->import_time = 1;
|
|
|
|
}
|
|
|
|
|
2021-07-07 16:07:12 -03:00
|
|
|
if (config_get_env(config, "PYTHONNODEBUGRANGES")
|
|
|
|
|| config_get_xoption(config, L"no_debug_ranges")) {
|
2021-12-02 06:43:37 -04:00
|
|
|
config->code_debug_ranges = 0;
|
2021-07-07 16:07:12 -03:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2018-08-01 12:56:14 -03:00
|
|
|
if (config->tracemalloc < 0) {
|
2019-05-27 11:39:22 -03:00
|
|
|
status = config_init_tracemalloc(config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
}
|
2022-09-02 13:35:08 -03:00
|
|
|
|
2022-08-30 14:11:18 -03:00
|
|
|
if (config->perf_profiling < 0) {
|
|
|
|
status = config_init_perf_profiling(config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
2018-08-01 12:56:14 -03:00
|
|
|
|
2022-10-03 17:55:45 -03:00
|
|
|
if (config->int_max_str_digits < 0) {
|
2022-09-02 13:35:08 -03:00
|
|
|
status = config_init_int_max_str_digits(config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-10 07:00:09 -03:00
|
|
|
if (config->cpu_count < 0) {
|
|
|
|
status = config_init_cpu_count(config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 12:56:14 -03:00
|
|
|
if (config->pycache_prefix == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
status = config_init_pycache_prefix(config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
}
|
2023-10-14 18:32:57 -03:00
|
|
|
|
|
|
|
#ifdef Py_DEBUG
|
|
|
|
if (config->run_presite == NULL) {
|
|
|
|
status = config_init_run_presite(config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2024-11-04 15:13:32 -04:00
|
|
|
status = config_init_tlbc(config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-02 15:56:30 -03:00
|
|
|
static const wchar_t *
|
2020-10-30 21:02:09 -03:00
|
|
|
config_get_stdio_errors(const PyPreConfig *preconfig)
|
2018-08-29 06:47:29 -03:00
|
|
|
{
|
2020-10-30 21:02:09 -03:00
|
|
|
if (preconfig->utf8_mode) {
|
|
|
|
/* UTF-8 Mode uses UTF-8/surrogateescape */
|
|
|
|
return L"surrogateescape";
|
|
|
|
}
|
|
|
|
|
2018-08-29 06:47:29 -03:00
|
|
|
#ifndef MS_WINDOWS
|
|
|
|
const char *loc = setlocale(LC_CTYPE, NULL);
|
|
|
|
if (loc != NULL) {
|
|
|
|
/* surrogateescape is the default in the legacy C and POSIX locales */
|
|
|
|
if (strcmp(loc, "C") == 0 || strcmp(loc, "POSIX") == 0) {
|
2019-05-02 15:56:30 -03:00
|
|
|
return L"surrogateescape";
|
2018-08-29 06:47:29 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef PY_COERCE_C_LOCALE
|
|
|
|
/* surrogateescape is the default in locale coercion target locales */
|
|
|
|
if (_Py_IsLocaleCoercionTarget(loc)) {
|
2019-05-02 15:56:30 -03:00
|
|
|
return L"surrogateescape";
|
2018-08-29 06:47:29 -03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-05-02 15:56:30 -03:00
|
|
|
return L"strict";
|
2018-08-29 06:47:29 -03:00
|
|
|
#else
|
|
|
|
/* On Windows, always use surrogateescape by default */
|
2019-05-02 15:56:30 -03:00
|
|
|
return L"surrogateescape";
|
2018-08-29 06:47:29 -03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-01 15:59:35 -04:00
|
|
|
// See also config_get_fs_encoding()
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
2020-10-30 21:02:09 -03:00
|
|
|
config_get_locale_encoding(PyConfig *config, const PyPreConfig *preconfig,
|
|
|
|
wchar_t **locale_encoding)
|
2018-08-29 06:47:29 -03:00
|
|
|
{
|
2022-04-08 21:54:54 -03:00
|
|
|
wchar_t *encoding;
|
|
|
|
if (preconfig->utf8_mode) {
|
|
|
|
encoding = _PyMem_RawWcsdup(L"utf-8");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
encoding = _Py_GetLocaleEncoding();
|
|
|
|
}
|
2020-11-01 15:59:35 -04:00
|
|
|
if (encoding == NULL) {
|
2020-11-01 18:07:23 -04:00
|
|
|
return _PyStatus_NO_MEMORY();
|
2018-08-29 06:47:29 -03:00
|
|
|
}
|
2020-11-01 15:59:35 -04:00
|
|
|
PyStatus status = PyConfig_SetString(config, locale_encoding, encoding);
|
|
|
|
PyMem_RawFree(encoding);
|
|
|
|
return status;
|
2018-08-29 06:47:29 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
|
|
|
config_init_stdio_encoding(PyConfig *config,
|
|
|
|
const PyPreConfig *preconfig)
|
2018-08-29 06:47:29 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2019-05-01 10:22:52 -03:00
|
|
|
|
2020-11-04 06:20:10 -04:00
|
|
|
// Exit if encoding and errors are defined
|
2018-08-29 06:47:29 -03:00
|
|
|
if (config->stdio_encoding != NULL && config->stdio_errors != NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2018-08-29 06:47:29 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* PYTHONIOENCODING environment variable */
|
2019-05-27 11:39:22 -03:00
|
|
|
const char *opt = config_get_env(config, "PYTHONIOENCODING");
|
2018-08-29 06:47:29 -03:00
|
|
|
if (opt) {
|
2019-05-02 15:56:30 -03:00
|
|
|
char *pythonioencoding = _PyMem_RawStrdup(opt);
|
|
|
|
if (pythonioencoding == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_NO_MEMORY();
|
2018-08-29 06:47:29 -03:00
|
|
|
}
|
|
|
|
|
2019-05-01 10:22:52 -03:00
|
|
|
char *errors = strchr(pythonioencoding, ':');
|
|
|
|
if (errors) {
|
|
|
|
*errors = '\0';
|
|
|
|
errors++;
|
|
|
|
if (!errors[0]) {
|
|
|
|
errors = NULL;
|
2018-08-29 06:47:29 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does PYTHONIOENCODING contain an encoding? */
|
|
|
|
if (pythonioencoding[0]) {
|
|
|
|
if (config->stdio_encoding == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
status = CONFIG_SET_BYTES_STR(config, &config->stdio_encoding,
|
|
|
|
pythonioencoding,
|
|
|
|
"PYTHONIOENCODING environment variable");
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
2018-08-29 06:47:29 -03:00
|
|
|
PyMem_RawFree(pythonioencoding);
|
2019-05-27 11:39:22 -03:00
|
|
|
return status;
|
2018-08-29 06:47:29 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the encoding is set but not the error handler,
|
|
|
|
use "strict" error handler by default.
|
|
|
|
PYTHONIOENCODING=latin1 behaves as
|
|
|
|
PYTHONIOENCODING=latin1:strict. */
|
2019-05-01 10:22:52 -03:00
|
|
|
if (!errors) {
|
|
|
|
errors = "strict";
|
2018-08-29 06:47:29 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 10:22:52 -03:00
|
|
|
if (config->stdio_errors == NULL && errors != NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
status = CONFIG_SET_BYTES_STR(config, &config->stdio_errors,
|
|
|
|
errors,
|
|
|
|
"PYTHONIOENCODING environment variable");
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
2018-08-29 06:47:29 -03:00
|
|
|
PyMem_RawFree(pythonioencoding);
|
2019-05-27 11:39:22 -03:00
|
|
|
return status;
|
2018-08-29 06:47:29 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PyMem_RawFree(pythonioencoding);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Choose the default error handler based on the current locale. */
|
|
|
|
if (config->stdio_encoding == NULL) {
|
2020-10-30 21:02:09 -03:00
|
|
|
status = config_get_locale_encoding(config, preconfig,
|
|
|
|
&config->stdio_encoding);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2018-08-29 06:47:29 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (config->stdio_errors == NULL) {
|
2020-10-30 21:02:09 -03:00
|
|
|
const wchar_t *errors = config_get_stdio_errors(preconfig);
|
2019-05-01 10:22:52 -03:00
|
|
|
assert(errors != NULL);
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = PyConfig_SetString(config, &config->stdio_errors, errors);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2018-08-29 06:47:29 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2018-08-29 06:47:29 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-30 21:02:09 -03:00
|
|
|
// See also config_get_locale_encoding()
|
|
|
|
static PyStatus
|
|
|
|
config_get_fs_encoding(PyConfig *config, const PyPreConfig *preconfig,
|
|
|
|
wchar_t **fs_encoding)
|
|
|
|
{
|
|
|
|
#ifdef _Py_FORCE_UTF8_FS_ENCODING
|
|
|
|
return PyConfig_SetString(config, fs_encoding, L"utf-8");
|
|
|
|
#elif defined(MS_WINDOWS)
|
|
|
|
const wchar_t *encoding;
|
|
|
|
if (preconfig->legacy_windows_fs_encoding) {
|
|
|
|
// Legacy Windows filesystem encoding: mbcs/replace
|
|
|
|
encoding = L"mbcs";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Windows defaults to utf-8/surrogatepass (PEP 529)
|
|
|
|
encoding = L"utf-8";
|
|
|
|
}
|
|
|
|
return PyConfig_SetString(config, fs_encoding, encoding);
|
|
|
|
#else // !MS_WINDOWS
|
|
|
|
if (preconfig->utf8_mode) {
|
|
|
|
return PyConfig_SetString(config, fs_encoding, L"utf-8");
|
|
|
|
}
|
2020-11-04 06:20:10 -04:00
|
|
|
|
|
|
|
if (_Py_GetForceASCII()) {
|
2020-10-30 21:02:09 -03:00
|
|
|
return PyConfig_SetString(config, fs_encoding, L"ascii");
|
|
|
|
}
|
2020-11-04 06:20:10 -04:00
|
|
|
|
|
|
|
return config_get_locale_encoding(config, preconfig, fs_encoding);
|
2020-10-30 21:02:09 -03:00
|
|
|
#endif // !MS_WINDOWS
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
|
|
|
config_init_fs_encoding(PyConfig *config, const PyPreConfig *preconfig)
|
2018-08-29 08:25:36 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2019-05-01 10:22:52 -03:00
|
|
|
|
2018-08-29 08:25:36 -03:00
|
|
|
if (config->filesystem_encoding == NULL) {
|
2020-10-30 21:02:09 -03:00
|
|
|
status = config_get_fs_encoding(config, preconfig,
|
|
|
|
&config->filesystem_encoding);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2018-08-29 08:25:36 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config->filesystem_errors == NULL) {
|
2019-05-02 15:56:30 -03:00
|
|
|
const wchar_t *errors;
|
2019-05-02 12:28:57 -03:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
if (preconfig->legacy_windows_fs_encoding) {
|
2019-05-02 15:56:30 -03:00
|
|
|
errors = L"replace";
|
2019-05-02 12:28:57 -03:00
|
|
|
}
|
|
|
|
else {
|
2019-05-02 15:56:30 -03:00
|
|
|
errors = L"surrogatepass";
|
2019-05-02 12:28:57 -03:00
|
|
|
}
|
|
|
|
#else
|
2019-05-02 15:56:30 -03:00
|
|
|
errors = L"surrogateescape";
|
2019-05-02 12:28:57 -03:00
|
|
|
#endif
|
2019-05-27 11:39:22 -03:00
|
|
|
status = PyConfig_SetString(config, &config->filesystem_errors, errors);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2018-08-29 08:25:36 -03:00
|
|
|
}
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2018-08-29 08:25:36 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-14 20:31:45 -03:00
|
|
|
static PyStatus
|
|
|
|
config_init_import(PyConfig *config, int compute_path_config)
|
|
|
|
{
|
|
|
|
PyStatus status;
|
|
|
|
|
|
|
|
status = _PyConfig_InitPathConfig(config, compute_path_config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2023-11-01 17:39:31 -03:00
|
|
|
const char *env = config_get_env(config, "PYTHON_FROZEN_MODULES");
|
|
|
|
if (env == NULL) {
|
|
|
|
}
|
|
|
|
else if (strcmp(env, "on") == 0) {
|
|
|
|
config->use_frozen_modules = 1;
|
|
|
|
}
|
|
|
|
else if (strcmp(env, "off") == 0) {
|
|
|
|
config->use_frozen_modules = 0;
|
|
|
|
} else {
|
|
|
|
return PyStatus_Error("bad value for PYTHON_FROZEN_MODULES "
|
|
|
|
"(expected \"on\" or \"off\")");
|
|
|
|
}
|
|
|
|
|
2021-09-14 20:31:45 -03:00
|
|
|
/* -X frozen_modules=[on|off] */
|
2022-05-03 19:20:13 -03:00
|
|
|
const wchar_t *value = config_get_xoption_value(config, L"frozen_modules");
|
|
|
|
if (value == NULL) {
|
|
|
|
}
|
|
|
|
else if (wcscmp(value, L"on") == 0) {
|
|
|
|
config->use_frozen_modules = 1;
|
|
|
|
}
|
|
|
|
else if (wcscmp(value, L"off") == 0) {
|
|
|
|
config->use_frozen_modules = 0;
|
|
|
|
}
|
|
|
|
else if (wcslen(value) == 0) {
|
|
|
|
// "-X frozen_modules" and "-X frozen_modules=" both imply "on".
|
|
|
|
config->use_frozen_modules = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return PyStatus_Error("bad value for option -X frozen_modules "
|
|
|
|
"(expected \"on\" or \"off\")");
|
2021-09-14 20:31:45 -03:00
|
|
|
}
|
|
|
|
|
2022-05-05 20:34:11 -03:00
|
|
|
assert(config->use_frozen_modules >= 0);
|
2021-09-14 20:31:45 -03:00
|
|
|
return _PyStatus_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
PyStatus
|
|
|
|
_PyConfig_InitImportConfig(PyConfig *config)
|
|
|
|
{
|
|
|
|
return config_init_import(config, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
2020-11-10 08:21:52 -04:00
|
|
|
config_read(PyConfig *config, int compute_path_config)
|
2018-08-01 12:56:14 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
|
|
|
const PyPreConfig *preconfig = &_PyRuntime.preconfig;
|
2019-03-19 22:20:13 -03:00
|
|
|
|
2019-03-25 22:31:11 -03:00
|
|
|
if (config->use_environment) {
|
2019-05-27 11:39:22 -03:00
|
|
|
status = config_read_env_vars(config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -X options */
|
|
|
|
if (config_get_xoption(config, L"showrefcount")) {
|
|
|
|
config->show_ref_count = 1;
|
|
|
|
}
|
|
|
|
|
2024-03-11 12:02:58 -03:00
|
|
|
const wchar_t *x_gil = config_get_xoption_value(config, L"gil");
|
|
|
|
if (x_gil != NULL) {
|
|
|
|
size_t len = wcslen(x_gil);
|
|
|
|
status = config_read_gil(config, len, x_gil[0]);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-12 10:50:43 -04:00
|
|
|
#ifdef Py_STATS
|
|
|
|
if (config_get_xoption(config, L"pystats")) {
|
2023-09-06 12:54:59 -03:00
|
|
|
config->_pystats = 1;
|
|
|
|
}
|
|
|
|
else if (config_get_env(config, "PYTHONSTATS")) {
|
|
|
|
config->_pystats = 1;
|
|
|
|
}
|
|
|
|
if (config->_pystats < 0) {
|
|
|
|
config->_pystats = 0;
|
2022-12-12 10:50:43 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = config_read_complex_options(config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
|
2020-11-10 16:10:22 -04:00
|
|
|
if (config->_install_importlib) {
|
2021-09-14 20:31:45 -03:00
|
|
|
status = config_init_import(config, compute_path_config);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* default values */
|
2019-03-25 22:31:11 -03:00
|
|
|
if (config->dev_mode) {
|
2018-08-01 12:56:14 -03:00
|
|
|
if (config->faulthandler < 0) {
|
|
|
|
config->faulthandler = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (config->faulthandler < 0) {
|
|
|
|
config->faulthandler = 0;
|
|
|
|
}
|
|
|
|
if (config->tracemalloc < 0) {
|
|
|
|
config->tracemalloc = 0;
|
|
|
|
}
|
2022-08-30 14:11:18 -03:00
|
|
|
if (config->perf_profiling < 0) {
|
|
|
|
config->perf_profiling = 0;
|
|
|
|
}
|
2019-03-26 12:58:50 -03:00
|
|
|
if (config->use_hash_seed < 0) {
|
|
|
|
config->use_hash_seed = 0;
|
|
|
|
config->hash_seed = 0;
|
|
|
|
}
|
2018-08-01 12:56:14 -03:00
|
|
|
|
2018-08-29 08:45:34 -03:00
|
|
|
if (config->filesystem_encoding == NULL || config->filesystem_errors == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
status = config_init_fs_encoding(config, preconfig);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2018-08-29 08:25:36 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = config_init_stdio_encoding(config, preconfig);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2018-08-29 06:47:29 -03:00
|
|
|
}
|
|
|
|
|
2019-03-15 12:03:23 -03:00
|
|
|
if (config->argv.length < 1) {
|
|
|
|
/* Ensure at least one (empty) argument is seen */
|
2019-05-27 11:39:22 -03:00
|
|
|
status = PyWideStringList_Append(&config->argv, L"");
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-03-15 12:03:23 -03:00
|
|
|
}
|
|
|
|
}
|
2019-05-16 22:15:12 -03:00
|
|
|
|
|
|
|
if (config->check_hash_pycs_mode == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
status = PyConfig_SetString(config, &config->check_hash_pycs_mode,
|
|
|
|
L"default");
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-05-16 22:15:12 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config->configure_c_stdio < 0) {
|
|
|
|
config->configure_c_stdio = 1;
|
|
|
|
}
|
|
|
|
|
2020-11-05 13:58:07 -04:00
|
|
|
// Only parse arguments once.
|
|
|
|
if (config->parse_argv == 1) {
|
|
|
|
config->parse_argv = 2;
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2018-08-01 12:56:14 -03:00
|
|
|
}
|
2018-11-06 10:59:52 -04:00
|
|
|
|
|
|
|
|
2019-03-01 11:25:19 -04:00
|
|
|
static void
|
2019-05-27 11:39:22 -03:00
|
|
|
config_init_stdio(const PyConfig *config)
|
2019-03-01 11:25:19 -04:00
|
|
|
{
|
|
|
|
#if defined(MS_WINDOWS) || defined(__CYGWIN__)
|
|
|
|
/* don't translate newlines (\r\n <=> \n) */
|
|
|
|
_setmode(fileno(stdin), O_BINARY);
|
|
|
|
_setmode(fileno(stdout), O_BINARY);
|
|
|
|
_setmode(fileno(stderr), O_BINARY);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!config->buffered_stdio) {
|
|
|
|
#ifdef HAVE_SETVBUF
|
|
|
|
setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
|
|
|
|
setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
|
|
|
|
setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
|
|
|
|
#else /* !HAVE_SETVBUF */
|
|
|
|
setbuf(stdin, (char *)NULL);
|
|
|
|
setbuf(stdout, (char *)NULL);
|
|
|
|
setbuf(stderr, (char *)NULL);
|
|
|
|
#endif /* !HAVE_SETVBUF */
|
|
|
|
}
|
|
|
|
else if (config->interactive) {
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
/* Doesn't have to have line-buffered -- use unbuffered */
|
|
|
|
/* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
|
|
|
|
setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
|
|
|
|
#else /* !MS_WINDOWS */
|
|
|
|
#ifdef HAVE_SETVBUF
|
|
|
|
setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
|
|
|
|
setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
|
|
|
|
#endif /* HAVE_SETVBUF */
|
|
|
|
#endif /* !MS_WINDOWS */
|
|
|
|
/* Leave stderr alone - it should be unbuffered anyway. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Write the configuration:
|
|
|
|
|
|
|
|
- set Py_xxx global configuration variables
|
|
|
|
- initialize C standard streams (stdin, stdout, stderr) */
|
2020-06-08 13:12:59 -03:00
|
|
|
PyStatus
|
2019-05-27 11:39:22 -03:00
|
|
|
_PyConfig_Write(const PyConfig *config, _PyRuntimeState *runtime)
|
2019-03-01 11:25:19 -04:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
config_set_global_vars(config);
|
2019-05-16 13:30:15 -03:00
|
|
|
|
|
|
|
if (config->configure_c_stdio) {
|
|
|
|
config_init_stdio(config);
|
|
|
|
}
|
2018-11-06 10:59:52 -04:00
|
|
|
|
2019-03-26 12:58:50 -03:00
|
|
|
/* Write the new pre-configuration into _PyRuntime */
|
2019-05-27 11:39:22 -03:00
|
|
|
PyPreConfig *preconfig = &runtime->preconfig;
|
2019-03-26 12:58:50 -03:00
|
|
|
preconfig->isolated = config->isolated;
|
|
|
|
preconfig->use_environment = config->use_environment;
|
|
|
|
preconfig->dev_mode = config->dev_mode;
|
2020-06-08 13:12:59 -03:00
|
|
|
|
2020-06-29 19:49:03 -03:00
|
|
|
if (_Py_SetArgcArgv(config->orig_argv.length,
|
|
|
|
config->orig_argv.items) < 0)
|
2020-06-08 13:12:59 -03:00
|
|
|
{
|
|
|
|
return _PyStatus_NO_MEMORY();
|
|
|
|
}
|
2023-09-06 12:54:59 -03:00
|
|
|
|
|
|
|
#ifdef Py_STATS
|
|
|
|
if (config->_pystats) {
|
|
|
|
_Py_StatsOn();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-06-08 13:12:59 -03:00
|
|
|
return _PyStatus_OK();
|
2018-11-06 10:59:52 -04:00
|
|
|
}
|
2019-03-01 11:25:19 -04:00
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
/* --- PyConfig command line parser -------------------------- */
|
2019-03-01 11:25:19 -04:00
|
|
|
|
|
|
|
static void
|
2019-03-29 11:13:46 -03:00
|
|
|
config_usage(int error, const wchar_t* program)
|
2019-03-01 11:25:19 -04:00
|
|
|
{
|
2019-03-29 11:13:46 -03:00
|
|
|
FILE *f = error ? stderr : stdout;
|
2019-03-01 11:25:19 -04:00
|
|
|
|
2019-03-29 11:13:46 -03:00
|
|
|
fprintf(f, usage_line, program);
|
|
|
|
if (error)
|
|
|
|
fprintf(f, "Try `python -h' for more information.\n");
|
|
|
|
else {
|
2022-06-01 06:50:01 -03:00
|
|
|
fputs(usage_help, f);
|
2019-03-29 11:13:46 -03:00
|
|
|
}
|
|
|
|
}
|
2019-03-01 11:25:19 -04:00
|
|
|
|
2022-06-01 06:50:01 -03:00
|
|
|
static void
|
2023-04-05 04:22:33 -03:00
|
|
|
config_envvars_usage(void)
|
2022-06-01 06:50:01 -03:00
|
|
|
{
|
|
|
|
printf(usage_envvars, (wint_t)DELIM, (wint_t)DELIM, PYTHONHOMEHELP);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-04-05 04:22:33 -03:00
|
|
|
config_xoptions_usage(void)
|
2022-06-01 06:50:01 -03:00
|
|
|
{
|
|
|
|
puts(usage_xoptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
config_complete_usage(const wchar_t* program)
|
|
|
|
{
|
|
|
|
config_usage(0, program);
|
2024-02-23 05:27:07 -04:00
|
|
|
putchar('\n');
|
2022-06-01 06:50:01 -03:00
|
|
|
config_envvars_usage();
|
2024-02-23 05:27:07 -04:00
|
|
|
putchar('\n');
|
2022-06-01 06:50:01 -03:00
|
|
|
config_xoptions_usage();
|
|
|
|
}
|
|
|
|
|
2019-03-01 11:25:19 -04:00
|
|
|
|
|
|
|
/* Parse the command line arguments */
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
|
|
|
config_parse_cmdline(PyConfig *config, PyWideStringList *warnoptions,
|
2019-05-17 19:38:16 -03:00
|
|
|
Py_ssize_t *opt_index)
|
2019-03-01 11:25:19 -04:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
|
|
|
const PyWideStringList *argv = &config->argv;
|
2019-03-29 11:13:46 -03:00
|
|
|
int print_version = 0;
|
2019-05-17 06:12:09 -03:00
|
|
|
const wchar_t* program = config->program_name;
|
2022-07-19 15:39:11 -03:00
|
|
|
if (!program && argv->length >= 1) {
|
|
|
|
program = argv->items[0];
|
|
|
|
}
|
2019-03-20 00:25:38 -03:00
|
|
|
|
2019-03-01 11:25:19 -04:00
|
|
|
_PyOS_ResetGetOpt();
|
|
|
|
do {
|
|
|
|
int longindex = -1;
|
2019-03-20 00:25:38 -03:00
|
|
|
int c = _PyOS_GetOpt(argv->length, argv->items, &longindex);
|
2019-03-01 11:25:19 -04:00
|
|
|
if (c == EOF) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == 'c') {
|
2019-03-19 23:11:38 -03:00
|
|
|
if (config->run_command == NULL) {
|
|
|
|
/* -c is the last option; following arguments
|
|
|
|
that look like options are left for the
|
|
|
|
command to interpret. */
|
|
|
|
size_t len = wcslen(_PyOS_optarg) + 1 + 1;
|
|
|
|
wchar_t *command = PyMem_RawMalloc(sizeof(wchar_t) * len);
|
|
|
|
if (command == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_NO_MEMORY();
|
2019-03-19 23:11:38 -03:00
|
|
|
}
|
|
|
|
memcpy(command, _PyOS_optarg, (len - 2) * sizeof(wchar_t));
|
|
|
|
command[len - 2] = '\n';
|
|
|
|
command[len - 1] = 0;
|
|
|
|
config->run_command = command;
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == 'm') {
|
|
|
|
/* -m is the last option; following arguments
|
|
|
|
that look like options are left for the
|
|
|
|
module to interpret. */
|
|
|
|
if (config->run_module == NULL) {
|
2019-03-19 23:11:38 -03:00
|
|
|
config->run_module = _PyMem_RawWcsdup(_PyOS_optarg);
|
|
|
|
if (config->run_module == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_NO_MEMORY();
|
2019-03-19 23:11:38 -03:00
|
|
|
}
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (c) {
|
2022-06-01 06:50:01 -03:00
|
|
|
// Integers represent long options, see Python/getopt.c
|
2019-03-01 11:25:19 -04:00
|
|
|
case 0:
|
2022-06-01 06:50:01 -03:00
|
|
|
// check-hash-based-pycs
|
2019-05-02 00:51:56 -03:00
|
|
|
if (wcscmp(_PyOS_optarg, L"always") == 0
|
|
|
|
|| wcscmp(_PyOS_optarg, L"never") == 0
|
|
|
|
|| wcscmp(_PyOS_optarg, L"default") == 0)
|
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
status = PyConfig_SetString(config, &config->check_hash_pycs_mode,
|
|
|
|
_PyOS_optarg);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-05-02 00:51:56 -03:00
|
|
|
}
|
2019-03-01 11:25:19 -04:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "--check-hash-based-pycs must be one of "
|
|
|
|
"'default', 'always', or 'never'\n");
|
2019-05-17 06:12:09 -03:00
|
|
|
config_usage(1, program);
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_EXIT(2);
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-06-01 06:50:01 -03:00
|
|
|
case 1:
|
|
|
|
// help-all
|
|
|
|
config_complete_usage(program);
|
|
|
|
return _PyStatus_EXIT(0);
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
// help-env
|
|
|
|
config_envvars_usage();
|
|
|
|
return _PyStatus_EXIT(0);
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
// help-xoptions
|
|
|
|
config_xoptions_usage();
|
|
|
|
return _PyStatus_EXIT(0);
|
|
|
|
|
2019-03-01 11:25:19 -04:00
|
|
|
case 'b':
|
|
|
|
config->bytes_warning++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
config->parser_debug++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i':
|
|
|
|
config->inspect++;
|
|
|
|
config->interactive++;
|
|
|
|
break;
|
|
|
|
|
2019-03-04 21:44:12 -04:00
|
|
|
case 'E':
|
2019-03-01 11:25:19 -04:00
|
|
|
case 'I':
|
2019-03-20 00:25:38 -03:00
|
|
|
case 'X':
|
2019-03-26 12:58:50 -03:00
|
|
|
/* option handled by _PyPreCmdline_Read() */
|
2019-03-01 11:25:19 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* case 'J': reserved for Jython */
|
|
|
|
|
|
|
|
case 'O':
|
|
|
|
config->optimization_level++;
|
|
|
|
break;
|
|
|
|
|
2022-05-05 20:34:11 -03:00
|
|
|
case 'P':
|
|
|
|
config->safe_path = 1;
|
|
|
|
break;
|
|
|
|
|
2019-03-01 11:25:19 -04:00
|
|
|
case 'B':
|
|
|
|
config->write_bytecode = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
config->user_site_directory = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'S':
|
|
|
|
config->site_import = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
/* ignored for backwards compatibility */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'u':
|
|
|
|
config->buffered_stdio = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
config->verbose++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'x':
|
|
|
|
config->skip_source_first_line = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
2019-05-17 06:12:09 -03:00
|
|
|
config_usage(0, program);
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_EXIT(0);
|
2019-03-01 11:25:19 -04:00
|
|
|
|
|
|
|
case 'V':
|
2019-03-29 11:13:46 -03:00
|
|
|
print_version++;
|
2019-03-01 11:25:19 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'W':
|
2019-05-27 11:39:22 -03:00
|
|
|
status = PyWideStringList_Append(warnoptions, _PyOS_optarg);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'q':
|
|
|
|
config->quiet++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'R':
|
|
|
|
config->use_hash_seed = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* This space reserved for other options */
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* unknown argument: parsing failed */
|
2019-05-17 06:12:09 -03:00
|
|
|
config_usage(1, program);
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_EXIT(2);
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
|
2019-03-29 11:13:46 -03:00
|
|
|
if (print_version) {
|
|
|
|
printf("Python %s\n",
|
|
|
|
(print_version >= 2) ? Py_GetVersion() : PY_VERSION);
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_EXIT(0);
|
2019-03-29 11:13:46 -03:00
|
|
|
}
|
|
|
|
|
2019-03-01 11:25:19 -04:00
|
|
|
if (config->run_command == NULL && config->run_module == NULL
|
2019-03-20 00:25:38 -03:00
|
|
|
&& _PyOS_optind < argv->length
|
|
|
|
&& wcscmp(argv->items[_PyOS_optind], L"-") != 0
|
2019-03-19 23:11:38 -03:00
|
|
|
&& config->run_filename == NULL)
|
2019-03-01 11:25:19 -04:00
|
|
|
{
|
2019-03-20 00:25:38 -03:00
|
|
|
config->run_filename = _PyMem_RawWcsdup(argv->items[_PyOS_optind]);
|
2019-03-01 11:25:19 -04:00
|
|
|
if (config->run_filename == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_NO_MEMORY();
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config->run_command != NULL || config->run_module != NULL) {
|
|
|
|
/* Backup _PyOS_optind */
|
|
|
|
_PyOS_optind--;
|
|
|
|
}
|
|
|
|
|
2019-05-16 12:02:56 -03:00
|
|
|
*opt_index = _PyOS_optind;
|
2019-03-01 11:25:19 -04:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
# define WCSTOK wcstok_s
|
|
|
|
#else
|
|
|
|
# define WCSTOK wcstok
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Get warning options from PYTHONWARNINGS environment variable. */
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
|
|
|
config_init_env_warnoptions(PyConfig *config, PyWideStringList *warnoptions)
|
2019-03-01 11:25:19 -04:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2019-05-01 10:22:52 -03:00
|
|
|
/* CONFIG_GET_ENV_DUP requires dest to be initialized to NULL */
|
|
|
|
wchar_t *env = NULL;
|
2019-05-27 11:39:22 -03:00
|
|
|
status = CONFIG_GET_ENV_DUP(config, &env,
|
2019-05-01 10:22:52 -03:00
|
|
|
L"PYTHONWARNINGS", "PYTHONWARNINGS");
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
|
2019-05-01 10:22:52 -03:00
|
|
|
/* env var is not set or is empty */
|
2019-03-01 11:25:19 -04:00
|
|
|
if (env == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wchar_t *warning, *context = NULL;
|
|
|
|
for (warning = WCSTOK(env, L",", &context);
|
|
|
|
warning != NULL;
|
|
|
|
warning = WCSTOK(NULL, L",", &context))
|
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
status = PyWideStringList_Append(warnoptions, warning);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
2019-03-01 11:25:19 -04:00
|
|
|
PyMem_RawFree(env);
|
2019-05-27 11:39:22 -03:00
|
|
|
return status;
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
PyMem_RawFree(env);
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
2019-09-29 20:40:17 -03:00
|
|
|
warnoptions_append(PyConfig *config, PyWideStringList *options,
|
|
|
|
const wchar_t *option)
|
2019-03-26 12:58:50 -03:00
|
|
|
{
|
2019-09-29 20:40:17 -03:00
|
|
|
/* config_init_warnoptions() add existing config warnoptions at the end:
|
|
|
|
ensure that the new option is not already present in this list to
|
2021-10-06 19:55:27 -03:00
|
|
|
prevent change the options order when config_init_warnoptions() is
|
2019-09-29 20:40:17 -03:00
|
|
|
called twice. */
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyWideStringList_Find(&config->warnoptions, option)) {
|
2019-03-26 12:58:50 -03:00
|
|
|
/* Already present: do nothing */
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2019-03-26 12:58:50 -03:00
|
|
|
}
|
2019-09-29 20:40:17 -03:00
|
|
|
if (_PyWideStringList_Find(options, option)) {
|
|
|
|
/* Already present: do nothing */
|
|
|
|
return _PyStatus_OK();
|
|
|
|
}
|
|
|
|
return PyWideStringList_Append(options, option);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyStatus
|
|
|
|
warnoptions_extend(PyConfig *config, PyWideStringList *options,
|
|
|
|
const PyWideStringList *options2)
|
|
|
|
{
|
|
|
|
const Py_ssize_t len = options2->length;
|
|
|
|
wchar_t *const *items = options2->items;
|
|
|
|
|
|
|
|
for (Py_ssize_t i = 0; i < len; i++) {
|
|
|
|
PyStatus status = warnoptions_append(config, options, items[i]);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _PyStatus_OK();
|
2019-03-26 12:58:50 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
|
|
|
config_init_warnoptions(PyConfig *config,
|
|
|
|
const PyWideStringList *cmdline_warnoptions,
|
2019-09-29 20:40:17 -03:00
|
|
|
const PyWideStringList *env_warnoptions,
|
|
|
|
const PyWideStringList *sys_warnoptions)
|
2019-03-01 11:25:19 -04:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2019-09-29 20:40:17 -03:00
|
|
|
PyWideStringList options = _PyWideStringList_INIT;
|
2019-05-27 11:39:22 -03:00
|
|
|
|
2019-09-29 20:40:17 -03:00
|
|
|
/* Priority of warnings options, lowest to highest:
|
2019-03-01 11:25:19 -04:00
|
|
|
*
|
|
|
|
* - any implicit filters added by _warnings.c/warnings.py
|
2019-09-29 20:40:17 -03:00
|
|
|
* - PyConfig.dev_mode: "default" filter
|
|
|
|
* - PYTHONWARNINGS environment variable
|
|
|
|
* - '-W' command line options
|
|
|
|
* - PyConfig.bytes_warning ('-b' and '-bb' command line options):
|
|
|
|
* "default::BytesWarning" or "error::BytesWarning" filter
|
|
|
|
* - early PySys_AddWarnOption() calls
|
|
|
|
* - PyConfig.warnoptions
|
2019-03-01 11:25:19 -04:00
|
|
|
*
|
2019-09-29 20:40:17 -03:00
|
|
|
* PyConfig.warnoptions is copied to sys.warnoptions. Since the warnings
|
|
|
|
* module works on the basis of "the most recently added filter will be
|
|
|
|
* checked first", we add the lowest precedence entries first so that later
|
|
|
|
* entries override them.
|
2019-03-01 11:25:19 -04:00
|
|
|
*/
|
|
|
|
|
2019-03-25 22:31:11 -03:00
|
|
|
if (config->dev_mode) {
|
2019-09-29 20:40:17 -03:00
|
|
|
status = warnoptions_append(config, &options, L"default");
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
2019-09-29 20:40:17 -03:00
|
|
|
goto error;
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-29 20:40:17 -03:00
|
|
|
status = warnoptions_extend(config, &options, env_warnoptions);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
goto error;
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
|
2019-09-29 20:40:17 -03:00
|
|
|
status = warnoptions_extend(config, &options, cmdline_warnoptions);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
goto error;
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If the bytes_warning_flag isn't set, bytesobject.c and bytearrayobject.c
|
|
|
|
* don't even try to emit a warning, so we skip setting the filter in that
|
|
|
|
* case.
|
|
|
|
*/
|
|
|
|
if (config->bytes_warning) {
|
2019-03-15 11:08:05 -03:00
|
|
|
const wchar_t *filter;
|
2019-03-01 11:25:19 -04:00
|
|
|
if (config->bytes_warning> 1) {
|
|
|
|
filter = L"error::BytesWarning";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
filter = L"default::BytesWarning";
|
|
|
|
}
|
2019-09-29 20:40:17 -03:00
|
|
|
status = warnoptions_append(config, &options, filter);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
2019-09-29 20:40:17 -03:00
|
|
|
goto error;
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
}
|
2019-08-23 14:03:08 -03:00
|
|
|
|
2019-09-29 20:40:17 -03:00
|
|
|
status = warnoptions_extend(config, &options, sys_warnoptions);
|
2019-08-23 14:03:08 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
2019-09-29 20:40:17 -03:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Always add all PyConfig.warnoptions options */
|
|
|
|
status = _PyWideStringList_Extend(&options, &config->warnoptions);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
goto error;
|
2019-08-23 14:03:08 -03:00
|
|
|
}
|
|
|
|
|
2019-09-29 20:40:17 -03:00
|
|
|
_PyWideStringList_Clear(&config->warnoptions);
|
|
|
|
config->warnoptions = options;
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2019-09-29 20:40:17 -03:00
|
|
|
|
|
|
|
error:
|
|
|
|
_PyWideStringList_Clear(&options);
|
|
|
|
return status;
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
|
|
|
config_update_argv(PyConfig *config, Py_ssize_t opt_index)
|
2019-03-01 11:25:19 -04:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
const PyWideStringList *cmdline_argv = &config->argv;
|
2019-09-29 20:40:17 -03:00
|
|
|
PyWideStringList config_argv = _PyWideStringList_INIT;
|
2019-03-01 11:25:19 -04:00
|
|
|
|
2019-03-15 11:08:05 -03:00
|
|
|
/* Copy argv to be able to modify it (to force -c/-m) */
|
2019-05-16 12:02:56 -03:00
|
|
|
if (cmdline_argv->length <= opt_index) {
|
2019-03-01 11:25:19 -04:00
|
|
|
/* Ensure at least one (empty) argument is seen */
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status = PyWideStringList_Append(&config_argv, L"");
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-03-15 11:08:05 -03:00
|
|
|
}
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
else {
|
2019-05-27 11:39:22 -03:00
|
|
|
PyWideStringList slice;
|
2019-05-16 12:02:56 -03:00
|
|
|
slice.length = cmdline_argv->length - opt_index;
|
|
|
|
slice.items = &cmdline_argv->items[opt_index];
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyWideStringList_Copy(&config_argv, &slice) < 0) {
|
|
|
|
return _PyStatus_NO_MEMORY();
|
2019-03-15 11:08:05 -03:00
|
|
|
}
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
2019-03-20 00:25:38 -03:00
|
|
|
assert(config_argv.length >= 1);
|
2019-03-01 11:25:19 -04:00
|
|
|
|
|
|
|
wchar_t *arg0 = NULL;
|
|
|
|
if (config->run_command != NULL) {
|
|
|
|
/* Force sys.argv[0] = '-c' */
|
|
|
|
arg0 = L"-c";
|
|
|
|
}
|
|
|
|
else if (config->run_module != NULL) {
|
|
|
|
/* Force sys.argv[0] = '-m'*/
|
|
|
|
arg0 = L"-m";
|
|
|
|
}
|
2019-06-25 10:02:43 -03:00
|
|
|
|
2019-03-01 11:25:19 -04:00
|
|
|
if (arg0 != NULL) {
|
|
|
|
arg0 = _PyMem_RawWcsdup(arg0);
|
|
|
|
if (arg0 == NULL) {
|
2019-05-27 11:39:22 -03:00
|
|
|
_PyWideStringList_Clear(&config_argv);
|
|
|
|
return _PyStatus_NO_MEMORY();
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
|
2019-03-20 00:25:38 -03:00
|
|
|
PyMem_RawFree(config_argv.items[0]);
|
|
|
|
config_argv.items[0] = arg0;
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
_PyWideStringList_Clear(&config->argv);
|
2019-03-20 00:25:38 -03:00
|
|
|
config->argv = config_argv;
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
|
|
|
core_read_precmdline(PyConfig *config, _PyPreCmdline *precmdline)
|
2019-03-27 09:40:14 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2019-03-27 09:40:14 -03:00
|
|
|
|
2020-11-05 13:58:07 -04:00
|
|
|
if (config->parse_argv == 1) {
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyWideStringList_Copy(&precmdline->argv, &config->argv) < 0) {
|
|
|
|
return _PyStatus_NO_MEMORY();
|
2019-05-17 14:01:14 -03:00
|
|
|
}
|
2019-03-27 09:40:14 -03:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyPreConfig preconfig;
|
2019-09-27 23:28:35 -03:00
|
|
|
|
|
|
|
status = _PyPreConfig_InitFromPreConfig(&preconfig, &_PyRuntime.preconfig);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
2019-03-27 09:40:14 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
_PyPreConfig_GetConfig(&preconfig, config);
|
2019-03-27 09:40:14 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _PyPreCmdline_Read(precmdline, &preconfig);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-05-16 22:15:12 -03:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _PyPreCmdline_SetConfig(precmdline, config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-05-16 22:15:12 -03:00
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyStatus_OK();
|
2019-03-27 09:40:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-25 10:02:43 -03:00
|
|
|
/* Get run_filename absolute path */
|
|
|
|
static PyStatus
|
|
|
|
config_run_filename_abspath(PyConfig *config)
|
|
|
|
{
|
|
|
|
if (!config->run_filename) {
|
|
|
|
return _PyStatus_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef MS_WINDOWS
|
|
|
|
if (_Py_isabs(config->run_filename)) {
|
|
|
|
/* path is already absolute */
|
|
|
|
return _PyStatus_OK();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
wchar_t *abs_filename;
|
|
|
|
if (_Py_abspath(config->run_filename, &abs_filename) < 0) {
|
|
|
|
/* failed to get the absolute path of the command line filename:
|
|
|
|
ignore the error, keep the relative path */
|
|
|
|
return _PyStatus_OK();
|
|
|
|
}
|
|
|
|
if (abs_filename == NULL) {
|
|
|
|
return _PyStatus_NO_MEMORY();
|
|
|
|
}
|
|
|
|
|
|
|
|
PyMem_RawFree(config->run_filename);
|
|
|
|
config->run_filename = abs_filename;
|
|
|
|
return _PyStatus_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
static PyStatus
|
|
|
|
config_read_cmdline(PyConfig *config)
|
2019-03-01 11:25:19 -04:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2019-09-29 20:40:17 -03:00
|
|
|
PyWideStringList cmdline_warnoptions = _PyWideStringList_INIT;
|
|
|
|
PyWideStringList env_warnoptions = _PyWideStringList_INIT;
|
|
|
|
PyWideStringList sys_warnoptions = _PyWideStringList_INIT;
|
2019-03-01 11:25:19 -04:00
|
|
|
|
2019-05-16 12:02:56 -03:00
|
|
|
if (config->parse_argv < 0) {
|
|
|
|
config->parse_argv = 1;
|
2019-03-26 12:58:50 -03:00
|
|
|
}
|
2019-05-16 22:15:12 -03:00
|
|
|
|
2020-11-05 13:58:07 -04:00
|
|
|
if (config->parse_argv == 1) {
|
2019-05-17 19:38:16 -03:00
|
|
|
Py_ssize_t opt_index;
|
2019-05-27 11:39:22 -03:00
|
|
|
status = config_parse_cmdline(config, &cmdline_warnoptions, &opt_index);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
2019-05-16 12:02:56 -03:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:02:43 -03:00
|
|
|
status = config_run_filename_abspath(config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = config_update_argv(config, opt_index);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
2019-05-16 12:02:56 -03:00
|
|
|
goto done;
|
|
|
|
}
|
2019-06-25 10:02:43 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
status = config_run_filename_abspath(config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
goto done;
|
|
|
|
}
|
2019-03-29 11:13:46 -03:00
|
|
|
}
|
2019-03-19 22:20:13 -03:00
|
|
|
|
2019-03-29 11:13:46 -03:00
|
|
|
if (config->use_environment) {
|
2019-05-27 11:39:22 -03:00
|
|
|
status = config_init_env_warnoptions(config, &env_warnoptions);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
2019-03-26 12:58:50 -03:00
|
|
|
goto done;
|
2019-03-19 23:11:38 -03:00
|
|
|
}
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
|
2019-09-29 20:40:17 -03:00
|
|
|
/* Handle early PySys_AddWarnOption() calls */
|
|
|
|
status = _PySys_ReadPreinitWarnOptions(&sys_warnoptions);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = config_init_warnoptions(config,
|
2019-08-23 14:03:08 -03:00
|
|
|
&cmdline_warnoptions,
|
2019-09-29 20:40:17 -03:00
|
|
|
&env_warnoptions,
|
|
|
|
&sys_warnoptions);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
2019-03-29 11:13:46 -03:00
|
|
|
goto done;
|
|
|
|
}
|
2019-03-20 00:25:38 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _PyStatus_OK();
|
2019-03-01 11:25:19 -04:00
|
|
|
|
2019-03-29 11:13:46 -03:00
|
|
|
done:
|
2019-05-27 11:39:22 -03:00
|
|
|
_PyWideStringList_Clear(&cmdline_warnoptions);
|
|
|
|
_PyWideStringList_Clear(&env_warnoptions);
|
2019-09-29 20:40:17 -03:00
|
|
|
_PyWideStringList_Clear(&sys_warnoptions);
|
2019-05-27 11:39:22 -03:00
|
|
|
return status;
|
2019-03-29 11:13:46 -03:00
|
|
|
}
|
2019-03-01 11:25:19 -04:00
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus
|
|
|
|
_PyConfig_SetPyArgv(PyConfig *config, const _PyArgv *args)
|
2019-04-30 21:30:12 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status = _Py_PreInitializeFromConfig(config, args);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-05-02 16:25:34 -03:00
|
|
|
}
|
2019-05-20 06:02:00 -03:00
|
|
|
|
2019-04-30 21:30:12 -03:00
|
|
|
return _PyArgv_AsWstrList(args, &config->argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-02 16:25:34 -03:00
|
|
|
/* Set config.argv: decode argv using Py_DecodeLocale(). Pre-initialize Python
|
|
|
|
if needed to ensure that encodings are properly configured. */
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus
|
|
|
|
PyConfig_SetBytesArgv(PyConfig *config, Py_ssize_t argc, char * const *argv)
|
2019-04-30 21:30:12 -03:00
|
|
|
{
|
|
|
|
_PyArgv args = {
|
|
|
|
.argc = argc,
|
|
|
|
.use_bytes_argv = 1,
|
|
|
|
.bytes_argv = argv,
|
|
|
|
.wchar_argv = NULL};
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyConfig_SetPyArgv(config, &args);
|
2019-04-30 21:30:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus
|
|
|
|
PyConfig_SetArgv(PyConfig *config, Py_ssize_t argc, wchar_t * const *argv)
|
2019-04-30 21:30:12 -03:00
|
|
|
{
|
|
|
|
_PyArgv args = {
|
|
|
|
.argc = argc,
|
|
|
|
.use_bytes_argv = 0,
|
|
|
|
.bytes_argv = NULL,
|
|
|
|
.wchar_argv = argv};
|
2019-05-27 11:39:22 -03:00
|
|
|
return _PyConfig_SetPyArgv(config, &args);
|
2019-04-30 21:30:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-01 14:13:50 -03:00
|
|
|
PyStatus
|
|
|
|
PyConfig_SetWideStringList(PyConfig *config, PyWideStringList *list,
|
|
|
|
Py_ssize_t length, wchar_t **items)
|
|
|
|
{
|
|
|
|
PyStatus status = _Py_PreInitializeFromConfig(config, NULL);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyWideStringList list2 = {.length = length, .items = items};
|
|
|
|
if (_PyWideStringList_Copy(list, &list2) < 0) {
|
|
|
|
return _PyStatus_NO_MEMORY();
|
|
|
|
}
|
|
|
|
return _PyStatus_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
/* Read the configuration into PyConfig from:
|
2019-03-29 11:13:46 -03:00
|
|
|
|
|
|
|
* Command line arguments
|
|
|
|
* Environment variables
|
2019-05-16 13:30:15 -03:00
|
|
|
* Py_xxx global configuration variables
|
|
|
|
|
|
|
|
The only side effects are to modify config and to call _Py_SetArgcArgv(). */
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus
|
2020-11-10 08:21:52 -04:00
|
|
|
_PyConfig_Read(PyConfig *config, int compute_path_config)
|
2019-03-29 11:13:46 -03:00
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyStatus status;
|
2019-03-01 11:25:19 -04:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _Py_PreInitializeFromConfig(config, NULL);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
return status;
|
2019-03-29 11:13:46 -03:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
config_get_global_vars(config);
|
2019-03-29 11:13:46 -03:00
|
|
|
|
2020-06-29 19:49:03 -03:00
|
|
|
if (config->orig_argv.length == 0
|
2020-06-08 13:12:59 -03:00
|
|
|
&& !(config->argv.length == 1
|
|
|
|
&& wcscmp(config->argv.items[0], L"") == 0))
|
|
|
|
{
|
2020-06-29 19:49:03 -03:00
|
|
|
if (_PyWideStringList_Copy(&config->orig_argv, &config->argv) < 0) {
|
2020-06-08 13:12:59 -03:00
|
|
|
return _PyStatus_NO_MEMORY();
|
|
|
|
}
|
2019-05-17 14:01:14 -03:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:13:46 -03:00
|
|
|
_PyPreCmdline precmdline = _PyPreCmdline_INIT;
|
2019-05-27 11:39:22 -03:00
|
|
|
status = core_read_precmdline(config, &precmdline);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
2019-03-26 12:58:50 -03:00
|
|
|
goto done;
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
|
2019-05-16 22:15:12 -03:00
|
|
|
assert(config->isolated >= 0);
|
|
|
|
if (config->isolated) {
|
2022-05-05 20:34:11 -03:00
|
|
|
config->safe_path = 1;
|
2019-05-16 22:15:12 -03:00
|
|
|
config->use_environment = 0;
|
|
|
|
config->user_site_directory = 0;
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = config_read_cmdline(config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
2019-05-16 22:15:12 -03:00
|
|
|
goto done;
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
|
2019-08-23 14:03:08 -03:00
|
|
|
/* Handle early PySys_AddXOption() calls */
|
|
|
|
status = _PySys_ReadPreinitXOptions(config);
|
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2020-11-10 08:21:52 -04:00
|
|
|
status = config_read(config, compute_path_config);
|
2019-05-27 11:39:22 -03:00
|
|
|
if (_PyStatus_EXCEPTION(status)) {
|
2019-03-26 12:58:50 -03:00
|
|
|
goto done;
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
|
|
|
|
2020-11-05 13:12:33 -04:00
|
|
|
assert(config_check_consistency(config));
|
2019-03-26 12:58:50 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
status = _PyStatus_OK();
|
2019-03-01 11:25:19 -04:00
|
|
|
|
|
|
|
done:
|
2019-03-26 12:58:50 -03:00
|
|
|
_PyPreCmdline_Clear(&precmdline);
|
2019-05-27 11:39:22 -03:00
|
|
|
return status;
|
2019-03-01 11:25:19 -04:00
|
|
|
}
|
2019-03-25 19:19:57 -03:00
|
|
|
|
|
|
|
|
2020-11-10 08:21:52 -04:00
|
|
|
PyStatus
|
|
|
|
PyConfig_Read(PyConfig *config)
|
|
|
|
{
|
2021-12-02 20:08:42 -04:00
|
|
|
return _PyConfig_Read(config, 0);
|
2020-11-10 08:21:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-25 19:19:57 -03:00
|
|
|
PyObject*
|
|
|
|
_Py_GetConfigsAsDict(void)
|
|
|
|
{
|
2019-05-27 11:39:22 -03:00
|
|
|
PyObject *result = NULL;
|
2019-03-25 19:19:57 -03:00
|
|
|
PyObject *dict = NULL;
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
result = PyDict_New();
|
|
|
|
if (result == NULL) {
|
2019-03-25 19:19:57 -03:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
/* global result */
|
2019-03-25 19:19:57 -03:00
|
|
|
dict = _Py_GetGlobalVariablesAsDict();
|
|
|
|
if (dict == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
if (PyDict_SetItemString(result, "global_config", dict) < 0) {
|
2019-03-25 19:19:57 -03:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
Py_CLEAR(dict);
|
|
|
|
|
|
|
|
/* pre config */
|
2021-02-19 10:10:45 -04:00
|
|
|
PyInterpreterState *interp = _PyInterpreterState_GET();
|
|
|
|
const PyPreConfig *pre_config = &interp->runtime->preconfig;
|
2019-03-25 19:19:57 -03:00
|
|
|
dict = _PyPreConfig_AsDict(pre_config);
|
|
|
|
if (dict == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
if (PyDict_SetItemString(result, "pre_config", dict) < 0) {
|
2019-03-25 19:19:57 -03:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
Py_CLEAR(dict);
|
|
|
|
|
|
|
|
/* core config */
|
2021-02-19 10:10:45 -04:00
|
|
|
const PyConfig *config = _PyInterpreterState_GetConfig(interp);
|
2020-11-05 13:12:33 -04:00
|
|
|
dict = _PyConfig_AsDict(config);
|
2019-03-25 19:19:57 -03:00
|
|
|
if (dict == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
if (PyDict_SetItemString(result, "config", dict) < 0) {
|
2019-03-25 19:19:57 -03:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
Py_CLEAR(dict);
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
return result;
|
2019-03-25 19:19:57 -03:00
|
|
|
|
|
|
|
error:
|
2019-05-27 11:39:22 -03:00
|
|
|
Py_XDECREF(result);
|
2019-03-25 19:19:57 -03:00
|
|
|
Py_XDECREF(dict);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-09-23 09:45:47 -03:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_dump_ascii_wstr(const wchar_t *str)
|
|
|
|
{
|
|
|
|
if (str == NULL) {
|
|
|
|
PySys_WriteStderr("(not set)");
|
2019-09-23 10:35:46 -03:00
|
|
|
return;
|
2019-09-23 09:45:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PySys_WriteStderr("'");
|
|
|
|
for (; *str != L'\0'; str++) {
|
2020-09-09 07:07:17 -03:00
|
|
|
unsigned int ch = (unsigned int)*str;
|
2019-09-23 09:45:47 -03:00
|
|
|
if (ch == L'\'') {
|
|
|
|
PySys_WriteStderr("\\'");
|
|
|
|
} else if (0x20 <= ch && ch < 0x7f) {
|
2020-09-21 05:35:17 -03:00
|
|
|
PySys_WriteStderr("%c", ch);
|
2019-09-23 09:45:47 -03:00
|
|
|
}
|
|
|
|
else if (ch <= 0xff) {
|
|
|
|
PySys_WriteStderr("\\x%02x", ch);
|
|
|
|
}
|
|
|
|
#if SIZEOF_WCHAR_T > 2
|
|
|
|
else if (ch > 0xffff) {
|
|
|
|
PySys_WriteStderr("\\U%08x", ch);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else {
|
|
|
|
PySys_WriteStderr("\\u%04x", ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PySys_WriteStderr("'");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Dump the Python path configuration into sys.stderr */
|
|
|
|
void
|
|
|
|
_Py_DumpPathConfig(PyThreadState *tstate)
|
|
|
|
{
|
2023-02-08 05:31:12 -04:00
|
|
|
PyObject *exc = _PyErr_GetRaisedException(tstate);
|
2019-09-23 09:45:47 -03:00
|
|
|
|
|
|
|
PySys_WriteStderr("Python path configuration:\n");
|
|
|
|
|
|
|
|
#define DUMP_CONFIG(NAME, FIELD) \
|
|
|
|
do { \
|
|
|
|
PySys_WriteStderr(" " NAME " = "); \
|
|
|
|
init_dump_ascii_wstr(config->FIELD); \
|
|
|
|
PySys_WriteStderr("\n"); \
|
|
|
|
} while (0)
|
|
|
|
|
2020-04-12 22:04:28 -03:00
|
|
|
const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
|
2019-09-23 09:45:47 -03:00
|
|
|
DUMP_CONFIG("PYTHONHOME", home);
|
|
|
|
DUMP_CONFIG("PYTHONPATH", pythonpath_env);
|
|
|
|
DUMP_CONFIG("program name", program_name);
|
|
|
|
PySys_WriteStderr(" isolated = %i\n", config->isolated);
|
|
|
|
PySys_WriteStderr(" environment = %i\n", config->use_environment);
|
|
|
|
PySys_WriteStderr(" user site = %i\n", config->user_site_directory);
|
2022-05-05 20:34:11 -03:00
|
|
|
PySys_WriteStderr(" safe_path = %i\n", config->safe_path);
|
2019-09-23 09:45:47 -03:00
|
|
|
PySys_WriteStderr(" import site = %i\n", config->site_import);
|
2021-12-02 20:08:42 -04:00
|
|
|
PySys_WriteStderr(" is in build tree = %i\n", config->_is_python_build);
|
2021-09-28 15:18:28 -03:00
|
|
|
DUMP_CONFIG("stdlib dir", stdlib_dir);
|
2023-10-02 16:59:05 -03:00
|
|
|
DUMP_CONFIG("sys.path[0]", sys_path_0);
|
2019-09-23 09:45:47 -03:00
|
|
|
#undef DUMP_CONFIG
|
|
|
|
|
|
|
|
#define DUMP_SYS(NAME) \
|
|
|
|
do { \
|
|
|
|
obj = PySys_GetObject(#NAME); \
|
|
|
|
PySys_FormatStderr(" sys.%s = ", #NAME); \
|
|
|
|
if (obj != NULL) { \
|
|
|
|
PySys_FormatStderr("%A", obj); \
|
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
PySys_WriteStderr("(not set)"); \
|
|
|
|
} \
|
|
|
|
PySys_FormatStderr("\n"); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
PyObject *obj;
|
|
|
|
DUMP_SYS(_base_executable);
|
|
|
|
DUMP_SYS(base_prefix);
|
|
|
|
DUMP_SYS(base_exec_prefix);
|
2020-06-08 12:28:11 -03:00
|
|
|
DUMP_SYS(platlibdir);
|
2019-09-23 09:45:47 -03:00
|
|
|
DUMP_SYS(executable);
|
|
|
|
DUMP_SYS(prefix);
|
|
|
|
DUMP_SYS(exec_prefix);
|
|
|
|
#undef DUMP_SYS
|
|
|
|
|
|
|
|
PyObject *sys_path = PySys_GetObject("path"); /* borrowed reference */
|
|
|
|
if (sys_path != NULL && PyList_Check(sys_path)) {
|
|
|
|
PySys_WriteStderr(" sys.path = [\n");
|
|
|
|
Py_ssize_t len = PyList_GET_SIZE(sys_path);
|
|
|
|
for (Py_ssize_t i=0; i < len; i++) {
|
|
|
|
PyObject *path = PyList_GET_ITEM(sys_path, i);
|
|
|
|
PySys_FormatStderr(" %A,\n", path);
|
|
|
|
}
|
|
|
|
PySys_WriteStderr(" ]\n");
|
|
|
|
}
|
|
|
|
|
2023-02-08 05:31:12 -04:00
|
|
|
_PyErr_SetRaisedException(tstate, exc);
|
2019-09-23 09:45:47 -03:00
|
|
|
}
|
2024-09-02 18:25:08 -03:00
|
|
|
|
|
|
|
|
2024-09-03 09:33:49 -03:00
|
|
|
// --- PyInitConfig API ---------------------------------------------------
|
|
|
|
|
|
|
|
struct PyInitConfig {
|
|
|
|
PyPreConfig preconfig;
|
|
|
|
PyConfig config;
|
2024-09-04 08:41:57 -03:00
|
|
|
struct _inittab *inittab;
|
|
|
|
Py_ssize_t inittab_size;
|
2024-09-03 09:33:49 -03:00
|
|
|
PyStatus status;
|
|
|
|
char *err_msg;
|
|
|
|
};
|
2024-09-02 18:25:08 -03:00
|
|
|
|
2024-09-03 09:33:49 -03:00
|
|
|
static PyInitConfig*
|
|
|
|
initconfig_alloc(void)
|
2024-09-02 18:25:08 -03:00
|
|
|
{
|
2024-09-03 09:33:49 -03:00
|
|
|
return calloc(1, sizeof(PyInitConfig));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyInitConfig*
|
|
|
|
PyInitConfig_Create(void)
|
|
|
|
{
|
|
|
|
PyInitConfig *config = initconfig_alloc();
|
|
|
|
if (config == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyPreConfig_InitIsolatedConfig(&config->preconfig);
|
|
|
|
PyConfig_InitIsolatedConfig(&config->config);
|
|
|
|
config->status = _PyStatus_OK();
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
PyInitConfig_Free(PyInitConfig *config)
|
|
|
|
{
|
2024-10-15 06:21:16 -03:00
|
|
|
if (config == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2024-09-03 09:33:49 -03:00
|
|
|
free(config->err_msg);
|
|
|
|
free(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
PyInitConfig_GetError(PyInitConfig* config, const char **perr_msg)
|
|
|
|
{
|
|
|
|
if (_PyStatus_IS_EXIT(config->status)) {
|
|
|
|
char buffer[22]; // len("exit code -2147483648\0")
|
|
|
|
PyOS_snprintf(buffer, sizeof(buffer),
|
|
|
|
"exit code %i",
|
|
|
|
config->status.exitcode);
|
|
|
|
|
|
|
|
if (config->err_msg != NULL) {
|
|
|
|
free(config->err_msg);
|
|
|
|
}
|
|
|
|
config->err_msg = strdup(buffer);
|
|
|
|
if (config->err_msg != NULL) {
|
|
|
|
*perr_msg = config->err_msg;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
config->status = _PyStatus_NO_MEMORY();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_PyStatus_IS_ERROR(config->status) && config->status.err_msg != NULL) {
|
|
|
|
*perr_msg = config->status.err_msg;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*perr_msg = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
PyInitConfig_GetExitCode(PyInitConfig* config, int *exitcode)
|
|
|
|
{
|
|
|
|
if (_PyStatus_IS_EXIT(config->status)) {
|
|
|
|
*exitcode = config->status.exitcode;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
initconfig_set_error(PyInitConfig *config, const char *err_msg)
|
|
|
|
{
|
|
|
|
config->status = _PyStatus_ERR(err_msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const PyConfigSpec*
|
|
|
|
initconfig_find_spec(const PyConfigSpec *spec, const char *name)
|
|
|
|
{
|
|
|
|
for (; spec->name != NULL; spec++) {
|
|
|
|
if (strcmp(name, spec->name) == 0) {
|
|
|
|
return spec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
PyInitConfig_HasOption(PyInitConfig *config, const char *name)
|
|
|
|
{
|
|
|
|
const PyConfigSpec *spec = initconfig_find_spec(PYCONFIG_SPEC, name);
|
|
|
|
if (spec == NULL) {
|
|
|
|
spec = initconfig_find_spec(PYPRECONFIG_SPEC, name);
|
|
|
|
}
|
|
|
|
return (spec != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const PyConfigSpec*
|
|
|
|
initconfig_prepare(PyInitConfig *config, const char *name, void **raw_member)
|
|
|
|
{
|
|
|
|
const PyConfigSpec *spec = initconfig_find_spec(PYCONFIG_SPEC, name);
|
|
|
|
if (spec != NULL) {
|
|
|
|
*raw_member = config_get_spec_member(&config->config, spec);
|
|
|
|
return spec;
|
|
|
|
}
|
|
|
|
|
|
|
|
spec = initconfig_find_spec(PYPRECONFIG_SPEC, name);
|
|
|
|
if (spec != NULL) {
|
|
|
|
*raw_member = preconfig_get_spec_member(&config->preconfig, spec);
|
|
|
|
return spec;
|
|
|
|
}
|
|
|
|
|
|
|
|
initconfig_set_error(config, "unknown config option name");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
PyInitConfig_GetInt(PyInitConfig *config, const char *name, int64_t *value)
|
|
|
|
{
|
|
|
|
void *raw_member;
|
|
|
|
const PyConfigSpec *spec = initconfig_prepare(config, name, &raw_member);
|
|
|
|
if (spec == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (spec->type) {
|
|
|
|
case PyConfig_MEMBER_INT:
|
|
|
|
case PyConfig_MEMBER_UINT:
|
|
|
|
case PyConfig_MEMBER_BOOL:
|
|
|
|
{
|
|
|
|
int *member = raw_member;
|
|
|
|
*value = *member;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PyConfig_MEMBER_ULONG:
|
|
|
|
{
|
|
|
|
unsigned long *member = raw_member;
|
|
|
|
#if SIZEOF_LONG >= 8
|
|
|
|
if ((unsigned long)INT64_MAX < *member) {
|
|
|
|
initconfig_set_error(config,
|
|
|
|
"config option value doesn't fit into int64_t");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
*value = *member;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
initconfig_set_error(config, "config option type is not int");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char*
|
|
|
|
wstr_to_utf8(PyInitConfig *config, wchar_t *wstr)
|
|
|
|
{
|
|
|
|
char *utf8;
|
|
|
|
int res = _Py_EncodeUTF8Ex(wstr, &utf8, NULL, NULL, 1, _Py_ERROR_STRICT);
|
|
|
|
if (res == -2) {
|
|
|
|
initconfig_set_error(config, "encoding error");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (res < 0) {
|
|
|
|
config->status = _PyStatus_NO_MEMORY();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy to use the malloc() memory allocator
|
|
|
|
size_t size = strlen(utf8) + 1;
|
|
|
|
char *str = malloc(size);
|
|
|
|
if (str == NULL) {
|
|
|
|
PyMem_RawFree(utf8);
|
|
|
|
config->status = _PyStatus_NO_MEMORY();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(str, utf8, size);
|
|
|
|
PyMem_RawFree(utf8);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
PyInitConfig_GetStr(PyInitConfig *config, const char *name, char **value)
|
|
|
|
{
|
|
|
|
void *raw_member;
|
|
|
|
const PyConfigSpec *spec = initconfig_prepare(config, name, &raw_member);
|
|
|
|
if (spec == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spec->type != PyConfig_MEMBER_WSTR
|
|
|
|
&& spec->type != PyConfig_MEMBER_WSTR_OPT)
|
|
|
|
{
|
|
|
|
initconfig_set_error(config, "config option type is not string");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t **member = raw_member;
|
|
|
|
if (*member == NULL) {
|
|
|
|
*value = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*value = wstr_to_utf8(config, *member);
|
|
|
|
if (*value == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
PyInitConfig_GetStrList(PyInitConfig *config, const char *name, size_t *length, char ***items)
|
|
|
|
{
|
|
|
|
void *raw_member;
|
|
|
|
const PyConfigSpec *spec = initconfig_prepare(config, name, &raw_member);
|
|
|
|
if (spec == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spec->type != PyConfig_MEMBER_WSTR_LIST) {
|
|
|
|
initconfig_set_error(config, "config option type is not string list");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyWideStringList *list = raw_member;
|
|
|
|
*length = list->length;
|
|
|
|
|
|
|
|
*items = malloc(list->length * sizeof(char*));
|
|
|
|
if (*items == NULL) {
|
|
|
|
config->status = _PyStatus_NO_MEMORY();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Py_ssize_t i=0; i < list->length; i++) {
|
|
|
|
(*items)[i] = wstr_to_utf8(config, list->items[i]);
|
|
|
|
if ((*items)[i] == NULL) {
|
|
|
|
PyInitConfig_FreeStrList(i, *items);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
PyInitConfig_FreeStrList(size_t length, char **items)
|
|
|
|
{
|
|
|
|
for (size_t i=0; i < length; i++) {
|
|
|
|
free(items[i]);
|
|
|
|
}
|
|
|
|
free(items);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
PyInitConfig_SetInt(PyInitConfig *config, const char *name, int64_t value)
|
|
|
|
{
|
|
|
|
void *raw_member;
|
|
|
|
const PyConfigSpec *spec = initconfig_prepare(config, name, &raw_member);
|
|
|
|
if (spec == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (spec->type) {
|
|
|
|
case PyConfig_MEMBER_INT:
|
|
|
|
{
|
|
|
|
if (value < (int64_t)INT_MIN || (int64_t)INT_MAX < value) {
|
|
|
|
initconfig_set_error(config,
|
|
|
|
"config option value is out of int range");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int int_value = (int)value;
|
|
|
|
|
|
|
|
int *member = raw_member;
|
|
|
|
*member = int_value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PyConfig_MEMBER_UINT:
|
|
|
|
case PyConfig_MEMBER_BOOL:
|
|
|
|
{
|
|
|
|
if (value < 0 || (uint64_t)UINT_MAX < (uint64_t)value) {
|
|
|
|
initconfig_set_error(config,
|
|
|
|
"config option value is out of unsigned int range");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int int_value = (int)value;
|
|
|
|
|
|
|
|
int *member = raw_member;
|
|
|
|
*member = int_value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PyConfig_MEMBER_ULONG:
|
|
|
|
{
|
|
|
|
if (value < 0 || (uint64_t)ULONG_MAX < (uint64_t)value) {
|
|
|
|
initconfig_set_error(config,
|
|
|
|
"config option value is out of unsigned long range");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
unsigned long ulong_value = (unsigned long)value;
|
|
|
|
|
|
|
|
unsigned long *member = raw_member;
|
|
|
|
*member = ulong_value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
initconfig_set_error(config, "config option type is not int");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-09-04 07:58:32 -03:00
|
|
|
if (strcmp(name, "hash_seed") == 0) {
|
2024-09-03 09:33:49 -03:00
|
|
|
config->config.use_hash_seed = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2024-09-02 18:25:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-03 09:33:49 -03:00
|
|
|
static wchar_t*
|
|
|
|
utf8_to_wstr(PyInitConfig *config, const char *str)
|
|
|
|
{
|
|
|
|
wchar_t *wstr;
|
|
|
|
size_t wlen;
|
|
|
|
int res = _Py_DecodeUTF8Ex(str, strlen(str), &wstr, &wlen, NULL, _Py_ERROR_STRICT);
|
|
|
|
if (res == -2) {
|
|
|
|
initconfig_set_error(config, "decoding error");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (res < 0) {
|
|
|
|
config->status = _PyStatus_NO_MEMORY();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy to use the malloc() memory allocator
|
|
|
|
size_t size = (wlen + 1) * sizeof(wchar_t);
|
|
|
|
wchar_t *wstr2 = malloc(size);
|
|
|
|
if (wstr2 == NULL) {
|
|
|
|
PyMem_RawFree(wstr);
|
|
|
|
config->status = _PyStatus_NO_MEMORY();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(wstr2, wstr, size);
|
|
|
|
PyMem_RawFree(wstr);
|
|
|
|
return wstr2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
PyInitConfig_SetStr(PyInitConfig *config, const char *name, const char* value)
|
|
|
|
{
|
|
|
|
void *raw_member;
|
|
|
|
const PyConfigSpec *spec = initconfig_prepare(config, name, &raw_member);
|
|
|
|
if (spec == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spec->type != PyConfig_MEMBER_WSTR
|
|
|
|
&& spec->type != PyConfig_MEMBER_WSTR_OPT) {
|
|
|
|
initconfig_set_error(config, "config option type is not string");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value == NULL && spec->type != PyConfig_MEMBER_WSTR_OPT) {
|
|
|
|
initconfig_set_error(config, "config option string cannot be NULL");
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t **member = raw_member;
|
|
|
|
|
|
|
|
*member = utf8_to_wstr(config, value);
|
|
|
|
if (*member == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
_PyWideStringList_FromUTF8(PyInitConfig *config, PyWideStringList *list,
|
|
|
|
Py_ssize_t length, char * const *items)
|
|
|
|
{
|
|
|
|
PyWideStringList wlist = _PyWideStringList_INIT;
|
|
|
|
size_t size = sizeof(wchar_t*) * length;
|
|
|
|
wlist.items = (wchar_t **)PyMem_RawMalloc(size);
|
|
|
|
if (wlist.items == NULL) {
|
|
|
|
config->status = _PyStatus_NO_MEMORY();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Py_ssize_t i = 0; i < length; i++) {
|
|
|
|
wchar_t *arg = utf8_to_wstr(config, items[i]);
|
|
|
|
if (arg == NULL) {
|
|
|
|
_PyWideStringList_Clear(&wlist);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
wlist.items[i] = arg;
|
|
|
|
wlist.length++;
|
|
|
|
}
|
|
|
|
|
|
|
|
_PyWideStringList_Clear(list);
|
|
|
|
*list = wlist;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
PyInitConfig_SetStrList(PyInitConfig *config, const char *name,
|
|
|
|
size_t length, char * const *items)
|
|
|
|
{
|
|
|
|
void *raw_member;
|
|
|
|
const PyConfigSpec *spec = initconfig_prepare(config, name, &raw_member);
|
|
|
|
if (spec == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spec->type != PyConfig_MEMBER_WSTR_LIST) {
|
|
|
|
initconfig_set_error(config, "config option type is not strings list");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
PyWideStringList *list = raw_member;
|
2024-09-04 07:58:32 -03:00
|
|
|
if (_PyWideStringList_FromUTF8(config, list, length, items) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(name, "module_search_paths") == 0) {
|
|
|
|
config->config.module_search_paths_set = 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2024-09-03 09:33:49 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-04 08:41:57 -03:00
|
|
|
int
|
|
|
|
PyInitConfig_AddModule(PyInitConfig *config, const char *name,
|
|
|
|
PyObject* (*initfunc)(void))
|
|
|
|
{
|
|
|
|
size_t size = sizeof(struct _inittab) * (config->inittab_size + 2);
|
|
|
|
struct _inittab *new_inittab = PyMem_RawRealloc(config->inittab, size);
|
|
|
|
if (new_inittab == NULL) {
|
|
|
|
config->status = _PyStatus_NO_MEMORY();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
config->inittab = new_inittab;
|
|
|
|
|
|
|
|
struct _inittab *entry = &config->inittab[config->inittab_size];
|
|
|
|
entry->name = name;
|
|
|
|
entry->initfunc = initfunc;
|
|
|
|
|
|
|
|
// Terminator entry
|
|
|
|
entry = &config->inittab[config->inittab_size + 1];
|
|
|
|
entry->name = NULL;
|
|
|
|
entry->initfunc = NULL;
|
|
|
|
|
|
|
|
config->inittab_size++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-03 09:33:49 -03:00
|
|
|
int
|
|
|
|
Py_InitializeFromInitConfig(PyInitConfig *config)
|
|
|
|
{
|
2024-09-04 08:41:57 -03:00
|
|
|
if (config->inittab_size >= 1) {
|
|
|
|
if (PyImport_ExtendInittab(config->inittab) < 0) {
|
|
|
|
config->status = _PyStatus_NO_MEMORY();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-03 09:33:49 -03:00
|
|
|
_PyPreConfig_GetConfig(&config->preconfig, &config->config);
|
|
|
|
|
|
|
|
config->status = Py_PreInitializeFromArgs(
|
|
|
|
&config->preconfig,
|
|
|
|
config->config.argv.length,
|
|
|
|
config->config.argv.items);
|
|
|
|
if (_PyStatus_EXCEPTION(config->status)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
config->status = Py_InitializeFromConfig(&config->config);
|
|
|
|
if (_PyStatus_EXCEPTION(config->status)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// --- PyConfig_Get() -------------------------------------------------------
|
|
|
|
|
2024-09-02 18:25:08 -03:00
|
|
|
static const PyConfigSpec*
|
|
|
|
config_generic_find_spec(const PyConfigSpec *spec, const char *name)
|
|
|
|
{
|
|
|
|
for (; spec->name != NULL; spec++) {
|
|
|
|
if (spec->visibility == PyConfig_MEMBER_INIT_ONLY) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strcmp(name, spec->name) == 0) {
|
|
|
|
return spec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const PyConfigSpec*
|
|
|
|
config_find_spec(const char *name)
|
|
|
|
{
|
|
|
|
return config_generic_find_spec(PYCONFIG_SPEC, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const PyConfigSpec*
|
|
|
|
preconfig_find_spec(const char *name)
|
|
|
|
{
|
|
|
|
return config_generic_find_spec(PYPRECONFIG_SPEC, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
config_add_xoption(PyObject *dict, const wchar_t *str)
|
|
|
|
{
|
|
|
|
PyObject *name = NULL, *value = NULL;
|
|
|
|
|
|
|
|
const wchar_t *name_end = wcschr(str, L'=');
|
|
|
|
if (!name_end) {
|
|
|
|
name = PyUnicode_FromWideChar(str, -1);
|
|
|
|
if (name == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
value = Py_NewRef(Py_True);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
name = PyUnicode_FromWideChar(str, name_end - str);
|
|
|
|
if (name == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
value = PyUnicode_FromWideChar(name_end + 1, -1);
|
|
|
|
if (value == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (PyDict_SetItem(dict, name, value) < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
Py_DECREF(name);
|
|
|
|
Py_DECREF(value);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
Py_XDECREF(name);
|
|
|
|
Py_XDECREF(value);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject*
|
|
|
|
_PyConfig_CreateXOptionsDict(const PyConfig *config)
|
|
|
|
{
|
|
|
|
PyObject *dict = PyDict_New();
|
|
|
|
if (dict == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_ssize_t nxoption = config->xoptions.length;
|
|
|
|
wchar_t **xoptions = config->xoptions.items;
|
|
|
|
for (Py_ssize_t i=0; i < nxoption; i++) {
|
|
|
|
const wchar_t *option = xoptions[i];
|
|
|
|
if (config_add_xoption(dict, option) < 0) {
|
|
|
|
Py_DECREF(dict);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
config_get_sys(const char *name)
|
|
|
|
{
|
|
|
|
PyObject *value = PySys_GetObject(name);
|
|
|
|
if (value == NULL) {
|
|
|
|
PyErr_Format(PyExc_RuntimeError, "lost sys.%s", name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return Py_NewRef(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
config_get_sys_write_bytecode(const PyConfig *config, int *value)
|
|
|
|
{
|
|
|
|
PyObject *attr = config_get_sys("dont_write_bytecode");
|
|
|
|
if (attr == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int is_true = PyObject_IsTrue(attr);
|
|
|
|
Py_DECREF(attr);
|
|
|
|
if (is_true < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*value = (!is_true);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
config_get(const PyConfig *config, const PyConfigSpec *spec,
|
|
|
|
int use_sys)
|
|
|
|
{
|
|
|
|
if (use_sys) {
|
|
|
|
if (spec->sys.attr != NULL) {
|
|
|
|
return config_get_sys(spec->sys.attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(spec->name, "write_bytecode") == 0) {
|
|
|
|
int value;
|
|
|
|
if (config_get_sys_write_bytecode(config, &value) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyBool_FromLong(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(spec->name, "int_max_str_digits") == 0) {
|
|
|
|
PyInterpreterState *interp = _PyInterpreterState_GET();
|
|
|
|
return PyLong_FromLong(interp->long_state.max_str_digits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-03 09:33:49 -03:00
|
|
|
void *member = config_get_spec_member(config, spec);
|
2024-09-02 18:25:08 -03:00
|
|
|
switch (spec->type) {
|
|
|
|
case PyConfig_MEMBER_INT:
|
|
|
|
case PyConfig_MEMBER_UINT:
|
|
|
|
{
|
|
|
|
int value = *(int *)member;
|
|
|
|
return PyLong_FromLong(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
case PyConfig_MEMBER_BOOL:
|
|
|
|
{
|
|
|
|
int value = *(int *)member;
|
|
|
|
return PyBool_FromLong(value != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
case PyConfig_MEMBER_ULONG:
|
|
|
|
{
|
|
|
|
unsigned long value = *(unsigned long *)member;
|
|
|
|
return PyLong_FromUnsignedLong(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
case PyConfig_MEMBER_WSTR:
|
|
|
|
case PyConfig_MEMBER_WSTR_OPT:
|
|
|
|
{
|
|
|
|
wchar_t *wstr = *(wchar_t **)member;
|
|
|
|
if (wstr != NULL) {
|
|
|
|
return PyUnicode_FromWideChar(wstr, -1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Py_NewRef(Py_None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case PyConfig_MEMBER_WSTR_LIST:
|
|
|
|
{
|
|
|
|
if (strcmp(spec->name, "xoptions") == 0) {
|
|
|
|
return _PyConfig_CreateXOptionsDict(config);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const PyWideStringList *list = (const PyWideStringList *)member;
|
|
|
|
return _PyWideStringList_AsTuple(list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
Py_UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
preconfig_get(const PyPreConfig *preconfig, const PyConfigSpec *spec)
|
|
|
|
{
|
|
|
|
// The type of all PYPRECONFIG_SPEC members is INT or BOOL.
|
|
|
|
assert(spec->type == PyConfig_MEMBER_INT
|
|
|
|
|| spec->type == PyConfig_MEMBER_BOOL);
|
|
|
|
|
|
|
|
char *member = (char *)preconfig + spec->offset;
|
|
|
|
int value = *(int *)member;
|
|
|
|
|
|
|
|
if (spec->type == PyConfig_MEMBER_BOOL) {
|
|
|
|
return PyBool_FromLong(value != 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return PyLong_FromLong(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
config_unknown_name_error(const char *name)
|
|
|
|
{
|
|
|
|
PyErr_Format(PyExc_ValueError, "unknown config option name: %s", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject*
|
|
|
|
PyConfig_Get(const char *name)
|
|
|
|
{
|
|
|
|
const PyConfigSpec *spec = config_find_spec(name);
|
|
|
|
if (spec != NULL) {
|
|
|
|
const PyConfig *config = _Py_GetConfig();
|
|
|
|
return config_get(config, spec, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
spec = preconfig_find_spec(name);
|
|
|
|
if (spec != NULL) {
|
|
|
|
const PyPreConfig *preconfig = &_PyRuntime.preconfig;
|
|
|
|
return preconfig_get(preconfig, spec);
|
|
|
|
}
|
|
|
|
|
|
|
|
config_unknown_name_error(name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
PyConfig_GetInt(const char *name, int *value)
|
|
|
|
{
|
|
|
|
assert(!PyErr_Occurred());
|
|
|
|
|
|
|
|
PyObject *obj = PyConfig_Get(name);
|
|
|
|
if (obj == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PyLong_Check(obj)) {
|
|
|
|
Py_DECREF(obj);
|
|
|
|
PyErr_Format(PyExc_TypeError, "config option %s is not an int", name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int as_int = PyLong_AsInt(obj);
|
|
|
|
Py_DECREF(obj);
|
|
|
|
if (as_int == -1 && PyErr_Occurred()) {
|
|
|
|
PyErr_Format(PyExc_OverflowError,
|
|
|
|
"config option %s value does not fit into a C int", name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*value = as_int;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
config_names_add(PyObject *names, const PyConfigSpec *spec)
|
|
|
|
{
|
|
|
|
for (; spec->name != NULL; spec++) {
|
|
|
|
if (spec->visibility == PyConfig_MEMBER_INIT_ONLY) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
PyObject *name = PyUnicode_FromString(spec->name);
|
|
|
|
if (name == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int res = PyList_Append(names, name);
|
|
|
|
Py_DECREF(name);
|
|
|
|
if (res < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject*
|
|
|
|
PyConfig_Names(void)
|
|
|
|
{
|
|
|
|
PyObject *names = PyList_New(0);
|
|
|
|
if (names == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config_names_add(names, PYCONFIG_SPEC) < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (config_names_add(names, PYPRECONFIG_SPEC) < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *frozen = PyFrozenSet_New(names);
|
|
|
|
Py_DECREF(names);
|
|
|
|
return frozen;
|
|
|
|
|
|
|
|
error:
|
|
|
|
Py_XDECREF(names);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// --- PyConfig_Set() -------------------------------------------------------
|
|
|
|
|
|
|
|
static int
|
|
|
|
config_set_sys_flag(const PyConfigSpec *spec, int int_value)
|
|
|
|
{
|
|
|
|
PyInterpreterState *interp = _PyInterpreterState_GET();
|
|
|
|
PyConfig *config = &interp->config;
|
|
|
|
|
|
|
|
if (spec->type == PyConfig_MEMBER_BOOL) {
|
|
|
|
if (int_value != 0) {
|
|
|
|
// convert values < 0 and values > 1 to 1
|
|
|
|
int_value = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *value;
|
|
|
|
if (spec->sys.flag_setter) {
|
|
|
|
value = spec->sys.flag_setter(int_value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
value = config_sys_flag_long(int_value);
|
|
|
|
}
|
|
|
|
if (value == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sys.flags.FLAG
|
|
|
|
Py_ssize_t pos = spec->sys.flag_index;
|
|
|
|
if (_PySys_SetFlagObj(pos, value) < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set PyConfig.ATTR
|
|
|
|
assert(spec->type == PyConfig_MEMBER_INT
|
|
|
|
|| spec->type == PyConfig_MEMBER_UINT
|
|
|
|
|| spec->type == PyConfig_MEMBER_BOOL);
|
2024-09-03 09:33:49 -03:00
|
|
|
int *member = config_get_spec_member(config, spec);
|
2024-09-02 18:25:08 -03:00
|
|
|
*member = int_value;
|
|
|
|
|
|
|
|
// Set sys.dont_write_bytecode attribute
|
|
|
|
if (strcmp(spec->name, "write_bytecode") == 0) {
|
|
|
|
if (PySys_SetObject("dont_write_bytecode", value) < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_DECREF(value);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
Py_DECREF(value);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
PyConfig_Set(const char *name, PyObject *value)
|
|
|
|
{
|
|
|
|
const PyConfigSpec *spec = config_find_spec(name);
|
|
|
|
if (spec == NULL) {
|
|
|
|
spec = preconfig_find_spec(name);
|
|
|
|
if (spec == NULL) {
|
|
|
|
config_unknown_name_error(name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
assert(spec->visibility != PyConfig_MEMBER_PUBLIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spec->visibility != PyConfig_MEMBER_PUBLIC) {
|
|
|
|
PyErr_Format(PyExc_ValueError, "cannot set read-only option %s",
|
|
|
|
name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int int_value = 0;
|
|
|
|
int has_int_value = 0;
|
|
|
|
|
|
|
|
switch (spec->type) {
|
|
|
|
case PyConfig_MEMBER_INT:
|
|
|
|
case PyConfig_MEMBER_UINT:
|
|
|
|
case PyConfig_MEMBER_BOOL:
|
|
|
|
if (!PyLong_Check(value)) {
|
|
|
|
PyErr_Format(PyExc_TypeError, "expected int or bool, got %T", value);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int_value = PyLong_AsInt(value);
|
|
|
|
if (int_value == -1 && PyErr_Occurred()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (int_value < 0 && spec->type != PyConfig_MEMBER_INT) {
|
|
|
|
PyErr_Format(PyExc_ValueError, "value must be >= 0");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
has_int_value = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PyConfig_MEMBER_ULONG:
|
|
|
|
// not implemented: only hash_seed uses this type, and it's read-only
|
|
|
|
goto cannot_set;
|
|
|
|
|
|
|
|
case PyConfig_MEMBER_WSTR:
|
|
|
|
if (!PyUnicode_CheckExact(value)) {
|
|
|
|
PyErr_Format(PyExc_TypeError, "expected str, got %T", value);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PyConfig_MEMBER_WSTR_OPT:
|
|
|
|
if (value != Py_None && !PyUnicode_CheckExact(value)) {
|
|
|
|
PyErr_Format(PyExc_TypeError, "expected str or None, got %T", value);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PyConfig_MEMBER_WSTR_LIST:
|
|
|
|
if (strcmp(spec->name, "xoptions") != 0) {
|
|
|
|
if (!PyList_Check(value)) {
|
|
|
|
PyErr_Format(PyExc_TypeError, "expected list[str], got %T",
|
|
|
|
value);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (Py_ssize_t i=0; i < PyList_GET_SIZE(value); i++) {
|
|
|
|
PyObject *item = PyList_GET_ITEM(value, i);
|
|
|
|
if (!PyUnicode_Check(item)) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"expected str, list item %zd has type %T",
|
|
|
|
i, item);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// xoptions type is dict[str, str]
|
|
|
|
if (!PyDict_Check(value)) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"expected dict[str, str | bool], got %T",
|
|
|
|
value);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_ssize_t pos = 0;
|
|
|
|
PyObject *key, *item;
|
|
|
|
while (PyDict_Next(value, &pos, &key, &item)) {
|
|
|
|
if (!PyUnicode_Check(key)) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"expected str, "
|
|
|
|
"got dict key type %T", key);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!PyUnicode_Check(item) && !PyBool_Check(item)) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"expected str or bool, "
|
|
|
|
"got dict value type %T", key);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
Py_UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (spec->sys.attr != NULL) {
|
|
|
|
// Set the sys attribute, but don't set PyInterpreterState.config
|
|
|
|
// to keep the code simple.
|
|
|
|
return PySys_SetObject(spec->sys.attr, value);
|
|
|
|
}
|
|
|
|
else if (spec->sys.flag_index >= 0 && has_int_value) {
|
|
|
|
return config_set_sys_flag(spec, int_value);
|
|
|
|
}
|
|
|
|
else if (strcmp(spec->name, "int_max_str_digits") == 0 && has_int_value) {
|
|
|
|
return _PySys_SetIntMaxStrDigits(int_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
cannot_set:
|
|
|
|
PyErr_Format(PyExc_ValueError, "cannot set option %s", name);
|
|
|
|
return -1;
|
|
|
|
}
|