[Python-checkins] cpython (merge 3.5 -> 3.5): Merged in brettcannon/cpython350/3.5 (pull request #2)
larry.hastings
python-checkins at python.org
Mon Aug 24 22:01:23 CEST 2015
https://hg.python.org/cpython/rev/5a9ac801f9b4
changeset: 97475:5a9ac801f9b4
branch: 3.5
parent: 97358:7cfe20a6395d
parent: 97379:cf3a62a8d786
user: larry <larry at hastings.org>
date: Tue Aug 11 18:59:15 2015 -0700
summary:
Merged in brettcannon/cpython350/3.5 (pull request #2)
Issue #24492: make sure that ``from ... import ...` raises an ImportError if __name__ is not defined on a package.
files:
Lib/test/test_import/__init__.py | 13 +++++++++++++
Misc/NEWS | 4 ++++
Python/ceval.c | 19 ++++++++++++-------
3 files changed, 29 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/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -23,6 +23,10 @@
Core and Builtins
-----------------
+- Issue #24492: A "package" lacking a __name__ attribute when trying to perform
+ a ``from .. import ...`` statement will trigger an ImportError instead of an
+ AttributeError.
+
- Issue #24667: Resize odict in all cases that the underlying dict resizes.
Library
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