Python compiler.compile() Examples
The following are 19
code examples of compiler.compile().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
compiler
, or try the search function
.
Example #1
Source File: test_transformer.py From BinderFilter with MIT License | 6 votes |
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
Example #2
Source File: test_transformer.py From CTFCrackTools with GNU General Public License v3.0 | 6 votes |
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
Example #3
Source File: test_transformer.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 6 votes |
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
Example #4
Source File: test_transformer.py From gcblue with BSD 3-Clause "New" or "Revised" License | 6 votes |
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
Example #5
Source File: test_transformer.py From oss-ftp with MIT License | 6 votes |
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
Example #6
Source File: test_transformer.py From ironpython2 with Apache License 2.0 | 6 votes |
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
Example #7
Source File: judger.py From osiris with GNU General Public License v3.0 | 5 votes |
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 )
Example #8
Source File: recipe-440501.py From code with MIT License | 5 votes |
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
Example #9
Source File: test.py From osiris with GNU General Public License v3.0 | 5 votes |
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, )) )
Example #10
Source File: outstanding_bugs.py From medicare-demo with Apache License 2.0 | 5 votes |
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")
Example #11
Source File: test_compiler.py From medicare-demo with Apache License 2.0 | 5 votes |
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
Example #12
Source File: test_compiler.py From medicare-demo with Apache License 2.0 | 5 votes |
def testNewClassSyntax(self): compiler.compile("class foo():pass\n\n","<string>","exec")
Example #13
Source File: test_compiler.py From medicare-demo with Apache License 2.0 | 5 votes |
def testYieldExpr(self): compiler.compile("def g(): yield\n\n", "<string>", "exec")
Example #14
Source File: test_compiler.py From medicare-demo with Apache License 2.0 | 5 votes |
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)
Example #15
Source File: test_compiler.py From medicare-demo with Apache License 2.0 | 5 votes |
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)
Example #16
Source File: test_compiler.py From medicare-demo with Apache License 2.0 | 5 votes |
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)])
Example #17
Source File: test_compiler.py From medicare-demo with Apache License 2.0 | 5 votes |
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)
Example #18
Source File: test_compiler.py From medicare-demo with Apache License 2.0 | 5 votes |
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)
Example #19
Source File: test_compiler.py From medicare-demo with Apache License 2.0 | 5 votes |
def _testErrEnc(self, src, text, offset): try: compile(src, "", "exec") except SyntaxError, e: self.assertEquals(e.offset, offset) self.assertEquals(e.text, text)