示例#1
0
 def get_source(self, fullname):
     path = self.get_filename(fullname)
     try:
         source_bytes = self.get_data(path)
     except OSError as exc:
         raise ImportError('source not available through get_data()',
                           name=fullname) from exc
     return decode_source(source_bytes)
示例#2
0
 def source_to_code(self, data, path, *, _optimize=-1):
     source = decode_source(data)
     tree = _call_with_frames_removed(compile, source, path, 'exec', ast.PyCF_ONLY_AST,
                                      dont_inherit=True, optimize=_optimize)
     tree = TypeguardTransformer().visit(tree)
     ast.fix_missing_locations(tree)
     return _call_with_frames_removed(compile, tree, path, 'exec',
                                      dont_inherit=True, optimize=_optimize)
示例#3
0
    def exec_module(self, module):
        """Import the source code, transform it before executing it so that
           it is known to Python.
        """
        if self.module_class is not None:
            module.__class__ = self.module_class

        with open(self.filename, mode="r+b") as f:
            source = decode_source(f.read())

        if self.transform_source is not None:
            source = self.transform_source(
                source,
                filename=self.filename,
                globals_=module.__dict__,
                module=module,
                callback_params=self.callback_params,
            )

        if self.source_init is not None:
            source = self.source_init() + source

        try:
            tree = ast.parse(source, self.filename)
        except Exception as e:
            print("Exception raised while parsing source.")
            raise e

        if self.transform_ast is not None:
            tree = self.transform_ast(tree)

        try:
            code_object = compile(tree, self.filename, "exec")
        except Exception as e:
            print("Exception raised while compiling tree.")
            raise e

        if self.transform_bytecode is not None:
            code_object = self.transform_bytecode(code_object)

        if self.exec_ is not None:
            self.exec_(
                code_object,
                filename=self.filename,
                globals_=module.__dict__,
                module=module,
                callback_params=self.callback_params,
            )
        else:
            try:
                exec(code_object, module.__dict__)
            except Exception as e:
                print("Exception raised while executing code object.")
                raise e
示例#4
0
 def source_to_code(self, data, path, *, _optimize=-1):
     source = decode_source(data)
     tree = ast.parse(source, filename=path)
     # tree = _call_with_frames_removed(compile, source, path, 'exec', ast.PyCF_ONLY_AST,
     #                                  dont_inherit=True, optimize=_optimize)
     tree = AstTransformer(self.name,
                           self.instrumentation_level).visit(tree)
     ast.fix_missing_locations(tree)
     # return _call_with_frames_removed(compile, tree, path, 'exec',
     #                                  dont_inherit=True, optimize=_optimize)
     print(path)
     print(astor.to_source(tree))
     return compile(tree, path, 'exec')
示例#5
0
 def source_to_code(self, data, path, *, _optimize=-1):
     source = decode_source(data)
     tree = compile(source,
                    path,
                    'exec',
                    ast.PyCF_ONLY_AST,
                    dont_inherit=True,
                    optimize=_optimize)
     self.transformer(self.name.split('.')).visit(tree)
     ast.fix_missing_locations(tree)
     return compile(tree,
                    path,
                    'exec',
                    dont_inherit=True,
                    optimize=_optimize)
示例#6
0
文件: __init__.py 项目: Lattay/worm
    def exec_module(self, module):
        """Import the source code, transform it before executing it so that
        it is known to Python.
        """
        with open(self.filename, mode="r+b") as f:
            source = decode_source(f.read())

        tree = ast.parse(source, self.filename)
        tree = self.transform_ast(self.filename, tree)
        tree = fix_missing_locations(tree)

        if self.debug:
            print(ast.dump(tree, indent=2))
            print(ast.unparse(tree))

        code_object = compile(tree, self.filename, "exec")
        exec(code_object, module.__dict__)
示例#7
0
 def get_source(self, fullname=None):
     raw = self.get_source_as_bytes(fullname)
     return decode_source(raw)
示例#8
0
 def get_source(self, fullname):
     with open(self.path, "rb") as source_file:
         return decode_source(source_file.read())
示例#9
0
 def test_universal_newlines(self):
     source = '\r\n'.join([self.source, self.source])
     source_bytes = source.encode('utf-8')
     self.assertEqual(util.decode_source(source_bytes),
                      '\n'.join([self.source, self.source]))
示例#10
0
 def test_specified_encoding(self):
     source = '# coding=latin-1\n' + self.source
     source_bytes = source.encode('latin-1')
     assert source_bytes != source.encode('utf-8')
     self.assertEqual(util.decode_source(source_bytes), source)
示例#11
0
 def test_ut8_default(self):
     source_bytes = self.source.encode('utf-8')
     self.assertEqual(util.decode_source(source_bytes), self.source)
示例#12
0
from importlib.util import decode_source

with open("./hello.py", "rb") as rf:
    data = rf.read()
print(decode_source(data))
示例#13
0
 def test_universal_newlines(self):
     source = '\r\n'.join([self.source, self.source])
     source_bytes = source.encode('utf-8')
     self.assertEqual(util.decode_source(source_bytes),
                      '\n'.join([self.source, self.source]))
示例#14
0
 def test_specified_encoding(self):
     source = '# coding=latin-1\n' + self.source
     source_bytes = source.encode('latin-1')
     assert source_bytes != source.encode('utf-8')
     self.assertEqual(util.decode_source(source_bytes), source)
示例#15
0
 def test_ut8_default(self):
     source_bytes = self.source.encode('utf-8')
     self.assertEqual(util.decode_source(source_bytes), self.source)