mirror of
https://github.com/python/cpython.git
synced 2025-08-02 16:13:13 +00:00
Merge from 3.5 for issue #24492
This commit is contained in:
commit
d868376288
2 changed files with 25 additions and 7 deletions
|
@ -324,6 +324,19 @@ class ImportTests(unittest.TestCase):
|
||||||
with self.assertRaisesRegex(ImportError, "^cannot import name 'bogus'"):
|
with self.assertRaisesRegex(ImportError, "^cannot import name 'bogus'"):
|
||||||
from re import bogus
|
from re import bogus
|
||||||
|
|
||||||
|
def test_from_import_AttributeError(self):
|
||||||
|
# Issue #24492: trying to import an attribute that raises an
|
||||||
|
# AttributeError should lead to an ImportError.
|
||||||
|
class AlwaysAttributeError:
|
||||||
|
def __getattr__(self, _):
|
||||||
|
raise AttributeError
|
||||||
|
|
||||||
|
module_name = 'test_from_import_AttributeError'
|
||||||
|
self.addCleanup(unload, module_name)
|
||||||
|
sys.modules[module_name] = AlwaysAttributeError()
|
||||||
|
with self.assertRaises(ImportError):
|
||||||
|
from test_from_import_AttributeError import does_not_exist
|
||||||
|
|
||||||
|
|
||||||
@skip_if_dont_write_bytecode
|
@skip_if_dont_write_bytecode
|
||||||
class FilePermissionTests(unittest.TestCase):
|
class FilePermissionTests(unittest.TestCase):
|
||||||
|
|
|
@ -5085,19 +5085,24 @@ import_from(PyObject *v, PyObject *name)
|
||||||
sys.modules. */
|
sys.modules. */
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
pkgname = _PyObject_GetAttrId(v, &PyId___name__);
|
pkgname = _PyObject_GetAttrId(v, &PyId___name__);
|
||||||
if (pkgname == NULL)
|
if (pkgname == NULL) {
|
||||||
return NULL;
|
goto error;
|
||||||
|
}
|
||||||
fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
|
fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
|
||||||
Py_DECREF(pkgname);
|
Py_DECREF(pkgname);
|
||||||
if (fullmodname == NULL)
|
if (fullmodname == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
|
x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
|
||||||
if (x == NULL)
|
|
||||||
PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
|
|
||||||
else
|
|
||||||
Py_INCREF(x);
|
|
||||||
Py_DECREF(fullmodname);
|
Py_DECREF(fullmodname);
|
||||||
|
if (x == NULL) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
Py_INCREF(x);
|
||||||
return x;
|
return x;
|
||||||
|
error:
|
||||||
|
PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue