[pypy-svn] r69969 - in pypy/trunk/pypy/module/__builtin__: . test

afa at codespeak.net afa at codespeak.net
Tue Dec 8 10:14:44 CET 2009


Author: afa
Date: Tue Dec  8 10:14:43 2009
New Revision: 69969

Modified:
   pypy/trunk/pypy/module/__builtin__/importing.py
   pypy/trunk/pypy/module/__builtin__/test/test_import.py
Log:
Be sure to check the import hooks before the list of available builtin modules.
This fixes lib-python.test_importhooks


Modified: pypy/trunk/pypy/module/__builtin__/importing.py
==============================================================================
--- pypy/trunk/pypy/module/__builtin__/importing.py	(original)
+++ pypy/trunk/pypy/module/__builtin__/importing.py	Tue Dec  8 10:14:43 2009
@@ -229,10 +229,6 @@
 def _absolute_import(space, modulename, baselevel, w_fromlist, tentative):
     w = space.wrap
 
-    # check the builtin modules
-    if modulename in space.builtin_modules:
-        return space.getbuiltinmodule(modulename)
-
     w_mod = None
     parts = modulename.split('.')
     prefix = []
@@ -295,6 +291,9 @@
 
             return w_mod
 
+        # check the builtin modules
+        if modulename in space.builtin_modules:
+            return space.getbuiltinmodule(modulename)
 
         if w_path is not None:
             for path in space.unpackiterable(w_path):

Modified: pypy/trunk/pypy/module/__builtin__/test/test_import.py
==============================================================================
--- pypy/trunk/pypy/module/__builtin__/test/test_import.py	(original)
+++ pypy/trunk/pypy/module/__builtin__/test/test_import.py	Tue Dec  8 10:14:43 2009
@@ -704,14 +704,39 @@
             def find_module(self, fullname, path=None):
                 tried_imports.append((fullname, path))
 
-        import sys
+        import sys, math
+        sys.meta_path.append(Importer())
         try:
-            sys.meta_path.append(Importer())
             import datetime
             assert len(tried_imports) == 1
-            tried_imports[0][0] == "datetime"
+            assert tried_imports[0][0] == "datetime"
+        finally:
+            sys.meta_path.pop()
+
+    def test_meta_path_block(self):
+        class ImportBlocker(object):
+            "Specified modules can't be imported, even if they are built-in"
+            def __init__(self, *namestoblock):
+                self.namestoblock = dict.fromkeys(namestoblock)
+            def find_module(self, fullname, path=None):
+                if fullname in self.namestoblock:
+                    return self
+            def load_module(self, fullname):
+                raise ImportError, "blocked"
+
+        import sys
+        modname = "errno" # an arbitrary harmless builtin module
+        mod = None
+        if modname in sys.modules:
+            mod = sys.modules
+            del sys.modules[modname]
+        sys.meta_path.append(ImportBlocker(modname))
+        try:
+            raises(ImportError, __import__, modname)
         finally:
             sys.meta_path.pop()
+            if mod:
+                sys.modules[modname] = mod
 
     def test_path_hooks_leaking(self):
         class Importer(object):



More information about the Pypy-commit mailing list