Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-35742: Fix test_envar_unimportable in test_builtin. #11561

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
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
1 change: 1 addition & 0 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1608,6 +1608,7 @@ def test_envar_good_path_empty_string(self):
def test_envar_unimportable(self):
for envar in (
'.', '..', '.foo', 'foo.', '.int', 'int.',
'.foo.bar', '..foo.bar', '/./',
'nosuchbuiltin',
'nosuchmodule.nosuchcallable',
):
Expand Down
25 changes: 15 additions & 10 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,14 @@ sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyOb
modulepath = PyUnicode_FromString("builtins");
attrname = envar;
}
else {
else if (last_dot != envar) {
/* Split on the last dot; */
modulepath = PyUnicode_FromStringAndSize(envar, last_dot - envar);
attrname = last_dot + 1;
}
else {
goto warn;
}
if (modulepath == NULL) {
PyMem_RawFree(envar);
return NULL;
Expand All @@ -155,27 +158,29 @@ sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyOb
Py_DECREF(modulepath);

if (module == NULL) {
goto error;
if (PyErr_ExceptionMatches(PyExc_ImportError)) {
goto warn;
}
PyMem_RawFree(envar);
return NULL;
}

PyObject *hook = PyObject_GetAttrString(module, attrname);
Py_DECREF(module);

if (hook == NULL) {
goto error;
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
goto warn;
}
PyMem_RawFree(envar);
return NULL;
}
PyMem_RawFree(envar);
PyObject *retval = _PyObject_FastCallKeywords(hook, args, nargs, keywords);
Py_DECREF(hook);
return retval;

error:
if (!PyErr_ExceptionMatches(PyExc_ImportError)
&& !PyErr_ExceptionMatches(PyExc_AttributeError))
{
PyMem_RawFree(envar);
return NULL;
}
warn:
/* If any of the imports went wrong, then warn and ignore. */
PyErr_Clear();
int status = PyErr_WarnFormat(
Expand Down