mirror of
https://github.com/python/cpython.git
synced 2025-10-17 12:18:23 +00:00
bpo-31492: Fix assertion failures in case of a module with a bad __name__ attribute. (#3620)
This commit is contained in:
parent
453408a505
commit
6db7033192
4 changed files with 20 additions and 4 deletions
|
@ -400,6 +400,18 @@ class ImportTests(unittest.TestCase):
|
||||||
self.assertEqual(str(cm.exception),
|
self.assertEqual(str(cm.exception),
|
||||||
"cannot import name 'does_not_exist' from '<unknown module name>' (unknown location)")
|
"cannot import name 'does_not_exist' from '<unknown module name>' (unknown location)")
|
||||||
|
|
||||||
|
@cpython_only
|
||||||
|
def test_issue31492(self):
|
||||||
|
# There shouldn't be an assertion failure in case of failing to import
|
||||||
|
# from a module with a bad __name__ attribute, or in case of failing
|
||||||
|
# to access an attribute of such a module.
|
||||||
|
with swap_attr(os, '__name__', None):
|
||||||
|
with self.assertRaises(ImportError):
|
||||||
|
from os import does_not_exist
|
||||||
|
|
||||||
|
with self.assertRaises(AttributeError):
|
||||||
|
os.does_not_exist
|
||||||
|
|
||||||
def test_concurrency(self):
|
def test_concurrency(self):
|
||||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'data'))
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'data'))
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
Fix assertion failures in case of failing to import from a module with a bad
|
||||||
|
``__name__`` attribute, and in case of failing to access an attribute of such
|
||||||
|
a module. Patch by Oren Milman.
|
|
@ -687,14 +687,11 @@ module_getattro(PyModuleObject *m, PyObject *name)
|
||||||
if (m->md_dict) {
|
if (m->md_dict) {
|
||||||
_Py_IDENTIFIER(__name__);
|
_Py_IDENTIFIER(__name__);
|
||||||
mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__);
|
mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__);
|
||||||
if (mod_name) {
|
if (mod_name && PyUnicode_Check(mod_name)) {
|
||||||
PyErr_Format(PyExc_AttributeError,
|
PyErr_Format(PyExc_AttributeError,
|
||||||
"module '%U' has no attribute '%U'", mod_name, name);
|
"module '%U' has no attribute '%U'", mod_name, name);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
else if (PyErr_Occurred()) {
|
|
||||||
PyErr_Clear();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
PyErr_Format(PyExc_AttributeError,
|
PyErr_Format(PyExc_AttributeError,
|
||||||
"module has no attribute '%U'", name);
|
"module has no attribute '%U'", name);
|
||||||
|
|
|
@ -4930,6 +4930,10 @@ import_from(PyObject *v, PyObject *name)
|
||||||
if (pkgname == NULL) {
|
if (pkgname == NULL) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
if (!PyUnicode_Check(pkgname)) {
|
||||||
|
Py_CLEAR(pkgname);
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
|
fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
|
||||||
if (fullmodname == NULL) {
|
if (fullmodname == NULL) {
|
||||||
Py_DECREF(pkgname);
|
Py_DECREF(pkgname);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue