Unverified Kaydet (Commit) 01de89cb authored tarafından Victor Stinner's avatar Victor Stinner Kaydeden (comit) GitHub

bpo-35233: InitConfigTests tests more config vars (GH-10541)

test_embed.InitConfigTests tests more configuration variables.

Changes:

* InitConfigTests tests more core configuration variables:

  * base_exec_prefix
  * base_prefix
  * exec_prefix
  * home
  * legacy_windows_fs_encoding
  * legacy_windows_stdio
  * module_search_path_env
  * prefix

* "_testembed init_from_config" tests more variables:

  * argv
  * warnoptions
  * xoptions

* InitConfigTests: add check_global_config(), check_core_config() and
  check_main_config() subfunctions to cleanup the code. Move also
  constants at the class level (ex: COPY_MAIN_CONFIG).
* Fix _PyCoreConfig_AsDict(): don't set stdio_encoding twice
* Use more macros in _PyCoreConfig_AsDict() and
  _PyMainInterpreterConfig_AsDict() to reduce code duplication.
* Other minor cleanups.
üst 64313478
...@@ -359,9 +359,9 @@ PyAPI_FUNC(int) _PyCoreConfig_GetEnvDup( ...@@ -359,9 +359,9 @@ PyAPI_FUNC(int) _PyCoreConfig_GetEnvDup(
wchar_t *wname, wchar_t *wname,
char *name); char *name);
/* Used by _testcapi.get_coreconfig() */ /* Used by _testcapi.get_global_config() and _testcapi.get_core_config() */
PyAPI_FUNC(PyObject *) _PyCoreConfig_AsDict(const _PyCoreConfig *config);
PyAPI_FUNC(PyObject *) _Py_GetGlobalVariablesAsDict(void); PyAPI_FUNC(PyObject *) _Py_GetGlobalVariablesAsDict(void);
PyAPI_FUNC(PyObject *) _PyCoreConfig_AsDict(const _PyCoreConfig *config);
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus
......
...@@ -37,6 +37,7 @@ PyAPI_FUNC(void) _PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *); ...@@ -37,6 +37,7 @@ PyAPI_FUNC(void) _PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *);
PyAPI_FUNC(int) _PyMainInterpreterConfig_Copy( PyAPI_FUNC(int) _PyMainInterpreterConfig_Copy(
_PyMainInterpreterConfig *config, _PyMainInterpreterConfig *config,
const _PyMainInterpreterConfig *config2); const _PyMainInterpreterConfig *config2);
/* Used by _testcapi.get_main_config() */
PyAPI_FUNC(PyObject*) _PyMainInterpreterConfig_AsDict( PyAPI_FUNC(PyObject*) _PyMainInterpreterConfig_AsDict(
const _PyMainInterpreterConfig *config); const _PyMainInterpreterConfig *config);
......
This diff is collapsed.
...@@ -1442,11 +1442,11 @@ _PyMainInterpreterConfig_Copy(_PyMainInterpreterConfig *config, ...@@ -1442,11 +1442,11 @@ _PyMainInterpreterConfig_Copy(_PyMainInterpreterConfig *config,
_PyMainInterpreterConfig_Clear(config); _PyMainInterpreterConfig_Clear(config);
#define COPY_ATTR(ATTR) config->ATTR = config2->ATTR #define COPY_ATTR(ATTR) config->ATTR = config2->ATTR
#define COPY_OBJ_ATTR(OBJ_ATTR) \ #define COPY_OBJ_ATTR(ATTR) \
do { \ do { \
if (config2->OBJ_ATTR != NULL) { \ if (config2->ATTR != NULL) { \
config->OBJ_ATTR = config_copy_attr(config2->OBJ_ATTR); \ config->ATTR = config_copy_attr(config2->ATTR); \
if (config->OBJ_ATTR == NULL) { \ if (config->ATTR == NULL) { \
return -1; \ return -1; \
} \ } \
} \ } \
...@@ -1480,38 +1480,42 @@ _PyMainInterpreterConfig_AsDict(const _PyMainInterpreterConfig *config) ...@@ -1480,38 +1480,42 @@ _PyMainInterpreterConfig_AsDict(const _PyMainInterpreterConfig *config)
return NULL; return NULL;
} }
#define SET_ITEM(KEY, ATTR) \ #define SET_ITEM_INT(ATTR) \
do { \ do { \
obj = config->ATTR; \ obj = PyLong_FromLong(config->ATTR); \
if (obj == NULL) { \ if (obj == NULL) { \
obj = Py_None; \ goto fail; \
} \ } \
res = PyDict_SetItemString(dict, (KEY), obj); \ res = PyDict_SetItemString(dict, #ATTR, obj); \
if (res < 0) { \ Py_DECREF(obj); \
goto fail; \ if (res < 0) { \
} \ goto fail; \
} while (0) } \
} while (0)
obj = PyLong_FromLong(config->install_signal_handlers); #define SET_ITEM_OBJ(ATTR) \
if (obj == NULL) { do { \
goto fail; obj = config->ATTR; \
} if (obj == NULL) { \
res = PyDict_SetItemString(dict, "install_signal_handlers", obj); obj = Py_None; \
Py_DECREF(obj); } \
if (res < 0) { res = PyDict_SetItemString(dict, #ATTR, obj); \
goto fail; if (res < 0) { \
} goto fail; \
} \
} while (0)
SET_ITEM("argv", argv); SET_ITEM_INT(install_signal_handlers);
SET_ITEM("executable", executable); SET_ITEM_OBJ(argv);
SET_ITEM("prefix", prefix); SET_ITEM_OBJ(executable);
SET_ITEM("base_prefix", base_prefix); SET_ITEM_OBJ(prefix);
SET_ITEM("exec_prefix", exec_prefix); SET_ITEM_OBJ(base_prefix);
SET_ITEM("base_exec_prefix", base_exec_prefix); SET_ITEM_OBJ(exec_prefix);
SET_ITEM("warnoptions", warnoptions); SET_ITEM_OBJ(base_exec_prefix);
SET_ITEM("xoptions", xoptions); SET_ITEM_OBJ(warnoptions);
SET_ITEM("module_search_path", module_search_path); SET_ITEM_OBJ(xoptions);
SET_ITEM("pycache_prefix", pycache_prefix); SET_ITEM_OBJ(module_search_path);
SET_ITEM_OBJ(pycache_prefix);
return dict; return dict;
...@@ -1519,7 +1523,7 @@ fail: ...@@ -1519,7 +1523,7 @@ fail:
Py_DECREF(dict); Py_DECREF(dict);
return NULL; return NULL;
#undef SET_ITEM #undef SET_ITEM_OBJ
} }
......
...@@ -355,6 +355,7 @@ error: ...@@ -355,6 +355,7 @@ error:
return -1; return -1;
} }
static void static void
dump_config(void) dump_config(void)
{ {
...@@ -468,10 +469,30 @@ static int test_init_from_config(void) ...@@ -468,10 +469,30 @@ static int test_init_from_config(void)
Py_SetProgramName(L"./globalvar"); Py_SetProgramName(L"./globalvar");
config.program_name = L"./conf_program_name"; config.program_name = L"./conf_program_name";
/* FIXME: test argc/argv */ static wchar_t* argv[2] = {
L"-c",
L"pass",
};
config.argc = Py_ARRAY_LENGTH(argv);
config.argv = argv;
config.program = L"conf_program"; config.program = L"conf_program";
/* FIXME: test xoptions */
/* FIXME: test warnoptions */ static wchar_t* xoptions[3] = {
L"core_xoption1=3",
L"core_xoption2=",
L"core_xoption3",
};
config.nxoption = Py_ARRAY_LENGTH(xoptions);
config.xoptions = xoptions;
static wchar_t* warnoptions[2] = {
L"default",
L"error::ResourceWarning",
};
config.nwarnoption = Py_ARRAY_LENGTH(warnoptions);
config.warnoptions = warnoptions;
/* FIXME: test module_search_path_env */ /* FIXME: test module_search_path_env */
/* FIXME: test home */ /* FIXME: test home */
/* FIXME: test path config: module_search_path .. dll_path */ /* FIXME: test path config: module_search_path .. dll_path */
...@@ -512,6 +533,11 @@ static int test_init_from_config(void) ...@@ -512,6 +533,11 @@ static int test_init_from_config(void)
putenv("PYTHONIOENCODING=cp424"); putenv("PYTHONIOENCODING=cp424");
Py_SetStandardStreamEncoding("ascii", "ignore"); Py_SetStandardStreamEncoding("ascii", "ignore");
#ifdef MS_WINDOWS
/* Py_SetStandardStreamEncoding() sets Py_LegacyWindowsStdioFlag to 1.
Force it to 0 through the config. */
config.legacy_windows_stdio = 0;
#endif
config.stdio_encoding = "iso8859-1"; config.stdio_encoding = "iso8859-1";
config.stdio_errors = "replace"; config.stdio_errors = "replace";
......
...@@ -1444,14 +1444,6 @@ _PyCoreConfig_AsDict(const _PyCoreConfig *config) ...@@ -1444,14 +1444,6 @@ _PyCoreConfig_AsDict(const _PyCoreConfig *config)
return NULL; return NULL;
} }
#define FROM_STRING(STR) \
((STR != NULL) ? \
PyUnicode_FromString(STR) \
: (Py_INCREF(Py_None), Py_None))
#define FROM_WSTRING(STR) \
((STR != NULL) ? \
PyUnicode_FromWideChar(STR, -1) \
: (Py_INCREF(Py_None), Py_None))
#define SET_ITEM(KEY, EXPR) \ #define SET_ITEM(KEY, EXPR) \
do { \ do { \
obj = (EXPR); \ obj = (EXPR); \
...@@ -1464,117 +1456,81 @@ _PyCoreConfig_AsDict(const _PyCoreConfig *config) ...@@ -1464,117 +1456,81 @@ _PyCoreConfig_AsDict(const _PyCoreConfig *config)
goto fail; \ goto fail; \
} \ } \
} while (0) } while (0)
#define FROM_STRING(STR) \
SET_ITEM("install_signal_handlers", ((STR != NULL) ? \
PyLong_FromLong(config->install_signal_handlers)); PyUnicode_FromString(STR) \
SET_ITEM("use_environment", : (Py_INCREF(Py_None), Py_None))
PyLong_FromLong(config->use_environment)); #define SET_ITEM_INT(ATTR) \
SET_ITEM("use_hash_seed", SET_ITEM(#ATTR, PyLong_FromLong(config->ATTR))
PyLong_FromLong(config->use_hash_seed)); #define SET_ITEM_UINT(ATTR) \
SET_ITEM("hash_seed", SET_ITEM(#ATTR, PyLong_FromUnsignedLong(config->ATTR))
PyLong_FromUnsignedLong(config->hash_seed)); #define SET_ITEM_STR(ATTR) \
SET_ITEM("allocator", SET_ITEM(#ATTR, FROM_STRING(config->ATTR))
FROM_STRING(config->allocator)); #define FROM_WSTRING(STR) \
SET_ITEM("dev_mode", ((STR != NULL) ? \
PyLong_FromLong(config->dev_mode)); PyUnicode_FromWideChar(STR, -1) \
SET_ITEM("faulthandler", : (Py_INCREF(Py_None), Py_None))
PyLong_FromLong(config->faulthandler)); #define SET_ITEM_WSTR(ATTR) \
SET_ITEM("tracemalloc", SET_ITEM(#ATTR, FROM_WSTRING(config->ATTR))
PyLong_FromLong(config->tracemalloc)); #define SET_ITEM_WSTRLIST(NOPTION, OPTIONS) \
SET_ITEM("import_time", SET_ITEM(#OPTIONS, _Py_wstrlist_as_pylist(config->NOPTION, config->OPTIONS))
PyLong_FromLong(config->import_time));
SET_ITEM("show_ref_count", SET_ITEM_INT(install_signal_handlers);
PyLong_FromLong(config->show_ref_count)); SET_ITEM_INT(use_environment);
SET_ITEM("show_alloc_count", SET_ITEM_INT(use_hash_seed);
PyLong_FromLong(config->show_alloc_count)); SET_ITEM_UINT(hash_seed);
SET_ITEM("dump_refs", SET_ITEM_STR(allocator);
PyLong_FromLong(config->dump_refs)); SET_ITEM_INT(dev_mode);
SET_ITEM("malloc_stats", SET_ITEM_INT(faulthandler);
PyLong_FromLong(config->malloc_stats)); SET_ITEM_INT(tracemalloc);
SET_ITEM("coerce_c_locale", SET_ITEM_INT(import_time);
PyLong_FromLong(config->coerce_c_locale)); SET_ITEM_INT(show_ref_count);
SET_ITEM("coerce_c_locale_warn", SET_ITEM_INT(show_alloc_count);
PyLong_FromLong(config->coerce_c_locale_warn)); SET_ITEM_INT(dump_refs);
SET_ITEM("filesystem_encoding", SET_ITEM_INT(malloc_stats);
FROM_STRING(config->filesystem_encoding)); SET_ITEM_INT(coerce_c_locale);
SET_ITEM("filesystem_errors", SET_ITEM_INT(coerce_c_locale_warn);
FROM_STRING(config->filesystem_errors)); SET_ITEM_STR(filesystem_encoding);
SET_ITEM("stdio_encoding", SET_ITEM_STR(filesystem_errors);
FROM_STRING(config->stdio_encoding)); SET_ITEM_INT(utf8_mode);
SET_ITEM("utf8_mode", SET_ITEM_WSTR(pycache_prefix);
PyLong_FromLong(config->utf8_mode)); SET_ITEM_WSTR(program_name);
SET_ITEM("pycache_prefix", SET_ITEM_WSTRLIST(argc, argv);
FROM_WSTRING(config->pycache_prefix)); SET_ITEM_WSTR(program);
SET_ITEM("program_name", SET_ITEM_WSTRLIST(nxoption, xoptions);
FROM_WSTRING(config->program_name)); SET_ITEM_WSTRLIST(nwarnoption, warnoptions);
SET_ITEM("argv", SET_ITEM_WSTR(module_search_path_env);
_Py_wstrlist_as_pylist(config->argc, config->argv)); SET_ITEM_WSTR(home);
SET_ITEM("program", SET_ITEM_WSTRLIST(nmodule_search_path, module_search_paths);
FROM_WSTRING(config->program)); SET_ITEM_WSTR(executable);
SET_ITEM("xoptions", SET_ITEM_WSTR(prefix);
_Py_wstrlist_as_pylist(config->nxoption, config->xoptions)); SET_ITEM_WSTR(base_prefix);
SET_ITEM("warnoptions", SET_ITEM_WSTR(exec_prefix);
_Py_wstrlist_as_pylist(config->nwarnoption, config->warnoptions)); SET_ITEM_WSTR(base_exec_prefix);
SET_ITEM("module_search_path_env",
FROM_WSTRING(config->module_search_path_env));
SET_ITEM("home",
FROM_WSTRING(config->home));
SET_ITEM("module_search_paths",
_Py_wstrlist_as_pylist(config->nmodule_search_path, config->module_search_paths));
SET_ITEM("executable",
FROM_WSTRING(config->executable));
SET_ITEM("prefix",
FROM_WSTRING(config->prefix));
SET_ITEM("base_prefix",
FROM_WSTRING(config->base_prefix));
SET_ITEM("exec_prefix",
FROM_WSTRING(config->exec_prefix));
SET_ITEM("base_exec_prefix",
FROM_WSTRING(config->base_exec_prefix));
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
SET_ITEM("dll_path", SET_ITEM_WSTR(dll_path);
FROM_WSTRING(config->dll_path));
#endif #endif
SET_ITEM("isolated", SET_ITEM_INT(isolated);
PyLong_FromLong(config->isolated)); SET_ITEM_INT(site_import);
SET_ITEM("site_import", SET_ITEM_INT(bytes_warning);
PyLong_FromLong(config->site_import)); SET_ITEM_INT(inspect);
SET_ITEM("bytes_warning", SET_ITEM_INT(interactive);
PyLong_FromLong(config->bytes_warning)); SET_ITEM_INT(optimization_level);
SET_ITEM("inspect", SET_ITEM_INT(parser_debug);
PyLong_FromLong(config->inspect)); SET_ITEM_INT(write_bytecode);
SET_ITEM("interactive", SET_ITEM_INT(verbose);
PyLong_FromLong(config->interactive)); SET_ITEM_INT(quiet);
SET_ITEM("optimization_level", SET_ITEM_INT(user_site_directory);
PyLong_FromLong(config->optimization_level)); SET_ITEM_INT(buffered_stdio);
SET_ITEM("parser_debug", SET_ITEM_STR(stdio_encoding);
PyLong_FromLong(config->parser_debug)); SET_ITEM_STR(stdio_errors);
SET_ITEM("write_bytecode",
PyLong_FromLong(config->write_bytecode));
SET_ITEM("verbose",
PyLong_FromLong(config->verbose));
SET_ITEM("quiet",
PyLong_FromLong(config->quiet));
SET_ITEM("user_site_directory",
PyLong_FromLong(config->user_site_directory));
SET_ITEM("buffered_stdio",
PyLong_FromLong(config->buffered_stdio));
SET_ITEM("stdio_encoding",
FROM_STRING(config->stdio_encoding));
SET_ITEM("stdio_errors",
FROM_STRING(config->stdio_errors));
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
SET_ITEM("legacy_windows_fs_encoding", SET_ITEM_INT(legacy_windows_fs_encoding);
PyLong_FromLong(config->legacy_windows_fs_encoding)); SET_ITEM_INT(legacy_windows_stdio);
SET_ITEM("legacy_windows_stdio",
PyLong_FromLong(config->legacy_windows_stdio));
#endif #endif
SET_ITEM("_install_importlib", SET_ITEM_INT(_install_importlib);
PyLong_FromLong(config->_install_importlib)); SET_ITEM_STR(_check_hash_pycs_mode);
SET_ITEM("_check_hash_pycs_mode", SET_ITEM_INT(_frozen);
FROM_STRING(config->_check_hash_pycs_mode));
SET_ITEM("_frozen",
PyLong_FromLong(config->_frozen));
return dict; return dict;
...@@ -1585,4 +1541,9 @@ fail: ...@@ -1585,4 +1541,9 @@ fail:
#undef FROM_STRING #undef FROM_STRING
#undef FROM_WSTRING #undef FROM_WSTRING
#undef SET_ITEM #undef SET_ITEM
#undef SET_ITEM_INT
#undef SET_ITEM_UINT
#undef SET_ITEM_STR
#undef SET_ITEM_WSTR
#undef SET_ITEM_WSTRLIST
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment