]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
raise an ImportError (rather than fatal) when __import__ is not found in __builtins__...
authorBenjamin Peterson <benjamin@python.org>
Mon, 29 Apr 2013 13:08:14 +0000 (09:08 -0400)
committerBenjamin Peterson <benjamin@python.org>
Mon, 29 Apr 2013 13:08:14 +0000 (09:08 -0400)
Lib/test/test_import.py
Misc/NEWS
Python/import.c

index 470a6d23927db1c8bc123de225033af849cf2d2d..8be66a1893a4ee9cf957a28b8e0dcbc02f835542 100644 (file)
@@ -324,6 +324,13 @@ class ImportTests(unittest.TestCase):
         except ImportError:
             self.fail("fromlist must allow bogus names")
 
+    @cpython_only
+    def test_delete_builtins_import(self):
+        args = ["-c", "del __builtins__.__import__; import os"]
+        popen = script_helper.spawn_python(*args)
+        stdout, stderr = popen.communicate()
+        self.assertIn(b"ImportError", stdout)
+
 
 @skip_if_dont_write_bytecode
 class FilePermissionTests(unittest.TestCase):
index 6e6ee2ce78dca808310297e1c2427544c1df00a7..eced97a6deb125bf2db6b109d4037f9ef2fc87c7 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 3.3.2?
 Core and Builtins
 -----------------
 
+- Issue #17867: Raise an ImportError if __import__ is not found in __builtins__.
+
 - Issue #17857: Prevent build failures with pre-3.5.0 versions of sqlite3,
   such as was shipped with Centos 5 and Mac OS X 10.4.
 
index 5fc2523b268525c66d3374004cc18f07a7942fee..26261e191488be57ff13e56631e137a8cb94d730 100644 (file)
@@ -1389,7 +1389,8 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
     if (builtins_import == NULL) {
         builtins_import = _PyDict_GetItemId(interp->builtins, &PyId___import__);
         if (builtins_import == NULL) {
-            Py_FatalError("__import__ missing");
+            PyErr_SetString(PyExc_ImportError, "__import__ not found");
+            goto error_with_unlock;
         }
     }
     Py_INCREF(builtins_import);