[Python-checkins] r52925 - 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
Wed Dec 6 01:03:54 CET 2006


Author: brett.cannon
Date: Wed Dec 6 01:03:53 2006
New Revision: 52925
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:
Handle the case where the .pyc file has invalid bytecode (raises ImportError).
Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py	(original)
+++ sandbox/trunk/import_in_py/importer.py	Wed Dec 6 01:03:53 2006
@@ -28,7 +28,7 @@
 
 XXX Semantics
 =============
-* When bad marshalled bytecode: ``ImportError: Non-code object in bad_bc.pyc``
+* If bad bytecode in .pyc but good source, is source used and bytecode recreated?
 
 
 Things to be exposed at the Python level
@@ -69,6 +69,7 @@
 + Explicit is better than implicit.
 + Can't entire remove without requiring ``import pkg.module as module``
 on imports instead of ``from pkg import module``.
+* Any way to replace imp.acquire_lock()/release_lock() with a context manager instead?
 
 Rejected Ideas
 --------------
@@ -411,7 +412,12 @@
 return imp.get_magic() == magic
 
 def code_from_bytecode(self, bytecode):
- """Create a code object from bytecode."""
+ """Create a code object from bytecode.
+ 
+ ValueError is expected to be raised if a code object can not be created
+ from the bytecode.
+ 
+ """
 return marshal.loads(bytecode)
 
 def code_from_source(self, source, path):
@@ -489,7 +495,11 @@
 raise ImportError("bytcode is stale")
 # Bytecode is valid.
 module.__file__ = path
- code_object = self.code_from_bytecode(bytecode)
+ try:
+ code_object = self.code_from_bytecode(bytecode)
+ except ValueError:
+ raise ImportError('Non-code object in %s' %
+ str(bytecode_path))
 exec code_object in module.__dict__
 return module
 except ImportError:
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	Wed Dec 6 01:03:53 2006
@@ -65,7 +65,7 @@
 
 @classmethod
 def setup(cls, good_magic=True, good_timestamp=True, pyc_exists=True,
- py_exists=True):
+ py_exists=True, good_bytecode=True):
 """Set up the mock loader based on possible scenarios of source and
 bytecode combinations/issues."""
 self = cls('<setup>', '<setup>')
@@ -90,7 +90,10 @@
 else:
 pyc += w_long(self.modification_time - 1)
 # Needed for read_data on .pyc path.
- self.pyc = pyc + bytecode
+ if good_bytecode:
+ self.pyc = pyc + bytecode
+ else:
+ self.pyc = pyc + "asd4asfasd4ae4fds"
 self.log = []
 return self
 
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	Wed Dec 6 01:03:53 2006
@@ -570,11 +570,14 @@
 self.failUnless(not self.handler.check_magic(larger_number))
 
 def test_code_from_bytecode(self):
- # Create a code object from bytecode.
+ # Create a code object from bytecode; raises ValueError if code object
+ # cannot be created.
 code_object = self.handler.code_from_bytecode(self.bytecode)
 module = mock_importer.MockModule(self.module_name)
 exec code_object in module.__dict__
 self.verify_module(module)
+ self.failUnlessRaises(ValueError, self.handler.code_from_bytecode,
+ "234lkjfdase4")
 
 def test_code_from_source(self):
 # Create a code object from source.
@@ -618,8 +621,12 @@
 self.failUnlessRaises(ImportError, loader._handle_pyc, handler)
 
 def test_bad_bytecode_no_py(self):
- # XXX Will bad bytecode lead to an ImportError?
- pass
+ # A .pyc file with bad bytecode (but good magic number and timestamp)
+ # should raise an ImportError.
+ loader = mock_importer.MockPyPycLoader.setup(py_exists=False,
+ good_bytecode=False)
+ handler = loader._create_handler(importer.PyPycHandler)
+ self.failUnlessRaises(ImportError, loader._handle_pyc, handler)
 
 def test_bad_magic_w_py(self):
 # A .pyc file with a bad magic number should lead to the .py file being


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /