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

Skip to content

Commit 97d6a56

Browse files
bpo-35742: Fix test_envar_unimportable in test_builtin. (GH-11561)
Handle the case of an empty module name in PYTHONBREAKPOINT. Fixes a regression introduced in bpo-34756. (cherry picked from commit 3607ef4) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 0bb6b89 commit 97d6a56

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

Lib/test/test_builtin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,7 @@ def test_envar_good_path_empty_string(self):
16011601
def test_envar_unimportable(self):
16021602
for envar in (
16031603
'.', '..', '.foo', 'foo.', '.int', 'int.',
1604+
'.foo.bar', '..foo.bar', '/./',
16041605
'nosuchbuiltin',
16051606
'nosuchmodule.nosuchcallable',
16061607
):

Python/sysmodule.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,14 @@ sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyOb
134134
modulepath = PyUnicode_FromString("builtins");
135135
attrname = envar;
136136
}
137-
else {
137+
else if (last_dot != envar) {
138138
/* Split on the last dot; */
139139
modulepath = PyUnicode_FromStringAndSize(envar, last_dot - envar);
140140
attrname = last_dot + 1;
141141
}
142+
else {
143+
goto warn;
144+
}
142145
if (modulepath == NULL) {
143146
PyMem_RawFree(envar);
144147
return NULL;
@@ -156,27 +159,29 @@ sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyOb
156159
Py_DECREF(fromlist);
157160

158161
if (module == NULL) {
159-
goto error;
162+
if (PyErr_ExceptionMatches(PyExc_ImportError)) {
163+
goto warn;
164+
}
165+
PyMem_RawFree(envar);
166+
return NULL;
160167
}
161168

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

165172
if (hook == NULL) {
166-
goto error;
173+
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
174+
goto warn;
175+
}
176+
PyMem_RawFree(envar);
177+
return NULL;
167178
}
168179
PyMem_RawFree(envar);
169180
PyObject *retval = _PyObject_FastCallKeywords(hook, args, nargs, keywords);
170181
Py_DECREF(hook);
171182
return retval;
172183

173-
error:
174-
if (!PyErr_ExceptionMatches(PyExc_ImportError)
175-
&& !PyErr_ExceptionMatches(PyExc_AttributeError))
176-
{
177-
PyMem_RawFree(envar);
178-
return NULL;
179-
}
184+
warn:
180185
/* If any of the imports went wrong, then warn and ignore. */
181186
PyErr_Clear();
182187
int status = PyErr_WarnFormat(

0 commit comments

Comments
 (0)