本文整理汇总了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
示例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,
)) )
示例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 )
示例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
示例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")
示例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
示例7: testNewClassSyntax
# 需要导入模块: import compiler [as 别名]
# 或者: from compiler import compile [as 别名]
def testNewClassSyntax(self):
compiler.compile("class foo():pass\n\n","<string>","exec")
示例8: testYieldExpr
# 需要导入模块: import compiler [as 别名]
# 或者: from compiler import compile [as 别名]
def testYieldExpr(self):
compiler.compile("def g(): yield\n\n", "<string>", "exec")
示例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)
示例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)
示例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)])
示例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)
示例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)
示例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)