Skip to content

bpo-35239: _PySys_EndInit() copies module_search_path #10532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 16, 2018
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
11 changes: 3 additions & 8 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,6 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
})

# main config
UNTESTED_MAIN_CONFIG = (
# FIXME: untested main configuration variables
'module_search_path',
)
COPY_MAIN_CONFIG = (
# Copy core config to main config for expected values
'argv',
Expand All @@ -346,7 +342,8 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
'prefix',
'pycache_prefix',
'warnoptions',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You switch warnoptions over to using the new COPY_LIST macro below, as well as updating it to be read from the sys module when needed. Did you mean to also remove it from this list?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And that's what I get for reviewing the code before reading the author's initial comments on the review - you already answered that question (the warnoptions here are the warning options as specified in the initial configuration, while the "live" state is always the copy in the sys module).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warnoptions comes from DEFAULT_CORE_CONFIG: warnoptions=[]. Can it can be replaced in other tests like: warnoptions=['default', 'error::ResourceWarning']. COPY_MAIN_CONFIG ensures that core and main configuration contain exactly the same value, it's just to not repeat the expected value twice.

Spoiler(!): I'm now experimenting a change to avoid redundancy between core and main configuration! Stay tuned.

# xoptions is created from core_config in check_main_config()
# xoptions is created from core_config in check_main_config().
# 'module_search_paths' is copied to 'module_search_path'.
)

# global config
Expand Down Expand Up @@ -426,12 +423,10 @@ def check_main_config(self, config):
main_config = config['main_config']

# main config
for key in self.UNTESTED_MAIN_CONFIG:
del main_config[key]

expected_main = {}
for key in self.COPY_MAIN_CONFIG:
expected_main[key] = core_config[key]
expected_main['module_search_path'] = core_config['module_search_paths']
expected_main['xoptions'] = self.main_xoptions(core_config['xoptions'])
self.assertEqual(main_config, expected_main)

Expand Down
4 changes: 2 additions & 2 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -836,8 +836,8 @@ _Py_InitializeMainInterpreter(PyInterpreterState *interp,
}

/* Initialize warnings. */
if (interp->config.warnoptions != NULL &&
PyList_Size(interp->config.warnoptions) > 0)
PyObject *warnoptions = PySys_GetObject("warnoptions");
if (warnoptions != NULL && PyList_Size(warnoptions) > 0)
{
PyObject *warnings_module = PyImport_ImportModule("warnings");
if (warnings_module == NULL) {
Expand Down
26 changes: 23 additions & 3 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2488,7 +2488,20 @@ _PySys_EndInit(PyObject *sysdict, PyInterpreterState *interp)
assert(config->exec_prefix != NULL);
assert(config->base_exec_prefix != NULL);

SET_SYS_FROM_STRING_BORROW("path", config->module_search_path);
#define COPY_LIST(KEY, ATTR) \
do { \
assert(PyList_Check(config->ATTR)); \
PyObject *list = PyList_GetSlice(config->ATTR, \
0, PyList_GET_SIZE(config->ATTR)); \
if (list == NULL) { \
return -1; \
} \
SET_SYS_FROM_STRING_BORROW(KEY, list); \
Py_DECREF(list); \
} while (0)

COPY_LIST("path", module_search_path);

SET_SYS_FROM_STRING_BORROW("executable", config->executable);
SET_SYS_FROM_STRING_BORROW("prefix", config->prefix);
SET_SYS_FROM_STRING_BORROW("base_prefix", config->base_prefix);
Expand All @@ -2505,12 +2518,19 @@ _PySys_EndInit(PyObject *sysdict, PyInterpreterState *interp)
SET_SYS_FROM_STRING_BORROW("argv", config->argv);
}
if (config->warnoptions != NULL) {
SET_SYS_FROM_STRING_BORROW("warnoptions", config->warnoptions);
COPY_LIST("warnoptions", warnoptions);
}
if (config->xoptions != NULL) {
SET_SYS_FROM_STRING_BORROW("_xoptions", config->xoptions);
PyObject *dict = PyDict_Copy(config->xoptions);
if (dict == NULL) {
return -1;
}
SET_SYS_FROM_STRING_BORROW("_xoptions", dict);
Py_DECREF(dict);
}

#undef COPY_LIST

/* Set flags to their final values */
SET_SYS_FROM_STRING_INT_RESULT("flags", make_flags());
/* prevent user from creating new instances */
Expand Down