[Python-checkins] r57243 - in sandbox/trunk/import_in_py: _importlib.py tests/test_fs_loader.py
brett.cannon
python-checkins at python.org
Tue Aug 21 06:31:50 CEST 2007
Author: brett.cannon
Date: Tue Aug 21 06:31:49 2007
New Revision: 57243
Modified:
sandbox/trunk/import_in_py/_importlib.py
sandbox/trunk/import_in_py/tests/test_fs_loader.py
Log:
Test (and fixes) for get_code for the source loader.
Modified: sandbox/trunk/import_in_py/_importlib.py
==============================================================================
--- sandbox/trunk/import_in_py/_importlib.py (original)
+++ sandbox/trunk/import_in_py/_importlib.py Tue Aug 21 06:31:49 2007
@@ -514,7 +514,7 @@
"""
source = self.get_source(fullname)
if source is not None:
- return compile(source, self._py_path, 'exec')
+ return compile(source, self._source_path, 'exec')
else:
magic, mtime, bytecode = self.get_bytecode(fullname)
if imp.get_magic() != magic:
Modified: sandbox/trunk/import_in_py/tests/test_fs_loader.py
==============================================================================
--- sandbox/trunk/import_in_py/tests/test_fs_loader.py (original)
+++ sandbox/trunk/import_in_py/tests/test_fs_loader.py Tue Aug 21 06:31:49 2007
@@ -310,8 +310,43 @@
True)
self.assert_(loader.is_package(self.pkg_name))
+ def get_code_test(self, loader):
+ attr_name, attr_value = self.test_attr
+ code_object = loader.get_code(loader._name)
+ ns = {}
+ exec code_object in ns
+ self.assert_(attr_name in ns)
+ self.assertEqual(ns[attr_name], attr_value)
+
def test_get_code(self):
- raise NotImplementedError
+ # Source and bytecode.
+ assert os.path.exists(self.py_path) and os.path.exists(self.pyc_path)
+ loader = importlib._PyFileLoader(self.module_name, self.py_path, False)
+ self.get_code_test(loader)
+ # Source only.
+ os.unlink(self.pyc_path)
+ loader = importlib._PyFileLoader(self.module_name, self.py_path, False)
+ self.get_code_test(loader)
+ # Bytecode only.
+ py_compile.compile(self.py_path, doraise=True)
+ os.unlink(self.py_path)
+ loader = importlib._PyFileLoader(self.module_name, self.pyc_path,
+ False)
+ self.get_code_test(loader)
+ # Bad magic number in bytecode.
+ with open(self.pyc_path, 'rb') as bytecode_file:
+ data = bytecode_file.read()
+ with open(self.pyc_path, 'wb') as bytecode_file:
+ bytecode_file.write('0' * 4)
+ bytecode_file.write('0' * 4)
+ bytecode_file.write(data[8:])
+ loader = importlib._PyFileLoader(self.module_name, self.pyc_path,
+ False)
+ self.assertRaises(ImportError, loader.get_code, self.module_name)
+ # Nothing available.
+ os.unlink(self.pyc_path)
+ loader = importlib._PyFileLoader(self.module_name, self.py_path, False)
+ self.assertRaises(ImportError, loader.get_code, self.module_name)
def test_main():
More information about the Python-checkins
mailing list