Python compiler 模块,compile() 实例源码

我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用compiler.compile()

项目:oil    作者:oilshell    | 项目源码 | 文件源码
def testMultipleLHS(self):
        """ Test multiple targets on the left hand side. """

        snippets = ['a, b = 1, 2',
                    '(a, b) = 1, 2',
                    '((a, b), c) = (1, 2), 3']

        for s in snippets:
            a = transformer.parse(s)
            self.assertIsInstance(a, ast.Module)
            child1 = a.getChildNodes()[0]
            self.assertIsInstance(child1, ast.Stmt)
            child2 = child1.getChildNodes()[0]
            self.assertIsInstance(child2, ast.Assign)

            # This actually tests the compiler, but it's a way to assure the ast
            # is correct
            c = compile(s, '<string>', 'single')
            vals = {}
            exec c in vals
            assert vals['a'] == 1
            assert vals['b'] == 2
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def testMultipleLHS(self):
        """ Test multiple targets on the left hand side. """

        snippets = ['a, b = 1, 2',
                    '(a, b) = 1, 2',
                    '((a, b), c) = (1, 2), 3']

        for s in snippets:
            a = transformer.parse(s)
            self.assertIsInstance(a, ast.Module)
            child1 = a.getChildNodes()[0]
            self.assertIsInstance(child1, ast.Stmt)
            child2 = child1.getChildNodes()[0]
            self.assertIsInstance(child2, ast.Assign)

            # This actually tests the compiler, but it's a way to assure the ast
            # is correct
            c = compile(s, '<string>', 'single')
            vals = {}
            exec c in vals
            assert vals['a'] == 1
            assert vals['b'] == 2
项目:pefile.pypy    作者:cloudtracer    | 项目源码 | 文件源码
def testMultipleLHS(self):
        """ Test multiple targets on the left hand side. """

        snippets = ['a, b = 1, 2',
                    '(a, b) = 1, 2',
                    '((a, b), c) = (1, 2), 3']

        for s in snippets:
            a = transformer.parse(s)
            self.assertIsInstance(a, ast.Module)
            child1 = a.getChildNodes()[0]
            self.assertIsInstance(child1, ast.Stmt)
            child2 = child1.getChildNodes()[0]
            self.assertIsInstance(child2, ast.Assign)

            # This actually tests the compiler, but it's a way to assure the ast
            # is correct
            c = compile(s, '<string>', 'single')
            vals = {}
            exec c in vals
            assert vals['a'] == 1
            assert vals['b'] == 2
项目:ndk-python    作者:gittor    | 项目源码 | 文件源码
def testMultipleLHS(self):
        """ Test multiple targets on the left hand side. """

        snippets = ['a, b = 1, 2',
                    '(a, b) = 1, 2',
                    '((a, b), c) = (1, 2), 3']

        for s in snippets:
            a = transformer.parse(s)
            self.assertIsInstance(a, ast.Module)
            child1 = a.getChildNodes()[0]
            self.assertIsInstance(child1, ast.Stmt)
            child2 = child1.getChildNodes()[0]
            self.assertIsInstance(child2, ast.Assign)

            # This actually tests the compiler, but it's a way to assure the ast
            # is correct
            c = compile(s, '<string>', 'single')
            vals = {}
            exec c in vals
            assert vals['a'] == 1
            assert vals['b'] == 2
项目:code    作者:ActiveState    | 项目源码 | 文件源码
def load_comments(self, pkgfile):
    """ Open the package and load comments if any. 
    Return the loaded comments """

        # Note: This has to be called with a Python
        # source file (.py) only!

    if not os.path.exists(pkgfile):
        return ""

    comment = ""

    try:
        of = open(pkgfile,'rb')
        data = of.read()
        if data:
        # Create code object
                try:
                    c = compiler.compile(data,pkgfile,'exec')
                    # Get the position of first line of code
                    if c:
                        lno = c.co_firstlineno
                        lnum = 0
                        # Read file till this line number
                        of.seek(0)
                        for line in of:
                            comment = "".join((comment, line))
                            lnum += 1
                            if lnum==lno or line=="\n": break
                except SyntaxError, e:
                    pass
                except Exception, e:
                    pass
        of.close()
    except (OSError, IOError, TypeError), e:
            pass

    return comment
项目:oil    作者:oilshell    | 项目源码 | 文件源码
def getPycHeader(filename):
  # compile.c uses marshal to write a long directly, with
  # calling the interface that would also generate a 1-byte code
  # to indicate the type of the value.  simplest way to get the
  # same effect is to call marshal and then skip the code.
  #mtime = os.path.getmtime(filename)
  mtime = 0  # to make it deterministic for now
  mtime = struct.pack('<i', int(mtime))
  return imp.get_magic() + mtime
项目:oil    作者:oilshell    | 项目源码 | 文件源码
def main(argv):
  in_path = argv[1]
  out_path = argv[2]

  compileAndWrite(in_path, out_path, stdlib_comp.compile)
项目:oil    作者:oilshell    | 项目源码 | 文件源码
def main(files):
    for file in files:
        print(file)
        buf = open(file).read()
        try:
            co1 = compile(buf, file, "exec")
        except SyntaxError:
            print("skipped")
            continue
        co2 = compiler.compile(buf, file, "exec")
        co1l = extract_code_objects(co1)
        co2l = extract_code_objects(co2)
        for a, b in zip(co1l, co2l):
            compare(a, b)
项目:oil    作者:oilshell    | 项目源码 | 文件源码
def main(files):
    for file in files:
        print file
        buf = open(file).read()
        try:
            co1 = compile(buf, file, "exec")
        except SyntaxError:
            print "skipped"
            continue
        co2 = compiler.compile(buf, file, "exec")
        co1l = extract_code_objects(co1)
        co2l = extract_code_objects(co2)
        for a, b in zip(co1l, co2l):
            compare(a, b)
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def main(files):
    for file in files:
        print file
        buf = open(file).read()
        try:
            co1 = compile(buf, file, "exec")
        except SyntaxError:
            print "skipped"
            continue
        co2 = compiler.compile(buf, file, "exec")
        co1l = extract_code_objects(co1)
        co2l = extract_code_objects(co2)
        for a, b in zip(co1l, co2l):
            compare(a, b)