當前位置: 首頁>>代碼示例>>Python>>正文


Python compiler.compile方法代碼示例

本文整理匯總了Python中compiler.compile方法的典型用法代碼示例。如果您正苦於以下問題:Python compiler.compile方法的具體用法?Python compiler.compile怎麽用?Python compiler.compile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在compiler的用法示例。


在下文中一共展示了compiler.compile方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: testMultipleLHS

# 需要導入模塊: import compiler [as 別名]
# 或者: from compiler import compile [as 別名]
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 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:24,代碼來源:test_transformer.py

示例2: test_compile

# 需要導入模塊: import compiler [as 別名]
# 或者: from compiler import compile [as 別名]
def test_compile():
    print( compile(
        submission = Submission(
            submission = 4 ,
            language = 'GNU G++17',
            code = open( 'testcase/compile_bomb.cpp' , "r" ).read(),
            sourcefile = 'main-144',
            time_limit = 5000,
            memory_limit = 64,
            output_limit = 64,
            stack_limit = 64,
            checker = 'wcmp',
            problem = 1,
        )) ) 
開發者ID:lutece-awesome,項目名稱:osiris,代碼行數:16,代碼來源:test.py

示例3: judge_submission

# 需要導入模塊: import compiler [as 別名]
# 或者: from compiler import compile [as 別名]
def judge_submission( submission ):
    '''
        Judge the target submission
    '''
    if submission.language is None:
        raise RuntimeError( 'Unknown Language' )
    else:
        upload_result( Report(
            result = Judge_result.PR,
            submission = submission.submission ))
    st , info = pull( lock = gloal_problem_lock.get( submission.problem ) , problem = submission.problem )
    if not st:
        raise RuntimeError( "Pull error: " + str( info ) )
    st , info = create_tempfile(
        sourcefile = submission.sourcefile,
        work_dir = submission.work_dir,
        lang = submission.language,
        code = submission.code)
    if st != 'Success':
        raise RuntimeError( "Judger Error during creating tempfile: " + str( info ) )
    if submission.language.value.compile is True:
        result , information = compile(
            submission = submission)
        if result is Judge_result.CE:
            upload_result( Report(
                result = result,
                complete = True,
                submission = submission.submission,
                compileerror_msg = information))
            return
        elif result is Judge_result.JE:
            raise RuntimeError( "Judger Error during compiling: " + str( information ) )
    submission.case = get_test_case( submission.problem )
    if len( submission.case ) == 0:
        raise RuntimeError( "Judger Error, because there is no test-data" )
    submission.data_dir = get_data_dir( get_data_dir( submission.problem ) )
    run( sub = submission ) 
開發者ID:lutece-awesome,項目名稱:osiris,代碼行數:39,代碼來源:judger.py

示例4: load_comments

# 需要導入模塊: import compiler [as 別名]
# 或者: from compiler import compile [as 別名]
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 
開發者ID:ActiveState,項目名稱:code,代碼行數:35,代碼來源:recipe-440501.py

示例5: testSyntaxError

# 需要導入模塊: import compiler [as 別名]
# 或者: from compiler import compile [as 別名]
def testSyntaxError(self):
        import compiler

        # The following snippet gives a SyntaxError in the interpreter
        #
        # If you compile and exec it, the call foo(7) returns (7, 1)
        self.assertRaises(SyntaxError, compiler.compile,
                          "def foo(a=1, b): return a, b\n\n", "<string>", "exec") 
開發者ID:ofermend,項目名稱:medicare-demo,代碼行數:10,代碼來源:outstanding_bugs.py

示例6: testCompileLibrary

# 需要導入模塊: import compiler [as 別名]
# 或者: from compiler import compile [as 別名]
def testCompileLibrary(self):
        # A simple but large test.  Compile all the code in the
        # standard library and its test suite.  This doesn't verify
        # that any of the code is correct, merely the compiler is able
        # to generate some kind of code for it.

        next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
        libdir = os.path.dirname(unittest.__file__)
        testdir = os.path.dirname(test.test_support.__file__)

        for dir in [libdir, testdir]:
            for basename in os.listdir(dir):
                # Print still working message since this test can be really slow
                if next_time <= time.time():
                    next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
                    print >>sys.__stdout__, \
                       '  testCompileLibrary still working, be patient...'
                    sys.__stdout__.flush()

                if not basename.endswith(".py"):
                    continue
                if not TEST_ALL and random() < 0.98:
                    continue
                path = os.path.join(dir, basename)
                if test.test_support.verbose:
                    print "compiling", path
                f = open(path, "U")
                buf = f.read()
                f.close()
                if "badsyntax" in basename or "bad_coding" in basename:
                    self.assertRaises(SyntaxError, compiler.compile,
                                      buf, basename, "exec")
                else:
                    try:
                        compiler.compile(buf, basename, "exec")
                    except Exception, e:
                        args = list(e.args)
                        args[0] += "[in file %s]" % basename
                        e.args = tuple(args)
                        raise 
開發者ID:ofermend,項目名稱:medicare-demo,代碼行數:42,代碼來源:test_compiler.py

示例7: testNewClassSyntax

# 需要導入模塊: import compiler [as 別名]
# 或者: from compiler import compile [as 別名]
def testNewClassSyntax(self):
        compiler.compile("class foo():pass\n\n","<string>","exec") 
開發者ID:ofermend,項目名稱:medicare-demo,代碼行數:4,代碼來源:test_compiler.py

示例8: testYieldExpr

# 需要導入模塊: import compiler [as 別名]
# 或者: from compiler import compile [as 別名]
def testYieldExpr(self):
        compiler.compile("def g(): yield\n\n", "<string>", "exec") 
開發者ID:ofermend,項目名稱:medicare-demo,代碼行數:4,代碼來源:test_compiler.py

示例9: testTryExceptFinally

# 需要導入模塊: import compiler [as 別名]
# 或者: from compiler import compile [as 別名]
def testTryExceptFinally(self):
        # Test that except and finally clauses in one try stmt are recognized
        c = compiler.compile("try:\n 1/0\nexcept:\n e = 1\nfinally:\n f = 1",
                             "<string>", "exec")
        dct = {}
        exec c in dct
        self.assertEquals(dct.get('e'), 1)
        self.assertEquals(dct.get('f'), 1) 
開發者ID:ofermend,項目名稱:medicare-demo,代碼行數:10,代碼來源:test_compiler.py

示例10: testNestedScope

# 需要導入模塊: import compiler [as 別名]
# 或者: from compiler import compile [as 別名]
def testNestedScope(self):
        c = compiler.compile('def g():\n'
                             '    a = 1\n'
                             '    def f(): return a + 2\n'
                             '    return f()\n'
                             'result = g()',
                             '<string>',
                             'exec')
        dct = {}
        exec c in dct
        self.assertEquals(dct.get('result'), 3) 
開發者ID:ofermend,項目名稱:medicare-demo,代碼行數:13,代碼來源:test_compiler.py

示例11: testGenExp

# 需要導入模塊: import compiler [as 別名]
# 或者: from compiler import compile [as 別名]
def testGenExp(self):
        c = compiler.compile('list((i,j) for i in range(3) if i < 3'
                             '           for j in range(4) if j > 2)',
                             '<string>',
                             'eval')
        self.assertEquals(eval(c), [(0, 3), (1, 3), (2, 3)]) 
開發者ID:ofermend,項目名稱:medicare-demo,代碼行數:8,代碼來源:test_compiler.py

示例12: testWith

# 需要導入模塊: import compiler [as 別名]
# 或者: from compiler import compile [as 別名]
def testWith(self):
        # SF bug 1638243
        c = compiler.compile('from __future__ import with_statement\n'
                             'def f():\n'
                             '    with TrivialContext():\n'
                             '        return 1\n'
                             'result = f()',
                             '<string>',
                             'exec' )
        dct = {'TrivialContext': TrivialContext}
        exec c in dct
        self.assertEquals(dct.get('result'), 1) 
開發者ID:ofermend,項目名稱:medicare-demo,代碼行數:14,代碼來源:test_compiler.py

示例13: testWithAss

# 需要導入模塊: import compiler [as 別名]
# 或者: from compiler import compile [as 別名]
def testWithAss(self):
        c = compiler.compile('from __future__ import with_statement\n'
                             'def f():\n'
                             '    with TrivialContext() as tc:\n'
                             '        return 1\n'
                             'result = f()',
                             '<string>',
                             'exec' )
        dct = {'TrivialContext': TrivialContext}
        exec c in dct
        self.assertEquals(dct.get('result'), 1) 
開發者ID:ofermend,項目名稱:medicare-demo,代碼行數:13,代碼來源:test_compiler.py

示例14: _testErrEnc

# 需要導入模塊: import compiler [as 別名]
# 或者: from compiler import compile [as 別名]
def _testErrEnc(self, src, text, offset):
        try:
            compile(src, "", "exec")
        except SyntaxError, e:
            self.assertEquals(e.offset, offset)
            self.assertEquals(e.text, text) 
開發者ID:ofermend,項目名稱:medicare-demo,代碼行數:8,代碼來源:test_compiler.py


注:本文中的compiler.compile方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。