[Python-checkins] r52595 - sandbox/trunk/import_in_py/importer.py sandbox/trunk/import_in_py/mock_importer.py sandbox/trunk/import_in_py/test_importer.py

brett.cannon python-checkins at python.org
Thu Nov 2 23:27:32 CET 2006


Author: brett.cannon
Date: Thu Nov  2 23:27:31 2006
New Revision: 52595

Modified:
   sandbox/trunk/import_in_py/importer.py
   sandbox/trunk/import_in_py/mock_importer.py
   sandbox/trunk/import_in_py/test_importer.py
Log:
Fix bug where __file__, __path__, and __loader__ were not being set early
enough by the filesystem handler (must be before any code is executed).


Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py	(original)
+++ sandbox/trunk/import_in_py/importer.py	Thu Nov  2 23:27:31 2006
@@ -314,13 +314,12 @@
             return sys.modules[fullname]
         except KeyError:
             try:
-                module = self.handler.handle_code(self, fullname, self.file_path)
+                module = self.handler.handle_code(self, fullname,
+                                                    self.file_path, self.package)
             except:
                 if fullname in sys.modules:                    
                     del sys.modules[fullname]
                 raise
-            if self.package is not None:
-                module.__path__ = [self.package]
             return module
             
     def mod_time(self, path):
@@ -413,7 +412,7 @@
         data += marshal.dumps(bytecode)
         return data
 
-    def handle_code(self, loader, mod_name, path):
+    def handle_code(self, loader, mod_name, path, package=None):
         """Handle creating a new module object that is initialized from Python
         bytecode or source.
         
@@ -444,7 +443,12 @@
         source_timstamp = None
         bytecode_path = None
         module = self.new_module(mod_name)
+        # __file__, __path__, and __loader__ *must* be set on the module before
+        # any code is executed by the import.
         module.__loader__ = loader
+        module.__file__ = path
+        if package is not None:
+            module.__path__ = [package]
         sys.modules[mod_name] = module
         base_path, type_ = loader.split_path(path)
         if type_ in self.bytecode_handles:

Modified: sandbox/trunk/import_in_py/mock_importer.py
==============================================================================
--- sandbox/trunk/import_in_py/mock_importer.py	(original)
+++ sandbox/trunk/import_in_py/mock_importer.py	Thu Nov  2 23:27:31 2006
@@ -18,7 +18,7 @@
     def __init__(self, *handles):
         self.handles = handles
     
-    def handle_code(self, loader, mod_name, path):
+    def handle_code(self, loader, mod_name, path, package=None):
         """Mock implementation of a handler.
         
         An object that can have arbitrary attributes attached to it must be
@@ -28,6 +28,7 @@
         self.loader = loader
         self.module_name = mod_name
         self.path = path
+        self.package = package
         sys.modules[mod_name] = self
         return self
 
@@ -56,6 +57,8 @@
         self.py_ext = "source" if py_exists else None
         self.pyc_ext = "bytecode" if pyc_exists else None
         self.base_path = "base path"
+        self.py_path = (self.base_path, self.py_ext)
+        self.pyc_path = (self.base_path, self.pyc_ext)
         self.modification_time = 1
         # Needed for read_data on source path.
         self.source = "test_attr = None"
@@ -87,12 +90,10 @@
         return handler(py_ext, pyc_ext)
         
     def _handle_py(self, handler):
-        return handler.handle_code(self, self.module_name,
-                                    (self.base_path, self.py_ext))
+        return handler.handle_code(self, self.module_name, self.py_path)
         
     def _handle_pyc(self, handler):
-        return handler.handle_code(self, self.module_name,
-                                    (self.base_path, self.pyc_ext))
+        return handler.handle_code(self, self.module_name, self.pyc_path)
         
     def _verify_module(self, module, test_metadata=True):
         if not hasattr(module, 'test_attr'):

Modified: sandbox/trunk/import_in_py/test_importer.py
==============================================================================
--- sandbox/trunk/import_in_py/test_importer.py	(original)
+++ sandbox/trunk/import_in_py/test_importer.py	Thu Nov  2 23:27:31 2006
@@ -299,8 +299,7 @@
         self.failUnlessEqual(module.loader, pkg_loader)
         self.failUnlessEqual(module.path, self.pkg_init_path)
         self.failUnlessEqual(module.module_name, self.pkg_name)
-        self.failUnless(hasattr(module, '__path__'))
-        self.failUnlessEqual(module.__path__, [self.pkg_path])
+        self.failUnlessEqual(module.package, self.pkg_path)
             
             
 class FileSystemLoaderWhiteBoxTests(FileSystemLoaderMockEnv):


More information about the Python-checkins mailing list