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
22 changes: 18 additions & 4 deletions Include/cpython/coreconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ typedef struct {
int _config_version; /* Internal configuration version,
used for ABI compatibility */

/* Parse _Py_PreInitializeFromArgs() arguments?
See _PyCoreConfig.parse_argv */
int parse_argv;

/* If greater than 0, enable isolated mode: sys.path contains
neither the script's directory nor the user's site-packages directory.

Expand Down Expand Up @@ -111,8 +115,9 @@ typedef struct {

int dev_mode; /* Development mode. PYTHONDEVMODE, -X dev */

/* Memory allocator: PYTHONMALLOC env var */
PyMemAllocatorName allocator;
/* Memory allocator: PYTHONMALLOC env var.
See PyMemAllocatorName for valid values. */
int allocator;
} _PyPreConfig;

PyAPI_FUNC(void) _PyPreConfig_InitPythonConfig(_PyPreConfig *config);
Expand All @@ -121,9 +126,16 @@ PyAPI_FUNC(void) _PyPreConfig_InitIsolatedConfig(_PyPreConfig *config);

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

typedef enum {
_PyCoreConfig_INIT = 0,
_PyCoreConfig_INIT_PYTHON = 1,
_PyCoreConfig_INIT_ISOLATED = 2
} _PyCoreConfigInitEnum;

typedef struct {
int _config_version; /* Internal configuration version,
used for ABI compatibility */
int _config_init; /* _PyCoreConfigInitEnum value */

int isolated; /* Isolated mode? see _PyPreConfig.isolated */
int use_environment; /* Use environment variables? see _PyPreConfig.use_environment */
Expand Down Expand Up @@ -401,19 +413,21 @@ PyAPI_FUNC(_PyInitError) _PyCoreConfig_InitPythonConfig(_PyCoreConfig *config);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_InitIsolatedConfig(_PyCoreConfig *config);
PyAPI_FUNC(void) _PyCoreConfig_Clear(_PyCoreConfig *);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetString(
_PyCoreConfig *config,
wchar_t **config_str,
const wchar_t *str);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_DecodeLocale(
_PyCoreConfig *config,
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);
char * const *argv);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetWideArgv(_PyCoreConfig *config,
Py_ssize_t argc,
wchar_t **argv);
wchar_t * const *argv);

#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions Include/cpython/pylifecycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ PyAPI_FUNC(_PyInitError) _Py_InitializeFromConfig(
PyAPI_FUNC(_PyInitError) _Py_InitializeFromArgs(
const _PyCoreConfig *config,
Py_ssize_t argc,
char **argv);
char * const *argv);
PyAPI_FUNC(_PyInitError) _Py_InitializeFromWideArgs(
const _PyCoreConfig *config,
Py_ssize_t argc,
wchar_t **argv);
wchar_t * const *argv);
PyAPI_FUNC(_PyInitError) _Py_InitializeMain(void);

PyAPI_FUNC(int) _Py_RunMain(void);
Expand Down
10 changes: 8 additions & 2 deletions Include/internal/pycore_coreconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ PyAPI_FUNC(int) _PyWstrList_Extend(_PyWstrList *list,
typedef struct {
Py_ssize_t argc;
int use_bytes_argv;
char **bytes_argv;
wchar_t **wchar_argv;
char * const *bytes_argv;
wchar_t * const *wchar_argv;
} _PyArgv;

PyAPI_FUNC(_PyInitError) _PyArgv_AsWstrList(const _PyArgv *args,
Expand Down Expand Up @@ -121,6 +121,12 @@ PyAPI_FUNC(_PyInitError) _PyPreCmdline_Read(_PyPreCmdline *cmdline,
/* --- _PyPreConfig ----------------------------------------------- */

PyAPI_FUNC(void) _PyPreConfig_Init(_PyPreConfig *config);
PyAPI_FUNC(void) _PyPreConfig_InitFromCoreConfig(
_PyPreConfig *config,
const _PyCoreConfig *coreconfig);
PyAPI_FUNC(void) _PyPreConfig_InitFromPreConfig(
_PyPreConfig *config,
const _PyPreConfig *config2);
PyAPI_FUNC(void) _PyPreConfig_Copy(_PyPreConfig *config,
const _PyPreConfig *config2);
PyAPI_FUNC(PyObject*) _PyPreConfig_AsDict(const _PyPreConfig *config);
Expand Down
96 changes: 74 additions & 22 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
PYMEM_ALLOCATOR_DEBUG = 2
PYMEM_ALLOCATOR_MALLOC = 3

CONFIG_INIT = 0
CONFIG_INIT_PYTHON = 1
CONFIG_INIT_ISOLATED = 2


class EmbeddingTestsMixin:
def setUp(self):
Expand Down Expand Up @@ -280,20 +284,26 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):

DEFAULT_PRE_CONFIG = {
'allocator': PYMEM_ALLOCATOR_NOT_SET,
'parse_argv': 0,
'configure_locale': 1,
'coerce_c_locale': 0,
'coerce_c_locale_warn': 0,
'utf8_mode': 0,
}
if MS_WINDOWS:
DEFAULT_PRE_CONFIG.update({
'legacy_windows_fs_encoding': 0,
})
PYTHON_PRE_CONFIG = dict(DEFAULT_PRE_CONFIG,
parse_argv=1,
)
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',
Expand All @@ -302,6 +312,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
]

DEFAULT_CORE_CONFIG = {
'_config_init': CONFIG_INIT,
'isolated': 0,
'use_environment': 1,
'dev_mode': 0,
Expand Down Expand Up @@ -365,9 +376,6 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
'_init_main': 1,
}
if MS_WINDOWS:
DEFAULT_PRE_CONFIG.update({
'legacy_windows_fs_encoding': 0,
})
DEFAULT_CORE_CONFIG.update({
'legacy_windows_stdio': 0,
})
Expand Down Expand Up @@ -439,13 +447,14 @@ def main_xoptions(self, xoptions_list):

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

code = textwrap.dedent('''
import json
Expand Down Expand Up @@ -519,9 +528,7 @@ def get_expected_config(self, expected_preconfig, expected, env, api,
return expected

def check_pre_config(self, config, expected):
pre_config = dict(config['pre_config'])
core_config = dict(config['core_config'])
self.assertEqual(pre_config, expected)
self.assertEqual(config['pre_config'], expected)

def check_core_config(self, config, expected):
core_config = dict(config['core_config'])
Expand Down Expand Up @@ -554,7 +561,7 @@ def check_global_config(self, config):
self.assertEqual(config['global_config'], expected)

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

if api == "isolated":
if api == CONFIG_INIT_ISOLATED:
default_preconfig = self.ISOLATED_PRE_CONFIG
elif api == CONFIG_INIT_PYTHON:
default_preconfig = self.PYTHON_PRE_CONFIG
else:
default_preconfig = self.DEFAULT_PRE_CONFIG
if expected_preconfig is None:
Expand Down Expand Up @@ -719,15 +728,46 @@ def test_init_dev_mode(self):
'dev_mode': 1,
'warnoptions': ['default'],
}
self.check_config("init_dev_mode", config, preconfig, api="python")
self.check_config("init_dev_mode", config, preconfig,
api=CONFIG_INIT_PYTHON)

def test_preinit_parse_argv(self):
# Pre-initialize implicitly using argv: make sure that -X dev
# is used to configure the allocation in preinitialization
preconfig = {
'allocator': PYMEM_ALLOCATOR_DEBUG,
}
config = {
'argv': ['script.py'],
'run_filename': 'script.py',
'dev_mode': 1,
'faulthandler': 1,
'warnoptions': ['default'],
'xoptions': ['dev'],
}
self.check_config("preinit_parse_argv", config, preconfig,
api=CONFIG_INIT_PYTHON)

def test_preinit_dont_parse_argv(self):
# -X dev must be ignored by isolated preconfiguration
preconfig = {
'isolated': 0,
}
config = {
'argv': ["python3", "-E", "-I",
"-X", "dev", "-X", "utf8", "script.py"],
'isolated': 0,
}
self.check_config("preinit_dont_parse_argv", config, preconfig,
api=CONFIG_INIT_ISOLATED)

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

def test_preinit_isolated1(self):
# _PyPreConfig.isolated=1, _PyCoreConfig.isolated not set
Expand All @@ -747,25 +787,30 @@ def test_preinit_isolated2(self):
}
self.check_config("preinit_isolated2", config)

def test_preinit_isolated_config(self):
self.check_config("preinit_isolated_config", api=CONFIG_INIT_ISOLATED)

def test_init_isolated_config(self):
self.check_config("init_isolated_config", api="isolated")
self.check_config("init_isolated_config", api=CONFIG_INIT_ISOLATED)

def test_init_python_config(self):
self.check_config("init_python_config", api="python")
self.check_config("init_python_config", api=CONFIG_INIT_PYTHON)

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

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

def test_init_run_main(self):
Expand All @@ -777,7 +822,8 @@ def test_init_run_main(self):
'run_command': code + '\n',
'parse_argv': 1,
}
self.check_config("init_run_main", core_config, api="python")
self.check_config("init_run_main", core_config,
api=CONFIG_INIT_PYTHON)

def test_init_main(self):
code = ('import _testinternalcapi, json; '
Expand All @@ -789,7 +835,8 @@ def test_init_main(self):
'parse_argv': 1,
'_init_main': 0,
}
self.check_config("init_main", core_config, api="python",
self.check_config("init_main", core_config,
api=CONFIG_INIT_PYTHON,
stderr="Run Python code before _Py_InitializeMain")

def test_init_parse_argv(self):
Expand All @@ -800,15 +847,20 @@ def test_init_parse_argv(self):
'run_command': 'pass\n',
'use_environment': 0,
}
self.check_config("init_parse_argv", core_config, api="python")
self.check_config("init_parse_argv", core_config,
api=CONFIG_INIT_PYTHON)

def test_init_dont_parse_argv(self):
pre_config = {
'parse_argv': 0,
}
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, api="python")
self.check_config("init_dont_parse_argv", core_config, pre_config,
api=CONFIG_INIT_PYTHON)


if __name__ == "__main__":
Expand Down
Loading