Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 17 additions & 60 deletions Include/cpython/coreconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,27 +115,8 @@ typedef struct {
PyMemAllocatorName allocator;
} _PyPreConfig;

#ifdef MS_WINDOWS
# define _PyPreConfig_WINDOWS_INIT \
.legacy_windows_fs_encoding = -1,
#else
# define _PyPreConfig_WINDOWS_INIT
#endif

#define _PyPreConfig_INIT \
(_PyPreConfig){ \
_PyPreConfig_WINDOWS_INIT \
._config_version = _Py_CONFIG_VERSION, \
.isolated = -1, \
.use_environment = -1, \
.configure_locale = 1, \
.utf8_mode = -2, \
.dev_mode = -1, \
.allocator = PYMEM_ALLOCATOR_NOT_SET}

PyAPI_FUNC(void) _PyPreConfig_Init(_PyPreConfig *config);
PyAPI_FUNC(void) _PyPreConfig_InitPythonConfig(_PyPreConfig *config);
PyAPI_FUNC(void) _PyPreConfig_InitIsolateConfig(_PyPreConfig *config);
PyAPI_FUNC(void) _PyPreConfig_InitIsolatedConfig(_PyPreConfig *config);


/* --- _PyCoreConfig ---------------------------------------------- */
Expand Down Expand Up @@ -419,47 +400,23 @@ typedef struct {

} _PyCoreConfig;

#ifdef MS_WINDOWS
# define _PyCoreConfig_WINDOWS_INIT \
.legacy_windows_stdio = -1,
#else
# define _PyCoreConfig_WINDOWS_INIT
#endif

#define _PyCoreConfig_INIT \
(_PyCoreConfig){ \
_PyCoreConfig_WINDOWS_INIT \
._config_version = _Py_CONFIG_VERSION, \
.isolated = -1, \
.use_environment = -1, \
.dev_mode = -1, \
.install_signal_handlers = 1, \
.use_hash_seed = -1, \
.faulthandler = -1, \
.tracemalloc = -1, \
.use_module_search_paths = 0, \
.parse_argv = 0, \
.site_import = -1, \
.bytes_warning = -1, \
.inspect = -1, \
.interactive = -1, \
.optimization_level = -1, \
.parser_debug= -1, \
.write_bytecode = -1, \
.verbose = -1, \
.quiet = -1, \
.user_site_directory = -1, \
.configure_c_stdio = 0, \
.buffered_stdio = -1, \
._install_importlib = 1, \
.check_hash_pycs_mode = NULL, \
.pathconfig_warnings = -1, \
._init_main = 1}
/* Note: _PyCoreConfig_INIT sets other fields to 0/NULL */

PyAPI_FUNC(void) _PyCoreConfig_Init(_PyCoreConfig *config);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_InitPythonConfig(_PyCoreConfig *config);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_InitIsolateConfig(_PyCoreConfig *config);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_InitIsolatedConfig(_PyCoreConfig *config);
PyAPI_FUNC(void) _PyCoreConfig_Clear(_PyCoreConfig *);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetString(
wchar_t **config_str,
const wchar_t *str);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_DecodeLocale(
wchar_t **config_str,
const char *str);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(_PyCoreConfig *config);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetArgv(
_PyCoreConfig *config,
Py_ssize_t argc,
char **argv);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetWideArgv(_PyCoreConfig *config,
Py_ssize_t argc,
wchar_t **argv);

#ifdef __cplusplus
}
Expand Down
20 changes: 1 addition & 19 deletions Include/internal/pycore_coreconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ PyAPI_FUNC(_PyInitError) _PyPreCmdline_Read(_PyPreCmdline *cmdline,
/* --- _PyPreConfig ----------------------------------------------- */

PyAPI_FUNC(void) _PyPreConfig_Init(_PyPreConfig *config);
PyAPI_FUNC(void) _PyPreConfig_InitPythonConfig(_PyPreConfig *config);
PyAPI_FUNC(void) _PyPreConfig_InitIsolatedConfig(_PyPreConfig *config);
PyAPI_FUNC(void) _PyPreConfig_Copy(_PyPreConfig *config,
const _PyPreConfig *config2);
PyAPI_FUNC(PyObject*) _PyPreConfig_AsDict(const _PyPreConfig *config);
Expand All @@ -135,34 +133,18 @@ PyAPI_FUNC(_PyInitError) _PyPreConfig_Write(const _PyPreConfig *config);

/* --- _PyCoreConfig ---------------------------------------------- */

PyAPI_FUNC(void) _PyCoreConfig_Clear(_PyCoreConfig *);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_InitPythonConfig(_PyCoreConfig *config);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_InitIsolatedConfig(_PyCoreConfig *config);
PyAPI_FUNC(void) _PyCoreConfig_Init(_PyCoreConfig *config);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_Copy(
_PyCoreConfig *config,
const _PyCoreConfig *config2);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetString(
wchar_t **config_str,
const wchar_t *str);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_DecodeLocale(
wchar_t **config_str,
const char *str);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_InitPathConfig(_PyCoreConfig *config);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetPathConfig(
const _PyCoreConfig *config);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(_PyCoreConfig *config);
PyAPI_FUNC(void) _PyCoreConfig_Write(const _PyCoreConfig *config,
_PyRuntimeState *runtime);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetPyArgv(
_PyCoreConfig *config,
const _PyArgv *args);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetArgv(
_PyCoreConfig *config,
Py_ssize_t argc,
char **argv);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetWideArgv(_PyCoreConfig *config,
Py_ssize_t argc,
wchar_t **argv);


/* --- Function used for testing ---------------------------------- */
Expand Down
105 changes: 65 additions & 40 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,16 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
'coerce_c_locale_warn': 0,
'utf8_mode': 0,
}
ISOLATED_PRE_CONFIG = dict(DEFAULT_PRE_CONFIG,
configure_locale=0,
isolated=1,
use_environment=0,
utf8_mode=0,
dev_mode=0,
)
if MS_WINDOWS:
ISOLATED_PRE_CONFIG['legacy_windows_fs_encoding'] = 0

COPY_PRE_CONFIG = [
'dev_mode',
'isolated',
Expand Down Expand Up @@ -363,6 +373,24 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
'legacy_windows_stdio': 0,
})

PYTHON_CORE_CONFIG = dict(DEFAULT_CORE_CONFIG,
configure_c_stdio=1,
parse_argv=1,
)
ISOLATED_CORE_CONFIG = dict(DEFAULT_CORE_CONFIG,
isolated=1,
use_environment=0,
user_site_directory=0,
dev_mode=0,
install_signal_handlers=0,
use_hash_seed=0,
faulthandler=0,
tracemalloc=0,
pathconfig_warnings=0,
)
if MS_WINDOWS:
ISOLATED_CORE_CONFIG['legacy_windows_stdio'] = 0

# global config
DEFAULT_GLOBAL_CONFIG = {
'Py_HasFileSystemDefaultEncoding': 0,
Expand Down Expand Up @@ -410,8 +438,15 @@ def main_xoptions(self, xoptions_list):
xoptions[opt] = True
return xoptions

def get_expected_config(self, expected_preconfig, expected, env, add_path=None):
expected = dict(self.DEFAULT_CORE_CONFIG, **expected)
def get_expected_config(self, expected_preconfig, expected, env, api,
add_path=None):
if api == "python":
default_config = self.PYTHON_CORE_CONFIG
elif api == "isolated":
default_config = self.ISOLATED_CORE_CONFIG
else:
default_config = self.DEFAULT_CORE_CONFIG
expected = dict(default_config, **expected)

code = textwrap.dedent('''
import json
Expand Down Expand Up @@ -521,8 +556,8 @@ def check_global_config(self, config):

self.assertEqual(config['global_config'], expected)

def check_config(self, testname, expected_config, expected_preconfig,
add_path=None, stderr=None):
def check_config(self, testname, expected_config=None, expected_preconfig=None,
add_path=None, stderr=None, api="default"):
env = dict(os.environ)
# Remove PYTHON* environment variables to get deterministic environment
for key in list(env):
Expand All @@ -533,8 +568,18 @@ def check_config(self, testname, expected_config, expected_preconfig,
env['PYTHONCOERCECLOCALE'] = '0'
env['PYTHONUTF8'] = '0'

expected_preconfig = dict(self.DEFAULT_PRE_CONFIG, **expected_preconfig)
expected_config = self.get_expected_config(expected_preconfig, expected_config, env, add_path)
if api == "isolated":
default_preconfig = self.ISOLATED_PRE_CONFIG
else:
default_preconfig = self.DEFAULT_PRE_CONFIG
if expected_preconfig is None:
expected_preconfig = {}
expected_preconfig = dict(default_preconfig, **expected_preconfig)
if expected_config is None:
expected_config = {}
expected_config = self.get_expected_config(expected_preconfig,
expected_config, env,
api, add_path)
for key in self.COPY_PRE_CONFIG:
if key not in expected_preconfig:
expected_preconfig[key] = expected_config[key]
Expand Down Expand Up @@ -677,76 +722,56 @@ def test_init_dev_mode(self):
'dev_mode': 1,
'warnoptions': ['default'],
}
self.check_config("init_dev_mode", config, preconfig)
self.check_config("init_dev_mode", config, preconfig, api="python")

def test_init_isolated_flag(self):
preconfig = {}
config = {
'isolated': 1,
'use_environment': 0,
'user_site_directory': 0,
}
self.check_config("init_isolated_flag", config, preconfig)
self.check_config("init_isolated_flag", config, api="python")

def test_preinit_isolated1(self):
# _PyPreConfig.isolated=1, _PyCoreConfig.isolated not set
preconfig = {}
config = {
'isolated': 1,
'use_environment': 0,
'user_site_directory': 0,
}
self.check_config("preinit_isolated1", config, preconfig)
self.check_config("preinit_isolated1", config)

def test_preinit_isolated2(self):
# _PyPreConfig.isolated=0, _PyCoreConfig.isolated=1
preconfig = {}
config = {
'isolated': 1,
'use_environment': 0,
'user_site_directory': 0,
}
self.check_config("preinit_isolated2", config, preconfig)
self.check_config("preinit_isolated2", config)

def test_init_isolated_config(self):
preconfig = {
'configure_locale': 0,
}
config = {
'isolated': 1,
'use_environment': 0,
'user_site_directory': 0,
'install_signal_handlers': 0,
'pathconfig_warnings': 0,
}
self.check_config("init_isolated_config", config, preconfig)
self.check_config("init_isolated_config", api="isolated")

def test_init_python_config(self):
preconfig = {}
config = {
'configure_c_stdio': 1,
'parse_argv': 1,
}
self.check_config("init_python_config", config, preconfig)
self.check_config("init_python_config", api="python")

def test_init_dont_configure_locale(self):
# _PyPreConfig.configure_locale=0
preconfig = {
'configure_locale': 0,
}
self.check_config("init_dont_configure_locale", {}, preconfig)
self.check_config("init_dont_configure_locale", {}, preconfig, api="python")

def test_init_read_set(self):
preconfig = {}
core_config = {
'program_name': './init_read_set',
'executable': 'my_executable',
}
self.check_config("init_read_set", core_config, preconfig,
self.check_config("init_read_set", core_config, api="python",
add_path="init_read_set_path")

def test_init_run_main(self):
preconfig = {}
code = ('import _testinternalcapi, json; '
'print(json.dumps(_testinternalcapi.get_configs()))')
core_config = {
Expand All @@ -755,10 +780,9 @@ def test_init_run_main(self):
'run_command': code + '\n',
'parse_argv': 1,
}
self.check_config("init_run_main", core_config, preconfig)
self.check_config("init_run_main", core_config, api="python")

def test_init_main(self):
preconfig = {}
code = ('import _testinternalcapi, json; '
'print(json.dumps(_testinternalcapi.get_configs()))')
core_config = {
Expand All @@ -768,25 +792,26 @@ def test_init_main(self):
'parse_argv': 1,
'_init_main': 0,
}
self.check_config("init_main", core_config, preconfig,
self.check_config("init_main", core_config, api="python",
stderr="Run Python code before _Py_InitializeMain")

def test_init_parse_argv(self):
core_config = {
'parse_argv': 1,
'argv': ['-c', 'arg1', '-v', 'arg3'],
'program_name': './argv0',
'parse_argv': 1,
'run_command': 'pass\n',
'use_environment': 0,
}
self.check_config("init_parse_argv", core_config, {})
self.check_config("init_parse_argv", core_config, api="python")

def test_init_dont_parse_argv(self):
core_config = {
'parse_argv': 0,
'argv': ['./argv0', '-E', '-c', 'pass', 'arg1', '-v', 'arg3'],
'program_name': './argv0',
}
self.check_config("init_dont_parse_argv", core_config, {})
self.check_config("init_dont_parse_argv", core_config, api="python")


if __name__ == "__main__":
Expand Down
6 changes: 2 additions & 4 deletions Programs/_freeze_importlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,12 @@ main(int argc, char *argv[])
text[text_size] = '\0';

_PyCoreConfig config;
_PyCoreConfig_Init(&config);
config.use_environment = 0;
config.user_site_directory = 0;
_PyCoreConfig_InitIsolatedConfig(&config);

config.site_import = 0;
config.program_name = L"./_freeze_importlib";
/* Don't install importlib, since it could execute outdated bytecode. */
config._install_importlib = 0;
config.pathconfig_warnings = 0;
config._init_main = 0;

_PyInitError err = _Py_InitializeFromConfig(&config);
Expand Down
Loading