Back to index

python3.2  3.2.2
Public Member Functions | Static Public Attributes
importlib.test.source.test_file_loader.SourceLoaderBadBytecodeTest Class Reference
Inheritance diagram for importlib.test.source.test_file_loader.SourceLoaderBadBytecodeTest:
Inheritance graph
[legend]
Collaboration diagram for importlib.test.source.test_file_loader.SourceLoaderBadBytecodeTest:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_empty_file
def test_partial_magic
def test_magic_only
def test_bad_magic
def test_partial_timestamp
def test_no_marshal
def test_non_code_marshal
def test_bad_marshal
def test_old_timestamp
def test_read_only_bytecode
def import_
def manipulate_bytecode

Static Public Attributes

 loader = _bootstrap._SourceFileLoader

Detailed Description

Definition at line 227 of file test_file_loader.py.


Member Function Documentation

def importlib.test.source.test_file_loader.BadBytecodeTest.import_ (   self,
  file,
  module_name 
) [inherited]

Definition at line 134 of file test_file_loader.py.

00134 
00135     def import_(self, file, module_name):
00136         loader = self.loader(module_name, file)
00137         module = loader.load_module(module_name)
00138         self.assertTrue(module_name in sys.modules)

Here is the call graph for this function:

Here is the caller graph for this function:

def importlib.test.source.test_file_loader.BadBytecodeTest.manipulate_bytecode (   self,
  name,
  mapping,
  manipulator,
  del_source = False 
) [inherited]
Manipulate the bytecode of a module by passing it into a callable
that returns what to use as the new bytecode.

Definition at line 140 of file test_file_loader.py.

00140 
00141                             del_source=False):
00142         """Manipulate the bytecode of a module by passing it into a callable
00143         that returns what to use as the new bytecode."""
00144         try:
00145             del sys.modules['_temp']
00146         except KeyError:
00147             pass
00148         py_compile.compile(mapping[name])
00149         if not del_source:
00150             bytecode_path = imp.cache_from_source(mapping[name])
00151         else:
00152             os.unlink(mapping[name])
00153             bytecode_path = make_legacy_pyc(mapping[name])
00154         if manipulator:
00155             with open(bytecode_path, 'rb') as file:
00156                 bc = file.read()
00157                 new_bc = manipulator(bc)
00158             with open(bytecode_path, 'wb') as file:
00159                 if new_bc is not None:
00160                     file.write(new_bc)
00161         return bytecode_path

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 260 of file test_file_loader.py.

00260 
00261     def test_bad_magic(self):
00262         # When the magic number is different, the bytecode should be
00263         # regenerated.
00264         def test(name, mapping, bytecode_path):
00265             self.import_(mapping[name], name)
00266             with open(bytecode_path, 'rb') as bytecode_file:
00267                 self.assertEqual(bytecode_file.read(4), imp.get_magic())
00268 
00269         self._test_bad_magic(test)

Here is the call graph for this function:

Definition at line 291 of file test_file_loader.py.

00291 
00292     def test_bad_marshal(self):
00293         # Bad marshal data should raise a ValueError.
00294         self._test_bad_marshal()

Here is the call graph for this function:

Definition at line 232 of file test_file_loader.py.

00232 
00233     def test_empty_file(self):
00234         # When a .pyc is empty, regenerate it if possible, else raise
00235         # ImportError.
00236         def test(name, mapping, bytecode_path):
00237             self.import_(mapping[name], name)
00238             with open(bytecode_path, 'rb') as file:
00239                 self.assertGreater(len(file.read()), 8)
00240 
00241         self._test_empty_file(test)

Here is the call graph for this function:

Definition at line 251 of file test_file_loader.py.

00251 
00252     def test_magic_only(self):
00253         # When there is only the magic number, regenerate the .pyc if possible,
00254         # else raise EOFError.
00255         def test(name, mapping, bytecode_path):
00256             self.import_(mapping[name], name)
00257             with open(bytecode_path, 'rb') as file:
00258                 self.assertGreater(len(file.read()), 8)

Here is the call graph for this function:

Definition at line 280 of file test_file_loader.py.

00280 
00281     def test_no_marshal(self):
00282         # When there is only the magic number and timestamp, raise EOFError.
00283         self._test_no_marshal()

Here is the call graph for this function:

Definition at line 285 of file test_file_loader.py.

00285 
00286     def test_non_code_marshal(self):
00287         self._test_non_code_marshal()
00288         # XXX ImportError when sourceless

Here is the call graph for this function:

Definition at line 297 of file test_file_loader.py.

00297 
00298     def test_old_timestamp(self):
00299         # When the timestamp is older than the source, bytecode should be
00300         # regenerated.
00301         zeros = b'\x00\x00\x00\x00'
00302         with source_util.create_modules('_temp') as mapping:
00303             py_compile.compile(mapping['_temp'])
00304             bytecode_path = imp.cache_from_source(mapping['_temp'])
00305             with open(bytecode_path, 'r+b') as bytecode_file:
00306                 bytecode_file.seek(4)
00307                 bytecode_file.write(zeros)
00308             self.import_(mapping['_temp'], '_temp')
00309             source_mtime = os.path.getmtime(mapping['_temp'])
00310             source_timestamp = importlib._w_long(source_mtime)
00311             with open(bytecode_path, 'rb') as bytecode_file:
00312                 bytecode_file.seek(4)
00313                 self.assertEqual(bytecode_file.read(4), source_timestamp)

Here is the call graph for this function:

Definition at line 242 of file test_file_loader.py.

00242 
00243     def test_partial_magic(self):
00244         def test(name, mapping, bytecode_path):
00245             self.import_(mapping[name], name)
00246             with open(bytecode_path, 'rb') as file:
00247                 self.assertGreater(len(file.read()), 8)
00248 
00249         self._test_partial_magic(test)

Here is the call graph for this function:

Definition at line 271 of file test_file_loader.py.

00271 
00272     def test_partial_timestamp(self):
00273         # When the timestamp is partial, regenerate the .pyc, else
00274         # raise EOFError.
00275         def test(name, mapping, bc_path):
00276             self.import_(mapping[name], name)
00277             with open(bc_path, 'rb') as file:
00278                 self.assertGreater(len(file.read()), 8)

Here is the call graph for this function:

Definition at line 316 of file test_file_loader.py.

00316 
00317     def test_read_only_bytecode(self):
00318         # When bytecode is read-only but should be rewritten, fail silently.
00319         with source_util.create_modules('_temp') as mapping:
00320             # Create bytecode that will need to be re-created.
00321             py_compile.compile(mapping['_temp'])
00322             bytecode_path = imp.cache_from_source(mapping['_temp'])
00323             with open(bytecode_path, 'r+b') as bytecode_file:
00324                 bytecode_file.seek(0)
00325                 bytecode_file.write(b'\x00\x00\x00\x00')
00326             # Make the bytecode read-only.
00327             os.chmod(bytecode_path,
00328                         stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
00329             try:
00330                 # Should not raise IOError!
00331                 self.import_(mapping['_temp'], '_temp')
00332             finally:
00333                 # Make writable for eventual clean-up.
00334                 os.chmod(bytecode_path, stat.S_IWUSR)
00335 

Here is the call graph for this function:


Member Data Documentation

Definition at line 229 of file test_file_loader.py.


The documentation for this class was generated from the following file: