コード例 #1
0
def test_imp_fix_co_filename():
    import _imp

    def func(x):
        return x + x

    code = func.__code__
    old_name = code.co_filename
    _imp._fix_co_filename(code, old_name + '_more_path')
    assert code.co_filename == old_name + '_more_path'
コード例 #2
0
def _compile_bytecode(data, name=None, bytecode_path=None, source_path=None):
    """Compile bytecode as found in a pyc."""
    code = marshal.loads(data)
    if isinstance(code, _code_type):
        _bootstrap._verbose_message('code object from {!r}', bytecode_path)
        if source_path is not None:
            _imp._fix_co_filename(code, source_path)
        return code
    else:
        raise ImportError('Non-code object in {!r}'.format(bytecode_path),
                          name=name, path=bytecode_path)
コード例 #3
0
ファイル: _python3.py プロジェクト: shx2/polyloader
 def get_code(self, fullname):
     source_path = self.get_filename(fullname)
     source_mtime = None
     try:
         bytecode_path = cache_from_source(source_path)
     except NotImplementedError:
         bytecode_path = None
     else:
         try:
             st = self.path_stats(source_path)
         except NotImplementedError:
             pass
         else:
             source_mtime = int(st['mtime'])
             try:
                 data = self.get_data(bytecode_path)
             except IOError:
                 pass
             else:
                 try:
                     bytes_data = self._poly_bytes_from_bytecode(
                         fullname, data, bytecode_path, st)
                 except (ImportError, EOFError):
                     pass
                 else:
                     _verbose_message('{} matches {}', bytecode_path,
                                      source_path)
                     found = marshal.loads(bytes_data)
                     if isinstance(found, _code_type):
                         _imp._fix_co_filename(found, source_path)
                         _verbose_message('code object from {}',
                                          bytecode_path)
                         return found
                     else:
                         msg = "Non-code object in {}"
                         raise ImportError(msg.format(bytecode_path),
                                           name=fullname,
                                           path=bytecode_path)
     source_bytes = self.get_data(source_path)
     code_object = self._compiler(source_bytes, source_path, fullname)
     _verbose_message('code object from {}', source_path)
     if (not sys.dont_write_bytecode and bytecode_path is not None
             and source_mtime is not None):
         data = bytearray(MAGIC_NUMBER)
         data.extend(_w_long(source_mtime))
         data.extend(_w_long(len(source_bytes)))
         data.extend(marshal.dumps(code_object))
         try:
             self._cache_bytecode(source_path, bytecode_path, data)
             _verbose_message('wrote {!r}', bytecode_path)
         except NotImplementedError:
             pass
     return code_object
コード例 #4
0
ファイル: _python3.py プロジェクト: elfsternberg/polyloader
 def get_code(self, fullname):
     source_path = self.get_filename(fullname)
     source_mtime = None
     try:
         bytecode_path = cache_from_source(source_path)
     except NotImplementedError:
         bytecode_path = None
     else:
         try:
             st = self.path_stats(source_path)
         except NotImplementedError:
             pass
         else:
             source_mtime = int(st['mtime'])
             try:
                 data = self.get_data(bytecode_path)
             except IOError:
                 pass
             else:
                 try:
                     bytes_data = self._poly_bytes_from_bytecode(fullname, data,
                                                                 bytecode_path,
                                                                 st)
                 except (ImportError, EOFError):
                     pass
                 else:
                     _verbose_message('{} matches {}', bytecode_path,
                                     source_path)
                     found = marshal.loads(bytes_data)
                     if isinstance(found, _code_type):
                         _imp._fix_co_filename(found, source_path)
                         _verbose_message('code object from {}',
                                         bytecode_path)
                         return found
                     else:
                         msg = "Non-code object in {}"
                         raise ImportError(msg.format(bytecode_path),
                                           name=fullname, path=bytecode_path)
     source_bytes = self.get_data(source_path)
     code_object = self._compiler(source_bytes, source_path, fullname)
     _verbose_message('code object from {}', source_path)
     if (not sys.dont_write_bytecode and bytecode_path is not None and
         source_mtime is not None):
         data = bytearray(MAGIC_NUMBER)
         data.extend(_w_long(source_mtime))
         data.extend(_w_long(len(source_bytes)))
         data.extend(marshal.dumps(code_object))
         try:
             self._cache_bytecode(source_path, bytecode_path, data)
             _verbose_message('wrote {!r}', bytecode_path)
         except NotImplementedError:
             pass
     return code_object
コード例 #5
0
def update_code_co_filename(code, src_path):
    """Update the co_filename attribute of the code.

    Parameters
    ----------
    code : types.CodeType
        Code object from which the co_filename should be updated.

    src_path : string
        Path to the source file for the code object

    Returns
    -------
    updated_code : types.CodeType
        Code object whose co_filename field is set to src_path.

    """
    _fix_co_filename(code, src_path)
    return code
コード例 #6
0
ファイル: compat.py プロジェクト: nucleic/enaml
    def update_code_co_filename(code, src_path):
        """Update the co_filename attribute of the code.

        Parameters
        ----------
        code : types.CodeType
            Code object from which the co_filename should be updated.

        src_path : string
            Path to the source file for the code object

        Returns
        -------
        updated_code : types.CodeType
            Code object whose co_filename field is set to src_path.

        """
        _fix_co_filename(code, src_path)
        return code
コード例 #7
0
 def get_code(self, fullname):
     code = super().get_code(fullname)
     _imp._fix_co_filename(code, self.path[:-1])
     return code