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: 11 additions & 11 deletions Include/cpython/coreconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,18 @@ typedef struct {

#define _Py_CONFIG_VERSION 1

typedef enum {
/* Py_Initialize() API: backward compatibility with Python 3.6 and 3.7 */
_PyConfig_INIT_COMPAT = 1,
_PyConfig_INIT_PYTHON = 2,
_PyConfig_INIT_ISOLATED = 3
} _PyConfigInitEnum;


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

/* Parse _Py_PreInitializeFromArgs() arguments?
See _PyCoreConfig.parse_argv */
Expand Down Expand Up @@ -107,10 +116,7 @@ typedef struct {
Set to 0 by "-X utf8=0" and PYTHONUTF8=0.

If equals to -1, it is set to 1 if the LC_CTYPE locale is "C" or
"POSIX", otherwise it is set to 0.

If equals to -2, inherit Py_UTF8Mode value value (which is equal to 0
by default). */
"POSIX", otherwise it is set to 0. Inherit Py_UTF8Mode value value. */
int utf8_mode;

int dev_mode; /* Development mode. PYTHONDEVMODE, -X dev */
Expand All @@ -126,16 +132,10 @@ 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 _config_init; /* _PyConfigInitEnum value */

int isolated; /* Isolated mode? see _PyPreConfig.isolated */
int use_environment; /* Use environment variables? see _PyPreConfig.use_environment */
Expand Down
4 changes: 2 additions & 2 deletions Include/internal/pycore_coreconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ PyAPI_FUNC(_PyInitError) _PyPreCmdline_Read(_PyPreCmdline *cmdline,

/* --- _PyPreConfig ----------------------------------------------- */

PyAPI_FUNC(void) _PyPreConfig_Init(_PyPreConfig *config);
PyAPI_FUNC(void) _PyPreConfig_InitCompatConfig(_PyPreConfig *config);
PyAPI_FUNC(void) _PyPreConfig_InitFromCoreConfig(
_PyPreConfig *config,
const _PyCoreConfig *coreconfig);
Expand All @@ -139,7 +139,7 @@ PyAPI_FUNC(_PyInitError) _PyPreConfig_Write(const _PyPreConfig *config);

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

PyAPI_FUNC(void) _PyCoreConfig_Init(_PyCoreConfig *config);
PyAPI_FUNC(void) _PyCoreConfig_InitCompatConfig(_PyCoreConfig *config);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_Copy(
_PyCoreConfig *config,
const _PyCoreConfig *config2);
Expand Down
113 changes: 64 additions & 49 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@


MS_WINDOWS = (os.name == 'nt')

PYMEM_ALLOCATOR_NOT_SET = 0
PYMEM_ALLOCATOR_DEBUG = 2
PYMEM_ALLOCATOR_MALLOC = 3

CONFIG_INIT = 0
CONFIG_INIT_PYTHON = 1
CONFIG_INIT_ISOLATED = 2
# _PyCoreConfig_InitCompatConfig()
API_COMPAT = 1
# _PyCoreConfig_InitPythonConfig()
API_PYTHON = 2
# _PyCoreConfig_InitIsolatedConfig()
API_ISOLATED = 3


class EmbeddingTestsMixin:
Expand Down Expand Up @@ -282,7 +286,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
# Marker to ignore a configuration parameter
IGNORE_CONFIG = object()

DEFAULT_PRE_CONFIG = {
PRE_CONFIG_COMPAT = {
'allocator': PYMEM_ALLOCATOR_NOT_SET,
'parse_argv': 0,
'configure_locale': 1,
Expand All @@ -291,15 +295,15 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
'utf8_mode': 0,
}
if MS_WINDOWS:
DEFAULT_PRE_CONFIG.update({
PRE_CONFIG_COMPAT.update({
'legacy_windows_fs_encoding': 0,
})
PYTHON_PRE_CONFIG = dict(DEFAULT_PRE_CONFIG,
PRE_CONFIG_PYTHON = dict(PRE_CONFIG_COMPAT,
parse_argv=1,
coerce_c_locale=GET_DEFAULT_CONFIG,
utf8_mode=GET_DEFAULT_CONFIG,
)
ISOLATED_PRE_CONFIG = dict(DEFAULT_PRE_CONFIG,
PRE_CONFIG_ISOLATED = dict(PRE_CONFIG_COMPAT,
configure_locale=0,
isolated=1,
use_environment=0,
Expand All @@ -314,8 +318,8 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
'use_environment',
]

DEFAULT_CORE_CONFIG = {
'_config_init': CONFIG_INIT,
CORE_CONFIG_COMPAT = {
'_config_init': API_COMPAT,
'isolated': 0,
'use_environment': 1,
'dev_mode': 0,
Expand Down Expand Up @@ -379,15 +383,15 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
'_init_main': 1,
}
if MS_WINDOWS:
DEFAULT_CORE_CONFIG.update({
CORE_CONFIG_COMPAT.update({
'legacy_windows_stdio': 0,
})

PYTHON_CORE_CONFIG = dict(DEFAULT_CORE_CONFIG,
CORE_CONFIG_PYTHON = dict(CORE_CONFIG_COMPAT,
configure_c_stdio=1,
parse_argv=1,
)
ISOLATED_CORE_CONFIG = dict(DEFAULT_CORE_CONFIG,
CORE_CONFIG_ISOLATED = dict(CORE_CONFIG_COMPAT,
isolated=1,
use_environment=0,
user_site_directory=0,
Expand All @@ -399,7 +403,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
pathconfig_warnings=0,
)
if MS_WINDOWS:
ISOLATED_CORE_CONFIG['legacy_windows_stdio'] = 0
CORE_CONFIG_ISOLATED['legacy_windows_stdio'] = 0

# global config
DEFAULT_GLOBAL_CONFIG = {
Expand Down Expand Up @@ -492,7 +496,7 @@ def get_expected_config(self, expected_preconfig, expected, env, api,
if value is self.GET_DEFAULT_CONFIG:
expected_preconfig[key] = pre_config[key]

if not expected_preconfig['configure_locale'] or api == CONFIG_INIT:
if not expected_preconfig['configure_locale'] or api == API_COMPAT:
# there is no easy way to get the locale encoding before
# setlocale(LC_CTYPE, "") is called: don't test encodings
for key in ('filesystem_encoding', 'filesystem_errors',
Expand Down Expand Up @@ -579,32 +583,33 @@ 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=CONFIG_INIT):
def check_config(self, testname, expected_config=None,
expected_preconfig=None, add_path=None, stderr=None,
*, api):
env = dict(os.environ)
# Remove PYTHON* environment variables to get deterministic environment
for key in list(env):
if key.startswith('PYTHON'):
del env[key]

if api == CONFIG_INIT_ISOLATED:
default_preconfig = self.ISOLATED_PRE_CONFIG
elif api == CONFIG_INIT_PYTHON:
default_preconfig = self.PYTHON_PRE_CONFIG
if api == API_ISOLATED:
default_preconfig = self.PRE_CONFIG_ISOLATED
elif api == API_PYTHON:
default_preconfig = self.PRE_CONFIG_PYTHON
else:
default_preconfig = self.DEFAULT_PRE_CONFIG
default_preconfig = self.PRE_CONFIG_COMPAT
if expected_preconfig is None:
expected_preconfig = {}
expected_preconfig = dict(default_preconfig, **expected_preconfig)
if expected_config is None:
expected_config = {}

if api == CONFIG_INIT_PYTHON:
default_config = self.PYTHON_CORE_CONFIG
elif api == CONFIG_INIT_ISOLATED:
default_config = self.ISOLATED_CORE_CONFIG
if api == API_PYTHON:
default_config = self.CORE_CONFIG_PYTHON
elif api == API_ISOLATED:
default_config = self.CORE_CONFIG_ISOLATED
else:
default_config = self.DEFAULT_CORE_CONFIG
default_config = self.CORE_CONFIG_COMPAT
expected_config = dict(default_config, **expected_config)
expected_config['_config_init'] = api

Expand All @@ -627,7 +632,13 @@ def check_config(self, testname, expected_config=None, expected_preconfig=None,
self.check_global_config(config)

def test_init_default_config(self):
self.check_config("init_default_config", {}, {})
self.check_config("init_initialize_config", api=API_COMPAT)

def test_preinit_compat_config(self):
self.check_config("preinit_compat_config", api=API_COMPAT)

def test_init_compat_config(self):
self.check_config("init_compat_config", api=API_COMPAT)

def test_init_global_config(self):
preconfig = {
Expand All @@ -649,7 +660,8 @@ def test_init_global_config(self):
'user_site_directory': 0,
'pathconfig_warnings': 0,
}
self.check_config("init_global_config", config, preconfig)
self.check_config("init_global_config", config, preconfig,
api=API_COMPAT)

def test_init_from_config(self):
preconfig = {
Expand Down Expand Up @@ -693,11 +705,13 @@ def test_init_from_config(self):
'check_hash_pycs_mode': 'always',
'pathconfig_warnings': 0,
}
self.check_config("init_from_config", config, preconfig)
self.check_config("init_from_config", config, preconfig,
api=API_COMPAT)

def test_init_env(self):
preconfig = {
'allocator': PYMEM_ALLOCATOR_MALLOC,
'utf8_mode': 1,
}
config = {
'use_hash_seed': 1,
Expand All @@ -718,21 +732,24 @@ def test_init_env(self):
'faulthandler': 1,
'warnoptions': ['EnvVar'],
}
self.check_config("init_env", config, preconfig)
self.check_config("init_env", config, preconfig,
api=API_COMPAT)

def test_init_env_dev_mode(self):
preconfig = dict(allocator=PYMEM_ALLOCATOR_DEBUG)
config = dict(dev_mode=1,
faulthandler=1,
warnoptions=['default'])
self.check_config("init_env_dev_mode", config, preconfig)
self.check_config("init_env_dev_mode", config, preconfig,
api=API_COMPAT)

def test_init_env_dev_mode_alloc(self):
preconfig = dict(allocator=PYMEM_ALLOCATOR_MALLOC)
config = dict(dev_mode=1,
faulthandler=1,
warnoptions=['default'])
self.check_config("init_env_dev_mode_alloc", config, preconfig)
self.check_config("init_env_dev_mode_alloc", config, preconfig,
api=API_COMPAT)

def test_init_dev_mode(self):
preconfig = {
Expand All @@ -744,7 +761,7 @@ def test_init_dev_mode(self):
'warnoptions': ['default'],
}
self.check_config("init_dev_mode", config, preconfig,
api=CONFIG_INIT_PYTHON)
api=API_PYTHON)

def test_preinit_parse_argv(self):
# Pre-initialize implicitly using argv: make sure that -X dev
Expand All @@ -761,7 +778,7 @@ def test_preinit_parse_argv(self):
'xoptions': ['dev'],
}
self.check_config("preinit_parse_argv", config, preconfig,
api=CONFIG_INIT_PYTHON)
api=API_PYTHON)

def test_preinit_dont_parse_argv(self):
# -X dev must be ignored by isolated preconfiguration
Expand All @@ -774,15 +791,15 @@ def test_preinit_dont_parse_argv(self):
'isolated': 0,
}
self.check_config("preinit_dont_parse_argv", config, preconfig,
api=CONFIG_INIT_ISOLATED)
api=API_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=CONFIG_INIT_PYTHON)
self.check_config("init_isolated_flag", config, api=API_PYTHON)

def test_preinit_isolated1(self):
# _PyPreConfig.isolated=1, _PyCoreConfig.isolated not set
Expand All @@ -791,7 +808,7 @@ def test_preinit_isolated1(self):
'use_environment': 0,
'user_site_directory': 0,
}
self.check_config("preinit_isolated1", config)
self.check_config("preinit_isolated1", config, api=API_COMPAT)

def test_preinit_isolated2(self):
# _PyPreConfig.isolated=0, _PyCoreConfig.isolated=1
Expand All @@ -800,16 +817,16 @@ def test_preinit_isolated2(self):
'use_environment': 0,
'user_site_directory': 0,
}
self.check_config("preinit_isolated2", config)
self.check_config("preinit_isolated2", config, api=API_COMPAT)

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

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

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

def test_init_dont_configure_locale(self):
# _PyPreConfig.configure_locale=0
Expand All @@ -818,15 +835,15 @@ def test_init_dont_configure_locale(self):
'coerce_c_locale': 0,
}
self.check_config("init_dont_configure_locale", {}, preconfig,
api=CONFIG_INIT_PYTHON)
api=API_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=CONFIG_INIT_PYTHON,
api=API_PYTHON,
add_path="init_read_set_path")

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

def test_init_main(self):
code = ('import _testinternalcapi, json; '
Expand All @@ -852,7 +868,7 @@ def test_init_main(self):
'_init_main': 0,
}
self.check_config("init_main", core_config,
api=CONFIG_INIT_PYTHON,
api=API_PYTHON,
stderr="Run Python code before _Py_InitializeMain")

def test_init_parse_argv(self):
Expand All @@ -863,8 +879,7 @@ def test_init_parse_argv(self):
'run_command': 'pass\n',
'use_environment': 0,
}
self.check_config("init_parse_argv", core_config,
api=CONFIG_INIT_PYTHON)
self.check_config("init_parse_argv", core_config, api=API_PYTHON)

def test_init_dont_parse_argv(self):
pre_config = {
Expand All @@ -876,7 +891,7 @@ def test_init_dont_parse_argv(self):
'program_name': './argv0',
}
self.check_config("init_dont_parse_argv", core_config, pre_config,
api=CONFIG_INIT_PYTHON)
api=API_PYTHON)


if __name__ == "__main__":
Expand Down
Loading