Example #1
0
 def verify_code(self, code_object, *, bytecode_written=False):
     super().verify_code(code_object)
     if bytecode_written:
         self.assertIn(self.cached, self.loader.written)
         data = bytearray(importlib.util.MAGIC_NUMBER)
         data.extend(importlib._w_long(self.loader.source_mtime))
         data.extend(importlib._w_long(self.loader.source_size))
         data.extend(marshal.dumps(code_object))
         self.assertEqual(self.loader.written[self.cached], bytes(data))
Example #2
0
 def verify_code(self, code_object, *, bytecode_written=False):
     super().verify_code(code_object)
     if bytecode_written:
         self.assertIn(self.cached, self.loader.written)
         data = bytearray(imp.get_magic())
         data.extend(importlib._w_long(self.loader.source_mtime))
         data.extend(importlib._w_long(self.loader.source_size))
         data.extend(marshal.dumps(code_object))
         self.assertEqual(self.loader.written[self.cached], bytes(data))
Example #3
0
 def __init__(self, path, magic=importlib.util.MAGIC_NUMBER):
     super().__init__(path)
     self.bytecode_path = importlib.util.cache_from_source(self.path)
     self.source_size = len(self.source)
     data = bytearray(magic)
     data.extend(importlib._w_long(self.source_mtime))
     data.extend(importlib._w_long(self.source_size))
     code_object = compile(self.source, self.path, 'exec',
                             dont_inherit=True)
     data.extend(marshal.dumps(code_object))
     self.bytecode = bytes(data)
     self.written = {}
Example #4
0
 def test_bad_marshal(self):
     with source_util.create_modules('_temp') as mapping:
         bytecode_path = source_util.bytecode_path(mapping['_temp'])
         source_mtime = os.path.getmtime(mapping['_temp'])
         source_timestamp = importlib._w_long(source_mtime)
         with open(bytecode_path, 'wb') as bytecode_file:
             bytecode_file.write(imp.get_magic())
             bytecode_file.write(source_timestamp)
             bytecode_file.write(b'AAAA')
         self.assertRaises(ValueError, self.import_, mapping['_temp'],
                             '_temp')
         self.assertTrue('_temp' not in sys.modules)
Example #5
0
 def test_bad_marshal(self):
     # Bad marshal data should raise a ValueError.
     with source_util.create_modules('_temp') as mapping:
         bytecode_path = source_util.bytecode_path(mapping['_temp'])
         source_mtime = os.path.getmtime(mapping['_temp'])
         source_timestamp = importlib._w_long(source_mtime)
         with open(bytecode_path, 'wb') as bytecode_file:
             bytecode_file.write(imp.get_magic())
             bytecode_file.write(source_timestamp)
             bytecode_file.write(b'AAAA')
         self.assertRaises(ValueError, self.import_, mapping['_temp'],
                             '_temp')
         self.assertTrue('_temp' not in sys.modules)
Example #6
0
 def test_bad_bytecode(self):
     zeros = b'\x00\x00\x00\x00'
     with source_util.create_modules('_temp') as mapping:
         py_compile.compile(mapping['_temp'])
         bytecode_path = source_util.bytecode_path(mapping['_temp'])
         with open(bytecode_path, 'r+b') as bytecode_file:
             bytecode_file.seek(4)
             bytecode_file.write(zeros)
         self.import_(mapping['_temp'], '_temp')
         source_mtime = os.path.getmtime(mapping['_temp'])
         source_timestamp = importlib._w_long(source_mtime)
         with open(bytecode_path, 'rb') as bytecode_file:
             bytecode_file.seek(4)
             self.assertEqual(bytecode_file.read(4), source_timestamp)
Example #7
0
    def __init__(self, source, bc={}):
        """Initialize mock.

        'bc' is a dict keyed on a module's name. The value is dict with
        possible keys of 'path', 'mtime', 'magic', and 'bc'. Except for 'path',
        each of those keys control if any part of created bytecode is to
        deviate from default values.

        """
        super().__init__(source)
        self.module_bytecode = {}
        self.path_to_bytecode = {}
        self.bytecode_to_path = {}
        for name, data in bc.items():
            self.path_to_bytecode[data['path']] = name
            self.bytecode_to_path[name] = data['path']
            magic = data.get('magic', imp.get_magic())
            mtime = importlib._w_long(data.get('mtime', self.default_mtime))
            source_size = importlib._w_long(len(self.source) & 0xFFFFFFFF)
            if 'bc' in data:
                bc = data['bc']
            else:
                bc = self.compile_bc(name)
            self.module_bytecode[name] = magic + mtime + source_size + bc
Example #8
0
    def __init__(self, source, bc={}):
        """Initialize mock.

        'bc' is a dict keyed on a module's name. The value is dict with
        possible keys of 'path', 'mtime', 'magic', and 'bc'. Except for 'path',
        each of those keys control if any part of created bytecode is to
        deviate from default values.

        """
        super().__init__(source)
        self.module_bytecode = {}
        self.path_to_bytecode = {}
        self.bytecode_to_path = {}
        for name, data in bc.items():
            self.path_to_bytecode[data['path']] = name
            self.bytecode_to_path[name] = data['path']
            magic = data.get('magic', imp.get_magic())
            mtime = importlib._w_long(data.get('mtime', self.default_mtime))
            source_size = importlib._w_long(len(self.source) & 0xFFFFFFFF)
            if 'bc' in data:
                bc = data['bc']
            else:
                bc = self.compile_bc(name)
            self.module_bytecode[name] = magic + mtime + source_size + bc
Example #9
0
 def test_old_timestamp(self):
     # When the timestamp is older than the source, bytecode should be
     # regenerated.
     zeros = b'\x00\x00\x00\x00'
     with source_util.create_modules('_temp') as mapping:
         py_compile.compile(mapping['_temp'])
         bytecode_path = imp.cache_from_source(mapping['_temp'])
         with open(bytecode_path, 'r+b') as bytecode_file:
             bytecode_file.seek(4)
             bytecode_file.write(zeros)
         self.import_(mapping['_temp'], '_temp')
         source_mtime = os.path.getmtime(mapping['_temp'])
         source_timestamp = importlib._w_long(source_mtime)
         with open(bytecode_path, 'rb') as bytecode_file:
             bytecode_file.seek(4)
             self.assertEqual(bytecode_file.read(4), source_timestamp)