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
2 changes: 2 additions & 0 deletions Include/pylifecycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ PyAPI_FUNC(void) _PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *);
PyAPI_FUNC(int) _PyMainInterpreterConfig_Copy(
_PyMainInterpreterConfig *config,
const _PyMainInterpreterConfig *config2);
PyAPI_FUNC(PyObject*) _PyMainInterpreterConfig_AsDict(
const _PyMainInterpreterConfig *config);

PyAPI_FUNC(_PyInitError) _Py_InitializeMainInterpreter(
PyInterpreterState *interp,
Expand Down
22 changes: 16 additions & 6 deletions Lib/test/pythoninfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,15 +534,25 @@ def collect_gdbm(info_add):
info_add('gdbm.GDBM_VERSION', '.'.join(map(str, _GDBM_VERSION)))


def collect_get_coreconfig(info_add):
def collect_get_config(info_add):
# Dump _PyCoreConfig and _PyMainInterpreterConfig
try:
from _testcapi import get_coreconfig
except ImportError:
return
pass
else:
config = get_coreconfig()
for key in sorted(config):
info_add('core_config[%s]' % key, repr(config[key]))

config = get_coreconfig()
for key in sorted(config):
info_add('coreconfig[%s]' % key, repr(config[key]))
try:
from _testcapi import get_mainconfig
except ImportError:
pass
else:
config = get_mainconfig()
for key in sorted(config):
info_add('main_config[%s]' % key, repr(config[key]))


def collect_info(info):
Expand Down Expand Up @@ -573,7 +583,7 @@ def collect_info(info):
collect_resource,
collect_cc,
collect_gdbm,
collect_get_coreconfig,
collect_get_config,

# Collecting from tests should be last as they have side effects.
collect_test_socket,
Expand Down
42 changes: 36 additions & 6 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,11 @@ def test_initialize_pymain(self):

class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
maxDiff = 4096
CORE_CONFIG_REGEX = re.compile(r"^core_config\[([^]]*)\] = (.*)$")
MAIN_CONFIG_REGEX = re.compile(r"^main_config\[([^]]*)\] = (.*)$")
UTF8_MODE_ERRORS = ('surrogatepass' if sys.platform == 'win32'
else 'surrogateescape')
DEFAULT_CONFIG = {
DEFAULT_CORE_CONFIG = {
'install_signal_handlers': 1,
'use_environment': 1,
'use_hash_seed': 0,
Expand Down Expand Up @@ -338,7 +340,7 @@ def get_filesystem_encoding(self, isolated, env):
return out.split()

def check_config(self, testname, expected):
expected = dict(self.DEFAULT_CONFIG, **expected)
expected = dict(self.DEFAULT_CORE_CONFIG, **expected)

env = dict(os.environ)
for key in list(env):
Expand Down Expand Up @@ -367,11 +369,39 @@ def check_config(self, testname, expected):
out, err = self.run_embedded_interpreter(testname, env=env)
# Ignore err

config = {}
core_config = {}
main_config = {}
for line in out.splitlines():
key, value = line.split(' = ', 1)
config[key] = value
self.assertEqual(config, expected)
match = self.CORE_CONFIG_REGEX.match(line)
if match is not None:
key = match.group(1)
value = match.group(2)
core_config[key] = value
else:
match = self.MAIN_CONFIG_REGEX.match(line)
if match is None:
raise ValueError(f"failed to parse line {line!r}")
key = match.group(1)
value = match.group(2)
main_config[key] = value
self.assertEqual(core_config, expected)

pycache_prefix = core_config['pycache_prefix']
if pycache_prefix != NULL_STR:
pycache_prefix = repr(pycache_prefix)
else:
pycache_prefix = "NULL"
expected_main = {
'install_signal_handlers': core_config['install_signal_handlers'],
'argv': '[]',
'prefix': repr(sys.prefix),
'base_prefix': repr(sys.base_prefix),
'base_exec_prefix': repr(sys.base_exec_prefix),
'warnoptions': '[]',
'xoptions': '{}',
'pycache_prefix': pycache_prefix,
}
self.assertEqual(main_config, expected_main)

def test_init_default_config(self):
self.check_config("init_default_config", {})
Expand Down
10 changes: 10 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4702,6 +4702,15 @@ get_coreconfig(PyObject *self, PyObject *Py_UNUSED(args))
}


static PyObject *
get_mainconfig(PyObject *self, PyObject *Py_UNUSED(args))
{
PyInterpreterState *interp = _PyInterpreterState_Get();
const _PyMainInterpreterConfig *config = &interp->config;
return _PyMainInterpreterConfig_AsDict(config);
}


#ifdef Py_REF_DEBUG
static PyObject *
negative_refcount(PyObject *self, PyObject *Py_UNUSED(args))
Expand Down Expand Up @@ -4948,6 +4957,7 @@ static PyMethodDef TestMethods[] = {
{"EncodeLocaleEx", encode_locale_ex, METH_VARARGS},
{"DecodeLocaleEx", decode_locale_ex, METH_VARARGS},
{"get_coreconfig", get_coreconfig, METH_NOARGS},
{"get_mainconfig", get_mainconfig, METH_NOARGS},
#ifdef Py_REF_DEBUG
{"negative_refcount", negative_refcount, METH_NOARGS},
#endif
Expand Down
85 changes: 71 additions & 14 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1441,31 +1441,88 @@ _PyMainInterpreterConfig_Copy(_PyMainInterpreterConfig *config,
{
_PyMainInterpreterConfig_Clear(config);

#define COPY_ATTR(ATTR) \
#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
#define COPY_OBJ_ATTR(OBJ_ATTR) \
do { \
if (config2->ATTR != NULL) { \
config->ATTR = config_copy_attr(config2->ATTR); \
if (config->ATTR == NULL) { \
if (config2->OBJ_ATTR != NULL) { \
config->OBJ_ATTR = config_copy_attr(config2->OBJ_ATTR); \
if (config->OBJ_ATTR == NULL) { \
return -1; \
} \
} \
} while (0)

COPY_ATTR(argv);
COPY_ATTR(executable);
COPY_ATTR(prefix);
COPY_ATTR(base_prefix);
COPY_ATTR(exec_prefix);
COPY_ATTR(base_exec_prefix);
COPY_ATTR(warnoptions);
COPY_ATTR(xoptions);
COPY_ATTR(module_search_path);
COPY_ATTR(pycache_prefix);
COPY_ATTR(install_signal_handlers);
COPY_OBJ_ATTR(argv);
COPY_OBJ_ATTR(executable);
COPY_OBJ_ATTR(prefix);
COPY_OBJ_ATTR(base_prefix);
COPY_OBJ_ATTR(exec_prefix);
COPY_OBJ_ATTR(base_exec_prefix);
COPY_OBJ_ATTR(warnoptions);
COPY_OBJ_ATTR(xoptions);
COPY_OBJ_ATTR(module_search_path);
COPY_OBJ_ATTR(pycache_prefix);
#undef COPY_ATTR
#undef COPY_OBJ_ATTR
return 0;
}


PyObject*
_PyMainInterpreterConfig_AsDict(const _PyMainInterpreterConfig *config)
{
PyObject *dict, *obj;
int res;

dict = PyDict_New();
if (dict == NULL) {
return NULL;
}

#define SET_ITEM(KEY, ATTR) \
do { \
obj = config->ATTR; \
if (obj == NULL) { \
obj = Py_None; \
} \
res = PyDict_SetItemString(dict, (KEY), obj); \
if (res < 0) { \
goto fail; \
} \
} while (0)

obj = PyLong_FromLong(config->install_signal_handlers);
if (obj == NULL) {
goto fail;
}
res = PyDict_SetItemString(dict, "install_signal_handlers", obj);
Py_DECREF(obj);
if (res < 0) {
goto fail;
}

SET_ITEM("argv", argv);
SET_ITEM("executable", executable);
SET_ITEM("prefix", prefix);
SET_ITEM("base_prefix", base_prefix);
SET_ITEM("exec_prefix", exec_prefix);
SET_ITEM("base_exec_prefix", base_exec_prefix);
SET_ITEM("warnoptions", warnoptions);
SET_ITEM("xoptions", xoptions);
SET_ITEM("module_search_path", module_search_path);
SET_ITEM("pycache_prefix", pycache_prefix);

return dict;

fail:
Py_DECREF(dict);
return NULL;

#undef SET_ITEM
}


_PyInitError
_PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *main_config,
const _PyCoreConfig *config)
Expand Down
Loading