예제 #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)