[Python-checkins] cpython (merge 3.5 -> default): Merge from 3.5 for issue #24492
brett.cannon
python-checkins at python.org
Fri Aug 14 20:10:30 CEST 2015
https://hg.python.org/cpython/rev/b0a6bba16b9c
changeset: 97381:b0a6bba16b9c
parent: 97378:a1daf32d920e
parent: 97380:bbe6b215df5d
user: Brett Cannon <brett at python.org>
date: Fri Aug 14 11:09:56 2015 -0700
summary:
Merge from 3.5 for issue #24492
files:
Lib/test/test_import/__init__.py | 13 +++++++++++++
Python/ceval.c | 19 ++++++++++++-------
2 files changed, 25 insertions(+), 7 deletions(-)
diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py
--- a/Lib/test/test_import/__init__.py
+++ b/Lib/test/test_import/__init__.py
@@ -324,6 +324,19 @@
with self.assertRaisesRegex(ImportError, "^cannot import name '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
class FilePermissionTests(unittest.TestCase):
diff --git a/Python/ceval.c b/Python/ceval.c
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -5085,19 +5085,24 @@
sys.modules. */
PyErr_Clear();
pkgname = _PyObject_GetAttrId(v, &PyId___name__);
- if (pkgname == NULL)
- return NULL;
+ if (pkgname == NULL) {
+ goto error;
+ }
fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Py_DECREF(pkgname);
- if (fullmodname == NULL)
+ if (fullmodname == NULL) {
return NULL;
+ }
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);
+ if (x == NULL) {
+ goto error;
+ }
+ Py_INCREF(x);
return x;
+ error:
+ PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
+ return NULL;
}
static int
--
Repository URL: https://hg.python.org/cpython
More information about the Python-checkins
mailing list