[Python-checkins] r57579 - in sandbox/trunk/import_in_py: Py3K_TODO zipimport_/tests.py zipimport_/zipimport.py
brett.cannon
python-checkins at python.org
Tue Aug 28 01:02:27 CEST 2007
Author: brett.cannon
Date: Tue Aug 28 01:02:27 2007
New Revision: 57579
Modified:
sandbox/trunk/import_in_py/Py3K_TODO
sandbox/trunk/import_in_py/zipimport_/tests.py
sandbox/trunk/import_in_py/zipimport_/zipimport.py
Log:
Implement load_module. Now need to test against 2.6 regression tests.
Modified: sandbox/trunk/import_in_py/Py3K_TODO
==============================================================================
--- sandbox/trunk/import_in_py/Py3K_TODO (original)
+++ sandbox/trunk/import_in_py/Py3K_TODO Tue Aug 28 01:02:27 2007
@@ -3,7 +3,6 @@
- 2.6-specific
* Rewrite (as a whitebox re-engineering job) zipimport.
- + Existing code (pkgutil, setuptools) access _zip_directory_cache.
+ Pass 2.6 unit tests for the module.
* Put into 2.6 stdlib.
- Py3K-specific
Modified: sandbox/trunk/import_in_py/zipimport_/tests.py
==============================================================================
--- sandbox/trunk/import_in_py/zipimport_/tests.py (original)
+++ sandbox/trunk/import_in_py/zipimport_/tests.py Tue Aug 28 01:02:27 2007
@@ -210,7 +210,10 @@
actual_mtime = int(os.stat('_top_level.py').st_mtime)
importer = zipimport.zipimporter(zip_path)
zip_mtime = importer.mod_time('_top_level')
- self.assertEqual(zip_mtime, actual_mtime)
+ # There can end up being a difference of 1 (probably from rounding)
+ # but that is acceptable.
+ self.assert_(zip_mtime == actual_mtime or
+ (zip_mtime + 1) == actual_mtime)
def verify_code(self, archive_path, module):
importer = zipimport.zipimporter(archive_path)
@@ -236,19 +239,33 @@
"""Test zipimporter.load_module()."""
def test_top_level(self):
- raise NotImplementedError
+ with temp_zipfile() as zip_path:
+ importer = zipimport.zipimporter(zip_path)
+ module = importer.load_module('_top_level')
+ self.assertEqual(module.__name__, '_top_level')
+ self.assertEqual(module.__loader__, importer)
+ self.assertEqual(module.__file__,
+ os.path.join(zip_path, '_top_level.pyc'))
+ self.assert_(hasattr(module, 'attr'))
+ self.assert_(not hasattr(module, '__path__'))
+ self.assertEqual(getattr(module, 'attr'), None)
def test_pkg(self):
- raise NotImplementedError
-
- def test_submodule(self):
- raise NotImplementedError
-
- def test_subpkg(self):
- raise NotImplementedError
-
- def test_subsubmodule(self):
- raise NotImplementedError
+ with temp_zipfile() as zip_path:
+ importer = zipimport.zipimporter(zip_path)
+ module = importer.load_module('_pkg')
+ self.assertEqual(module.__name__, '_pkg')
+ self.assertEqual(module.__loader__, importer)
+ self.assertEqual(module.__file__,
+ os.path.join(zip_path, '_pkg', '__init__.pyc'))
+ self.assertEqual(module.__path__, [os.path.join(zip_path, '_pkg')])
+ self.assert_(hasattr(module, 'attr'))
+ self.assertEqual(getattr(module, 'attr'), None)
+ importer = zipimport.zipimporter(os.path.join(zip_path, '_pkg'))
+ module = importer.load_module('_pkg._subpkg')
+ self.assertEqual(module.__name__, '_pkg._subpkg')
+ self.assertEqual(module.__path__,
+ [os.path.join(zip_path, '_pkg', '_subpkg')])
def test_main():
@@ -259,7 +276,7 @@
IsPackage,
GetSource,
GetCode,
- #LoadModule
+ LoadModule
)
Modified: sandbox/trunk/import_in_py/zipimport_/zipimport.py
==============================================================================
--- sandbox/trunk/import_in_py/zipimport_/zipimport.py (original)
+++ sandbox/trunk/import_in_py/zipimport_/zipimport.py Tue Aug 28 01:02:27 2007
@@ -23,9 +23,8 @@
class zipimporter(object):
- """XXX Might need to implement 'archive', 'prefix' attributes."""
-
_handler = importlib.handle_code
+ _init_module = importlib.init_module
def __init__(self, archivepath):
"""Open the specified zip file.
@@ -97,10 +96,10 @@
return self
return None
- def get_code(self, fullname):
- """Return the code object for the module, raising ZipImportError if the
- module is not found."""
- tail = fullname.rpartition('.')[2]
+ def _handle_code(self, name):
+ """Get the code object, the path used, and whether the name points to a
+ package."""
+ tail = name.rpartition('.')[2]
info = self._check_paths(tail, '__init__')
if info:
pkg = True
@@ -109,8 +108,23 @@
if info:
pkg = False
else:
- raise ZipImportError('%s is not known' % fullname)
- return self._handler(fullname, info[0], info[1])[0]
+ raise ZipImportError('%s is not known' % name)
+ if info[0]:
+ source_path = os.path.join(self.archive, info[0])
+ else:
+ source_path = None
+ if info[1]:
+ bytecode_path = os.path.join(self.archive, info[1])
+ else:
+ bytecode_path = None
+ return self._handler(name, source_path, bytecode_path) + (pkg,)
+
+
+
+ def get_code(self, fullname):
+ """Return the code object for the module, raising ZipImportError if the
+ module is not found."""
+ return self._handle_code(fullname)[0]
def get_data(self, pathname):
"""Return the data (raw source or bytecode) for the specified module,
@@ -190,4 +204,5 @@
def load_module(self, fullname):
"""Load the specified module, returning the module or raising
ZipImportError if the module could not be found."""
- raise NotImplementedError
+ code_object, path, is_pkg = self._handle_code(fullname)
+ return self._init_module(code_object, fullname, path, is_pkg)
More information about the Python-checkins
mailing list