[Python-checkins] cpython: Issue #14592: A relative import will raise a KeyError if __package__
brett.cannon
python-checkins at python.org
Wed Apr 18 01:05:20 CEST 2012
http://hg.python.org/cpython/rev/68f9ad6a3b13
changeset: 76380:68f9ad6a3b13
user: Brett Cannon <brett at python.org>
date: Tue Apr 17 19:05:11 2012 -0400
summary:
Issue #14592: A relative import will raise a KeyError if __package__
or __name__ are not set in globals.
Thanks to Stefan Behnel for the bug report.
files:
Lib/importlib/test/import_/test_relative_imports.py | 5 +++++
Misc/NEWS | 3 +++
Python/import.c | 3 ++-
3 files changed, 10 insertions(+), 1 deletions(-)
diff --git a/Lib/importlib/test/import_/test_relative_imports.py b/Lib/importlib/test/import_/test_relative_imports.py
--- a/Lib/importlib/test/import_/test_relative_imports.py
+++ b/Lib/importlib/test/import_/test_relative_imports.py
@@ -203,6 +203,11 @@
self.assertEqual(mod.__name__, 'crash.mod')
self.relative_import_test(create, globals_, callback)
+ def test_relative_import_no_globals(self):
+ # No globals for a relative import is an error.
+ with self.assertRaises(KeyError):
+ import_util.import_('sys', level=1)
+
def test_main():
from test.support import run_unittest
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
Core and Builtins
-----------------
+- Issue #14592: Attempting a relative import w/o __package__ or __name__ set in
+ globals raises a KeyError.
+
- Issue #10854: The ImportError raised when an extension module on Windows
fails to import now uses the new path and name attributes from
Issue #1559549.
diff --git a/Python/import.c b/Python/import.c
--- a/Python/import.c
+++ b/Python/import.c
@@ -2355,8 +2355,9 @@
}
}
else {
- package = _PyDict_GetItemIdWithError(globals, &PyId___name__);
+ package = _PyDict_GetItemId(globals, &PyId___name__);
if (package == NULL) {
+ PyErr_SetString(PyExc_KeyError, "'__name__' not in globals");
goto error;
}
else if (!PyUnicode_Check(package)) {
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list