def getstatementrange_old(lineno, source, assertion=False): """ return (start, end) tuple which spans the minimal statement region which containing the given lineno. raise an IndexError if no such statementrange can be found. """ # XXX this logic is only used on python2.4 and below # 1. find the start of the statement from codeop import compile_command for start in range(lineno, -1, -1): if assertion: line = source.lines[start] # the following lines are not fully tested, change with care if 'super' in line and 'self' in line and '__init__' in line: raise IndexError("likely a subclass") if "assert" not in line and "raise" not in line: continue trylines = source.lines[start:lineno + 1] # quick hack to prepare parsing an indented line with # compile_command() (which errors on "return" outside defs) trylines.insert(0, 'def xxx():') trysource = '\n '.join(trylines) # ^ space here try: compile_command(trysource) except (SyntaxError, OverflowError, ValueError): continue # 2. find the end of the statement for end in range(lineno + 1, len(source) + 1): trysource = source[start:end] if trysource.isparseable(): return start, end raise SyntaxError("no valid source range around line %d " % (lineno, ))
def handle(self, line_info): """Try to get some help for the object. obj? or ?obj -> basic information. obj?? or ??obj -> more details. """ normal_handler = self.prefilter_manager.get_handler_by_name('normal') line = line_info.line # We need to make sure that we don't process lines which would be # otherwise valid python, such as "x=1 # what?" try: codeop.compile_command(line) except SyntaxError: # We should only handle as help stuff which is NOT valid syntax if line[0] == ESC_HELP: line = line[1:] elif line[-1] == ESC_HELP: line = line[:-1] if line: #print 'line:<%r>' % line # dbg self.shell.magic_pinfo(line) else: self.shell.show_usage() return '' # Empty string is needed here! except: raise # Pass any other exceptions through to the normal handler return normal_handler.handle(line_info) else: # If the code compiles ok, we should handle it normally return normal_handler.handle(line_info)
def getstatementrange(self, lineno, assertion=False): """ return (start, end) tuple which spans the minimal statement region which containing the given lineno. """ # XXX there must be a better than these heuristic ways ... # XXX there may even be better heuristics :-) if not (0 <= lineno < len(self)): raise IndexError("lineno out of range") # 1. find the start of the statement from codeop import compile_command for start in range(lineno, -1, -1): if assertion: line = self.lines[start] # the following lines are not fully tested, change with care if 'super' in line and 'self' in line and '__init__' in line: raise IndexError("likely a subclass") if "assert" not in line and "raise" not in line: continue trylines = self.lines[start:lineno+1] # quick hack to indent the source and get it as a string in one go trylines.insert(0, 'if xxx:') trysource = '\n '.join(trylines) # ^ space here try: compile_command(trysource) except (SyntaxError, OverflowError, ValueError): continue # 2. find the end of the statement for end in range(lineno+1, len(self)+1): trysource = self[start:end] if trysource.isparseable(): return start, end return start, len(self)
def test_filename(self): self.assertEqual( compile_command("a = 1\n", "abc").co_filename, compile("a = 1\n", "abc", 'single').co_filename) self.assertNotEqual( compile_command("a = 1\n", "abc").co_filename, compile("a = 1\n", "def", 'single').co_filename)
def handle(self, line_info): """Try to get some help for the object. obj? or ?obj -> basic information. obj?? or ??obj -> more details. """ normal_handler = self.prefilter_manager.get_handler_by_name('normal') line = line_info.line # We need to make sure that we don't process lines which would be # otherwise valid python, such as "x=1 # what?" try: codeop.compile_command(line) except SyntaxError: # We should only handle as help stuff which is NOT valid syntax if line[0]==ESC_HELP: line = line[1:] elif line[-1]==ESC_HELP: line = line[:-1] if line: #print 'line:<%r>' % line # dbg self.shell.magic_pinfo(line) else: self.shell.show_usage() return '' # Empty string is needed here! except: raise # Pass any other exceptions through to the normal handler return normal_handler.handle(line_info) else: # If the code compiles ok, we should handle it normally return normal_handler.handle(line_info)
def tryCode(self, iLineStart, filename, block): err = None code = None isValue = False requireMore = False try: block = block.encode('latin1').decode('utf8') except: pass try: code = compile_command('\n' * iLineStart + block, filename, 'eval') isValue = True except (OverflowError, SyntaxError, ValueError): try: code = compile_command('\n' * iLineStart + block + '\n', filename, 'exec') isValue = False except (OverflowError, SyntaxError, ValueError): self.interp.showsyntaxerror(filename) err = self.buffer.read() if not err: err = True requireMore = code is None and err is None if not requireMore: self.code = code return err, requireMore, isValue
def test_filename(self): self.assertEqual( compile_command('a = 1\n', 'abc').co_filename, compile('a = 1\n', 'abc', 'single').co_filename) self.assertNotEqual( compile_command('a = 1\n', 'abc').co_filename, compile('a = 1\n', 'def', 'single').co_filename)
def getstatementrange(self, lineno): """ return (start, end) tuple which spans the minimal statement region which containing the given lineno. """ # XXX there must be a better than these heuristic ways ... # XXX there may even be better heuristics :-) if not (0 <= lineno < len(self)): raise IndexError("lineno out of range") # 1. find the start of the statement from codeop import compile_command for start in range(lineno, -1, -1): trylines = self.lines[start:lineno + 1] # quick hack to indent the source and get it as a string in one go trylines.insert(0, 'def xxx():') trysource = '\n '.join(trylines) # ^ space here try: compile_command(trysource) except (SyntaxError, OverflowError, ValueError): pass else: break # got a valid or incomplete statement # 2. find the end of the statement for end in range(lineno + 1, len(self) + 1): trysource = self[start:end] if trysource.isparseable(): break return start, end
def getstatementrange(self, lineno, assertion=False): """ return (start, end) tuple which spans the minimal statement region which containing the given lineno. """ # XXX there must be a better than these heuristic ways ... # XXX there may even be better heuristics :-) if not (0 <= lineno < len(self)): raise IndexError("lineno out of range") # 1. find the start of the statement from codeop import compile_command for start in range(lineno, -1, -1): if assertion: line = self.lines[start] # the following lines are not fully tested, change with care if 'super' in line and 'self' in line and '__init__' in line: raise IndexError("likely a subclass") if "assert" not in line and "raise" not in line: continue trylines = self.lines[start:lineno + 1] # quick hack to indent the source and get it as a string in one go trylines.insert(0, 'if xxx:') trysource = '\n '.join(trylines) # ^ space here try: compile_command(trysource) except (SyntaxError, OverflowError, ValueError): continue # 2. find the end of the statement for end in range(lineno + 1, len(self) + 1): trysource = self[start:end] if trysource.isparseable(): return start, end return start, len(self)
def getstatementrange_old(lineno, source, assertion=False): """ return (start, end) tuple which spans the minimal statement region which containing the given lineno. raise an IndexError if no such statementrange can be found. """ # XXX this logic is only used on python2.4 and below # 1. find the start of the statement from codeop import compile_command for start in range(lineno, -1, -1): if assertion: line = source.lines[start] # the following lines are not fully tested, change with care if 'super' in line and 'self' in line and '__init__' in line: raise IndexError("likely a subclass") if "assert" not in line and "raise" not in line: continue trylines = source.lines[start:lineno+1] # quick hack to prepare parsing an indented line with # compile_command() (which errors on "return" outside defs) trylines.insert(0, 'def xxx():') trysource = '\n '.join(trylines) # ^ space here try: compile_command(trysource) except (SyntaxError, OverflowError, ValueError): continue # 2. find the end of the statement for end in range(lineno+1, len(source)+1): trysource = source[start:end] if trysource.isparseable(): return start, end raise SyntaxError("no valid source range around line %d " % (lineno,))
def test_filename(self): self.assertEquals( compile_command("a = 1\n", "abc").co_filename, compile("a = 1\n", "abc", "single").co_filename ) self.assertNotEquals( compile_command("a = 1\n", "abc").co_filename, compile("a = 1\n", "def", "single").co_filename )
def getstatementrange(self, lineno): """ return (start, end) tuple which spans the minimal statement region which containing the given lineno. """ # XXX there must be a better than these heuristic ways ... # XXX there may even be better heuristics :-) if not (0 <= lineno < len(self)): raise IndexError("lineno out of range") # 1. find the start of the statement from codeop import compile_command for start in range(lineno, -1, -1): trylines = self.lines[start:lineno+1] # quick hack to indent the source and get it as a string in one go trylines.insert(0, 'def xxx():') trysource = '\n '.join(trylines) # ^ space here try: compile_command(trysource) except (SyntaxError, OverflowError, ValueError): pass else: break # got a valid or incomplete statement # 2. find the end of the statement for end in range(lineno+1, len(self)+1): trysource = self[start:end] if trysource.isparseable(): break return start, end
def assertInvalid(self, str, symbol="single", is_syntax=1): """succeed iff str is the start of an invalid piece of code""" try: compile_command(str, symbol=symbol) self.fail("No exception thrown for invalid code") except SyntaxError: self.assertTrue(is_syntax) except OverflowError: self.assertTrue(not is_syntax)
def assertInvalid(self, str, symbol='single', is_syntax=1): '''succeed iff str is the start of an invalid piece of code''' try: compile_command(str, symbol=symbol) self.fail("No exception raised for invalid code") except SyntaxError: self.assertTrue(is_syntax) except OverflowError: self.assertTrue(not is_syntax)
def assertInvalid(self, str, symbol='single', is_syntax=1): '''succeed iff str is the start of an invalid piece of code''' try: compile_command(str,symbol=symbol) self.fail("No exception raised for invalid code") except SyntaxError: self.assertTrue(is_syntax) except OverflowError: self.assertTrue(not is_syntax)
def test_warning(self): # Test that the warning is only returned once. with warnings_helper.check_warnings((".*literal", SyntaxWarning)) as w: compile_command("0 is 0") self.assertEqual(len(w.warnings), 1) # bpo-41520: check SyntaxWarning treated as an SyntaxError with self.assertRaises(SyntaxError): warnings.simplefilter('error', SyntaxWarning) compile_command('1 is 1\n', symbol='exec')
def test_notOkIfInvalidIds(self) -> None: with self.assertRaises(SyntaxError): codeop.compile_command( 'class Color(metaclass=Enum):'\ ' R$ = 1') with self.assertRaises(SyntaxError): codeop.compile_command( 'class Class(metaclass=Enum):'\ ' 1998 = 1998')
def test_warning(self): # Test that the warning is only returned once. with warnings_helper.check_warnings( (".*literal", SyntaxWarning), (".*invalid", DeprecationWarning), ) as w: compile_command(r"'\e' is 0") self.assertEqual(len(w.warnings), 2) # bpo-41520: check SyntaxWarning treated as an SyntaxError with warnings.catch_warnings(), self.assertRaises(SyntaxError): warnings.simplefilter('error', SyntaxWarning) compile_command('1 is 1', symbol='exec')
def test_valid(self): av = self.assertValid # special case if not is_jython: self.assertEquals( compile_command(""), compile("pass", "<input>", 'single', PyCF_DONT_IMPLY_DEDENT)) self.assertEquals( compile_command("\n"), compile("pass", "<input>", 'single', PyCF_DONT_IMPLY_DEDENT)) else: av("") av("\n") av("a = 1") av("\na = 1") av("a = 1\n") av("a = 1\n\n") av("\n\na = 1\n\n") av("def x():\n pass\n") av("if 1:\n pass\n") av("\n\nif 1: pass\n") av("\n\nif 1: pass\n\n") av("def x():\n\n pass\n") av("def x():\n pass\n \n") av("def x():\n pass\n \n") av("pass\n") av("3**3\n") av("if 9==3:\n pass\nelse:\n pass\n") av("if 1:\n pass\n if 1:\n pass\n else:\n pass\n") av("#a\n#b\na = 3\n") av("#a\n\n \na=3\n") av("a=3\n\n") av("a = 9+ \\\n3") av("3**3", "eval") av("(lambda z: \n z**3)", "eval") av("9+ \\\n3", "eval") av("9+ \\\n3\n", "eval") av("\n\na**3", "eval") av("\n \na**3", "eval") av("#a\n#b\na**3", "eval")
def test_valid(self): av = self.assertValid # special case if not is_jython: self.assertEquals(compile_command(""), compile("pass", "<input>", 'single', PyCF_DONT_IMPLY_DEDENT)) self.assertEquals(compile_command("\n"), compile("pass", "<input>", 'single', PyCF_DONT_IMPLY_DEDENT)) else: av("") av("\n") av("a = 1") av("\na = 1") av("a = 1\n") av("a = 1\n\n") av("\n\na = 1\n\n") av("def x():\n pass\n") av("if 1:\n pass\n") av("\n\nif 1: pass\n") av("\n\nif 1: pass\n\n") av("def x():\n\n pass\n") av("def x():\n pass\n \n") av("def x():\n pass\n \n") av("pass\n") av("3**3\n") av("if 9==3:\n pass\nelse:\n pass\n") av("if 1:\n pass\n if 1:\n pass\n else:\n pass\n") av("#a\n#b\na = 3\n") av("#a\n\n \na=3\n") av("a=3\n\n") av("a = 9+ \\\n3") av("3**3","eval") av("(lambda z: \n z**3)","eval") av("9+ \\\n3","eval") av("9+ \\\n3\n","eval") av("\n\na**3","eval") av("\n \na**3","eval") av("#a\n#b\na**3","eval")
def test_valid(self): av = self.assertValid if not is_jython: self.assertEqual( compile_command(''), compile('pass', '<input>', 'single', PyCF_DONT_IMPLY_DEDENT)) self.assertEqual( compile_command('\n'), compile('pass', '<input>', 'single', PyCF_DONT_IMPLY_DEDENT)) else: av('') av('\n') av('a = 1') av('\na = 1') av('a = 1\n') av('a = 1\n\n') av('\n\na = 1\n\n') av('def x():\n pass\n') av('if 1:\n pass\n') av('\n\nif 1: pass\n') av('\n\nif 1: pass\n\n') av('def x():\n\n pass\n') av('def x():\n pass\n \n') av('def x():\n pass\n \n') av('pass\n') av('3**3\n') av('if 9==3:\n pass\nelse:\n pass\n') av('if 1:\n pass\n if 1:\n pass\n else:\n pass\n') av('#a\n#b\na = 3\n') av('#a\n\n \na=3\n') av('a=3\n\n') av('a = 9+ \\\n3') av('3**3', 'eval') av('(lambda z: \n z**3)', 'eval') av('9+ \\\n3', 'eval') av('9+ \\\n3\n', 'eval') av('\n\na**3', 'eval') av('\n \na**3', 'eval') av('#a\n#b\na**3', 'eval') av('\n\na = 1\n\n') av('\n\nif 1: a=1\n\n') av('if 1:\n pass\n if 1:\n pass\n else:\n pass\n') av('#a\n\n \na=3\n\n') av('\n\na**3', 'eval') av('\n \na**3', 'eval') av('#a\n#b\na**3', 'eval') av('def f():\n try: pass\n finally: [x for x in (1,2)]\n') av('def f():\n pass\n#foo\n') av('@a.b.c\ndef f():\n pass\n')
def gen_one(code, op, table=None): op['mn'] = op['mn'][0] gen = None generator = GEN_DICT.get(op['mn'][0], None) if generator is not None: gen = generator(code, op, table) try: for c in gen: codeop.compile_command(c, op['asm']) # check syntax except SyntaxError: print op['mn'] print '\n'.join(gen) print '' raise return gen
def is_complete(self, string): """ Check if a string forms a complete, executable set of commands. For the line-oriented frontend, multi-line code is not executed as soon as it is complete: the users has to enter two line returns. """ if string in ('', '\n'): # Prefiltering, eg through ipython0, may return an empty # string although some operations have been accomplished. We # thus want to consider an empty string as a complete # statement. return True elif (len(self.input_buffer.split('\n')) > 2 and not re.findall(r"\n[\t ]*\n[\t ]*$", string)): return False else: self.capture_output() try: # Add line returns here, to make sure that the statement is # complete. is_complete = codeop.compile_command(string.rstrip() + '\n\n', "<string>", "exec") self.release_output() except Exception, e: # XXX: Hack: return True so that the # code gets executed and the error captured. is_complete = True return is_complete
def process(self, cmd): """Append cmd to the current cmdtxt and try to compile it and exec cmd - wrapped Python code resp - wrapped response from python code """ self.cmdtxt += BufSocket.unwrap(cmd) if self.cmdtxt.startswith(CMDQUIT): self.stop = True self.cmdtxt = '' if self.logger is not None: self.logger.info('id:%d client quit', self.myid) return BufSocket.wrap('Bye.') sysfps = sys.stdout, sys.stderr sys.stderr = sys.stdout = fio = io.StringIO() try: compile_ok = False code = codeop.compile_command(self.cmdtxt) if code: compile_ok = True self.cmdtxt = '' exec(code, self.globs, self.locs) retval = fio.getvalue() + PS1 else: retval = PS2 except Exception: # May be syntax err. or raised exception msg = ('Runtime' if compile_ok else 'Compile') + ' exception.\n' self.cmdtxt = '' traceback.print_exc(file=fio) retval = msg + fio.getvalue() + PS1 sys.stdout, sys.stderr = sysfps fio.close() return BufSocket.wrap(retval)
def update(self): self.blocks = [] if not os.path.isfile(self.path): return self.lastUpdate = datetime.datetime.now() f = file(self.path) text = f.read() f.close() index = 0 m = self.__reBeginCode.search(text) while m: m2 = self.__reEndCode.search(text, m.end()) if not m2: raise Exception("Syntax Error: Unclosed code block") textBlock = text[index:m.start()] self.blocks.append(textBlock) codeBlock = text[m.end():m2.start()].strip() code = codeop.compile_command(codeBlock, self.path) self.blocks.append(code) index = m2.end() m = self.__reBeginCode.search(text, index) block = text[index:] self.blocks.append(block)
def submitFirstLine(self, line): if line is None: # We just got cancelled. Do nothing, and wait for someone else # to start us up again. return # Right now, we need to decide whether to continue the command # across multiple lines. try: code = compile_command(line) complete = code is not None # (Don't) Handle exceptions from compile_command. # Why? According to a comment in JES 4.3, Jython's compile_command # doesn't detect all syntax errors. (This may not be true any longer.) except SyntaxError: complete = self.incompleteRegex.match(line) is None except ValueError: complete = self.incompleteRegex.match(line) is None except OverflowError: complete = self.incompleteRegex.match(line) is None if not complete: self.continueStatement(line) else: self.finishStatement(line)
def _on_line_entered(self, line): self._pending_text += line self._pending_text += "\n" try: print "trying %s" % self._pending_text code = codeop.compile_command(self._pending_text) if code: self._pending_text = "" try: exec code in self._locals except SystemExit: MessageLoop.quit() except: fmt = traceback.format_exc() self.output(fmt) else: self.output("\n") self.output(self.ps1) else: print "no value" self.output(self.ps2) except SyntaxError, ex: self.output(str(ex)) self._pending_text = "" self.output(self.ps1)
def code_exec(self): """Execute waiting code. Called every timeout period.""" self.ready.acquire() if self._has_quit: if self.main_loop: self.main_loop.quit() return False if self.new_cmd != None: self.ready.notify() self.cmd = self.cmd + self.new_cmd self.new_cmd = None try: code = codeop.compile_command(self.cmd[:-1]) if code: self.cmd = '' exec code in self.globs, self.locs self.completer.update(self.globs, self.locs) except Exception: traceback.print_exc() self.cmd = '' self.ready.release() return True
def slotEnter(self, command): """ Called if the return key is pressed in the edit control.""" newc = self.command_build + '\n' + command # check whether command can be compiled # c set to None if incomplete try: c = codeop.compile_command(newc) except Exception: # we want errors to be caught by self.interpreter.run below c = 1 # which prompt? prompt = '>>>' if self.command_build != '': prompt = '...' # output the command in the log pane self.appendOutput('%s %s\n' % (prompt, command), 'command') # are we ready to run this? if c is None or (len(command) != 0 and len(self.command_build) != 0 and (command[0] == ' ' or command[0] == '\t')): # build up the expression self.command_build = newc # modify the prompt self._prompt.setText( '...' ) else: # actually execute the command self.interpreter.run(newc) self.command_build = '' # modify the prompt self._prompt.setText( '>>>' )
def slotEnter(self, command): """ Called if the return key is pressed in the edit control.""" newc = self.command_build + "\n" + command # check whether command can be compiled # c set to None if incomplete try: c = codeop.compile_command(newc) except Exception: # we want errors to be caught by self.interpreter.run below c = 1 # which prompt? prompt = ">>>" if self.command_build != "": prompt = "..." # output the command in the log pane self.appendOutput("%s %s\n" % (prompt, command), "command") # are we ready to run this? if c is None or ( len(command) != 0 and len(self.command_build) != 0 and (command[0] == " " or command[0] == "\t") ): # build up the expression self.command_build = newc # modify the prompt self._prompt.setText("...") else: # actually execute the command self.interpreter.run(newc) self.command_build = "" # modify the prompt self._prompt.setText(">>>")
def slotEnter(self, command): """ Called if the return key is pressed in the edit control.""" newc = self.command_build + '\n' + command # check whether command can be compiled # c set to None if incomplete try: c = codeop.compile_command(newc) except Exception: # we want errors to be caught by self.interpreter.run below c = 1 # which prompt? prompt = '>>>' if self.command_build != '': prompt = '...' # output the command in the log pane self.appendOutput('%s %s\n' % (prompt, command), 'command') # are we ready to run this? if c is None or (len(command) != 0 and len(self.command_build) != 0 and (command[0] == ' ' or command[0] == '\t')): # build up the expression self.command_build = newc # modify the prompt self._prompt.setText('...') else: # actually execute the command self.interpreter.run(newc) self.command_build = '' # modify the prompt self._prompt.setText('>>>')
def is_complete(self, string): """ Check if a string forms a complete, executable set of commands. For the line-oriented frontend, multi-line code is not executed as soon as it is complete: the users has to enter two line returns. """ if string in ('', '\n'): # Prefiltering, eg through ipython0, may return an empty # string although some operations have been accomplished. We # thus want to consider an empty string as a complete # statement. return True elif ( len(self.input_buffer.split('\n'))>2 and not re.findall(r"\n[\t ]*\n[\t ]*$", string)): return False else: self.capture_output() try: # Add line returns here, to make sure that the statement is # complete (except if '\' was used). # This should probably be done in a different place (like # maybe 'prefilter_input' method? For now, this works. clean_string = string.rstrip('\n') if not clean_string.endswith('\\'): clean_string +='\n\n' is_complete = codeop.compile_command(clean_string, "<string>", "exec") self.release_output() except Exception, e: # XXX: Hack: return True so that the # code gets executed and the error captured. is_complete = True return is_complete
def is_complete(self, string): #Based on IPython 0.10.1 if string in ('', '\n'): # Prefiltering, eg through ipython0, may return an empty # string although some operations have been accomplished. We # thus want to consider an empty string as a complete # statement. return True else: try: # Add line returns here, to make sure that the statement is # complete (except if '\' was used). # This should probably be done in a different place (like # maybe 'prefilter_input' method? For now, this works. clean_string = string.rstrip('\n') if not clean_string.endswith('\\'): clean_string += '\n\n' is_complete = codeop.compile_command(clean_string, "<string>", "exec") except Exception: # XXX: Hack: return True so that the # code gets executed and the error captured. is_complete = True return is_complete
def is_complete(self, string): #Based on IPython 0.10.1 if string in ('', '\n'): # Prefiltering, eg through ipython0, may return an empty # string although some operations have been accomplished. We # thus want to consider an empty string as a complete # statement. return True else: try: # Add line returns here, to make sure that the statement is # complete (except if '\' was used). # This should probably be done in a different place (like # maybe 'prefilter_input' method? For now, this works. clean_string = string.rstrip('\n') if not clean_string.endswith('\\'): clean_string += '\n\n' is_complete = codeop.compile_command( clean_string, "<string>", "exec" ) except Exception: # XXX: Hack: return True so that the # code gets executed and the error captured. is_complete = True return is_complete
def is_complete(self, string): """ Check if a string forms a complete, executable set of commands. For the line-oriented frontend, multi-line code is not executed as soon as it is complete: the users has to enter two line returns. """ if string in ('', '\n'): # Prefiltering, eg through ipython0, may return an empty # string although some operations have been accomplished. We # thus want to consider an empty string as a complete # statement. return True elif (len(self.input_buffer.split('\n')) > 2 and not re.findall(r"\n[\t ]*\n[\t ]*$", string)): return False else: self.capture_output() try: # Add line returns here, to make sure that the statement is # complete (except if '\' was used). # This should probably be done in a different place (like # maybe 'prefilter_input' method? For now, this works. clean_string = string.rstrip('\n') if not clean_string.endswith('\\'): clean_string += '\n\n' is_complete = codeop.compile_command(clean_string, "<string>", "exec") self.release_output() except Exception as e: # XXX: Hack: return True so that the # code gets executed and the error captured. is_complete = True return is_complete
def go(self, text): try: try: fun = codeop.compile_command("___result = " + text, "<Gzz commander input>", "single") except SyntaxError: fun = codeop.compile_command(text, "<Gzz commander input>", "single") exec fun in self.gl res = self.gl.get("___result", "") self.phys.output(repr(res)) except: typ, val, tra = sys.exc_info() self.phys.output(repr((typ, val, tra))) l = traceback.format_list(traceback.extract_tb(tra)) apply(self.phys.output, l)
def remote_debug(sig, frame): """Handler to allow process to be remotely debugged.""" def _raiseEx(ex): """Raise specified exception in the remote process""" _raiseEx.ex = ex _raiseEx.ex = None try: # Provide some useful functions. #locs = {'_raiseEx' : _raiseEx} #locs.update(frame.f_locals) # Unless shadowed. locs = frame.f_locals removeRaiseEx = False if '_raiseEx' not in locs: locs['_raiseEx'] = _raiseEx removeRaiseEx = True globs = frame.f_globals pid = os.getpid() # Use pipe name based on pid pipe = NamedPipe(pipename(pid)) old_stdout, old_stderr = sys.stdout, sys.stderr txt = '' pipe.put("Interrupting process at following point:\n" + ''.join(traceback.format_stack(frame)) + ">>> ") try: while pipe.is_open() and _raiseEx.ex is None: line = pipe.get() if line is None: continue # EOF txt += line try: code = codeop.compile_command(txt) if code: sys.stdout = cStringIO.StringIO() sys.stderr = sys.stdout exec code in globs, locs txt = '' pipe.put(sys.stdout.getvalue() + '>>> ') else: pipe.put('... ') except: txt = '' # May be syntax err. sys.stdout = cStringIO.StringIO() sys.stderr = sys.stdout traceback.print_exc() pipe.put(sys.stdout.getvalue() + '>>> ') finally: sys.stdout = old_stdout # Restore redirected output. sys.stderr = old_stderr pipe.close() if removeRaiseEx and '_raiseEx' in locs: del locs['_raiseEx'] except Exception: # Don't allow debug exceptions to propogate to real program. traceback.print_exc() if _raiseEx.ex is not None: raise _raiseEx.ex
def _checkIfCode(self, inCodeBlock): """Checks whether or not a given line appears to be Python code.""" while True: ## @formatter:off ↓ line: str lines: list lineNum: int line, lines, lineNum = (yield) # # ! return testLineNum = 1 currentLineNum = 0 testLine = line.strip() #type:str lineOfCode = None ## @formatter:on while lineOfCode is None: match = RE._errorLineRE.match(testLine) if testLine == "" or testLine == '...' or match: # These are ambiguous. line, lines, lineNum = (yield) # # ! return testLine = line.strip() # testLineNum = 1 elif testLine.startswith('>>> '): # This is definitely code. lineOfCode = True else: try: compLine = compile_command(testLine) if compLine and lines[currentLineNum].strip( ).startswith('#'): lineOfCode = True else: line, lines, lineNum = (yield) # # ! return line = line.strip() if line.startswith('>>> '): # Definitely code, don't compile further. lineOfCode = True else: testLine += linesep + line testLine = testLine.strip() testLineNum += 1 except (SyntaxError, RuntimeError): # This is definitely not code. lineOfCode = False except Exception: # Other errors are ambiguous. line, lines, lineNum = (yield) # # ! return testLine = line.strip() # testLineNum = 1 currentLineNum = lineNum - testLineNum if not inCodeBlock and lineOfCode: inCodeBlock = True lines[currentLineNum] = '{0}{1}# @code{1}'.format( lines[currentLineNum], linesep) elif inCodeBlock and lineOfCode is False: # None is ambiguous, so strict checking # against False is necessary. inCodeBlock = False lines[currentLineNum] = '{0}{1}# @endcode{1}'.format( lines[currentLineNum], linesep)
def ExtractShellCode(filepath): try: #readline.remove_history_item(readline.get_current_history_length()-1) with open(filepath, 'w') as f: for i in range(readline.get_current_history_length()): line = readline.get_history_item(i + 1) try: compile_command(line.strip()) if not ('ExtractShellCode(' in line): f.write(line + '\n') except SyntaxError: pass print('Write to ' + str(filepath) + ' succeded.') except Exception, e: print('write failed.') print(str(e)) print(repr(e))
def __try_compile(lines): try: if codeop.compile_command("\n".join(lines) + "\n"): return True else: return None except StandardError: return False
def remote_debug(sig,frame): """Handler to allow process to be remotely debugged.""" def _raiseEx(ex): """Raise specified exception in the remote process""" _raiseEx.ex = ex _raiseEx.ex = None try: # Provide some useful functions. #locs = {'_raiseEx' : _raiseEx} #locs.update(frame.f_locals) # Unless shadowed. locs = frame.f_locals removeRaiseEx = False if '_raiseEx' not in locs: locs['_raiseEx'] = _raiseEx removeRaiseEx = True globs = frame.f_globals pid = os.getpid() # Use pipe name based on pid pipe = NamedPipe(pipename(pid)) old_stdout, old_stderr = sys.stdout, sys.stderr txt = '' pipe.put("Interrupting process at following point:\n" + ''.join(traceback.format_stack(frame)) + ">>> ") try: while pipe.is_open() and _raiseEx.ex is None: line = pipe.get() if line is None: continue # EOF txt += line try: code = codeop.compile_command(txt) if code: sys.stdout = cStringIO.StringIO() sys.stderr = sys.stdout exec code in globs,locs txt = '' pipe.put(sys.stdout.getvalue() + '>>> ') else: pipe.put('... ') except: txt='' # May be syntax err. sys.stdout = cStringIO.StringIO() sys.stderr = sys.stdout traceback.print_exc() pipe.put(sys.stdout.getvalue() + '>>> ') finally: sys.stdout = old_stdout # Restore redirected output. sys.stderr = old_stderr pipe.close() if removeRaiseEx and '_raiseEx' in locs: del locs['_raiseEx'] except Exception: # Don't allow debug exceptions to propogate to real program. traceback.print_exc() if _raiseEx.ex is not None: raise _raiseEx.ex
def _checkIfCode(self, inCodeBlock): """Checks whether or not a given line appears to be Python code.""" while True: line, lines, lineNum = (yield) testLineNum = 1 currentLineNum = 0 testLine = line.strip() lineOfCode = None while lineOfCode is None: match = AstWalker.__errorLineRE.match(testLine) if not testLine or testLine == '...' or match: # These are ambiguous. line, lines, lineNum = (yield) testLine = line.strip() #testLineNum = 1 elif testLine.startswith('>>> '): # This is definitely code. lineOfCode = True else: try: compLine = compile_command(testLine) if compLine and lines[currentLineNum].strip().startswith('#'): lineOfCode = True else: line, lines, lineNum = (yield) line = line.strip() if line.startswith('>>> '): # Definitely code, don't compile further. lineOfCode = True else: testLine += linesep + line testLine = testLine.strip() testLineNum += 1 except (SyntaxError, RuntimeError): # This is definitely not code. lineOfCode = False except Exception: # Other errors are ambiguous. line, lines, lineNum = (yield) testLine = line.strip() #testLineNum = 1 currentLineNum = lineNum - testLineNum if not inCodeBlock and lineOfCode: inCodeBlock = True lines[currentLineNum] = '{0}{1}# @code{1}'.format( lines[currentLineNum], linesep ) elif inCodeBlock and lineOfCode is False: # None is ambiguous, so strict checking # against False is necessary. inCodeBlock = False lines[currentLineNum] = '{0}{1}# @endcode{1}'.format( lines[currentLineNum], linesep )
def is_incomplete(self, source): """ Get a string of code. Return True if it's incomplete and False otherwise (if it's complete or if there's a syntax error.) This is used to run single-line statements without need for ctrl+enter. """ try: r = codeop.compile_command(source) except (SyntaxError, OverflowError, ValueError): return False return (r is None)
def is_incomplete_source(src, filename="<input>", symbol="single"): """ Test if a given pytuga code is incomplete. Incomplete code may appear in users interactions when user is typing a multi line command: para cada x de 1 ate 10: ... should continue here, but user already pressed enter! """ pytuga_src= transpile(src) return codeop.compile_command(pytuga_src, filename, symbol) is None
def assertValid(self, str, symbol='single'): '''succeed iff str is a valid piece of code''' if is_jython: code = compile_command(str, "<input>", symbol) self.assert_(code) if symbol == "single": d,r = {},{} saved_stdout = sys.stdout sys.stdout = cStringIO.StringIO() try: exec code in d exec compile(str,"<input>","single") in r finally: sys.stdout = saved_stdout elif symbol == 'eval': ctx = {'a': 2} d = { 'value': eval(code,ctx) } r = { 'value': eval(str,ctx) } self.assertEquals(unify_callables(r),unify_callables(d)) else: expected = compile(str, "<input>", symbol) self.assertEquals( compile_command(str, "<input>", symbol), expected)
def assertValid(self, str, symbol='single'): '''succeed iff str is a valid piece of code''' if is_jython: code = compile_command(str, "<input>", symbol) self.assertTrue(code) if symbol == "single": d,r = {},{} saved_stdout = sys.stdout sys.stdout = io.StringIO() try: exec(code, d) exec(compile(str,"<input>","single"), r) finally: sys.stdout = saved_stdout elif symbol == 'eval': ctx = {'a': 2} d = { 'value': eval(code,ctx) } r = { 'value': eval(str,ctx) } self.assertEqual(unify_callables(r),unify_callables(d)) else: expected = compile(str, "<input>", symbol, PyCF_DONT_IMPLY_DEDENT) self.assertEqual(compile_command(str, "<input>", symbol), expected)
def assertValid(self, str, symbol="single"): """succeed iff str is a valid piece of code""" if is_jython: code = compile_command(str, "<input>", symbol) self.assertTrue(code) if symbol == "single": d, r = {}, {} saved_stdout = sys.stdout sys.stdout = cStringIO.StringIO() try: exec code in d exec compile(str, "<input>", "single") in r finally: sys.stdout = saved_stdout elif symbol == "eval": ctx = {"a": 2} d = {"value": eval(code, ctx)} r = {"value": eval(str, ctx)} self.assertEquals(unify_callables(r), unify_callables(d)) else: expected = compile(str, "<input>", symbol, PyCF_DONT_IMPLY_DEDENT) self.assertEquals(compile_command(str, "<input>", symbol), expected)
def assertValid(self, str, symbol='single'): '''succeed iff str is a valid piece of code''' if support.is_jython: code = compile_command(str, "<input>", symbol) self.assertTrue(code) if symbol == "single": d, r = {}, {} saved_stdout = sys.stdout sys.stdout = io.StringIO() try: exec(code, d) exec(compile(str, "<input>", "single"), r) finally: sys.stdout = saved_stdout elif symbol == 'eval': ctx = {'a': 2} d = {'value': eval(code, ctx)} r = {'value': eval(str, ctx)} self.assertEqual(unify_callables(r), unify_callables(d)) else: expected = compile(str, "<input>", symbol, PyCF_DONT_IMPLY_DEDENT) self.assertEqual(compile_command(str, "<input>", symbol), expected)
def runScript(self): name = self.arguments[2] path = self.configuration.scripts.get(name, None) if path is None: self.conversation(carrier.PrintText("Sorry, but this script dosen't exist")) return f = file(path) code = compile_command(f.read(),path,'exec') if code is None: self.conversation(carrier.PrintText('script is not finished, blank line at the end missing?')) f.close() self.interpreter.runcode(code) stderr = self.interpreter.getErrorStream() if stderr.len: self.conversation(carrier.SendStderr(stderr))
def handle_help(self, line, pre=None, iFun=None, theRest=None): """Try to get some help for the object. obj? or ?obj -> basic information. obj?? or ??obj -> more details. """ # We need to make sure that we don't process lines which would be # otherwise valid python, such as "x=1 # what?" try: codeop.compile_command(line) except SyntaxError: # We should only handle as help stuff which is NOT valid syntax if line[0] == self.ESC_HELP: line = line[1:] elif line[-1] == self.ESC_HELP: line = line[:-1] return '_ip.ipmagic("pinfo","%s")' % line except: # Pass any other exceptions through to the normal handler return self.handle_normal(line) else: # If the code compiles ok, we should handle it normally return self.handle_normal(line)
def run(self, source:str) -> bool: """ Attempt to compile and run a Python <source> code string. Return True if the source makes an incomplete statement. """ try: code = codeop.compile_command(source, self._filename) if code is None: # The input is incomplete. Nothing is executed. return True # The input is complete. The code object is executed. self._exec(code) except (OverflowError, SyntaxError, ValueError) as exc: # The input is incorrect. There is no stack, so no traceback either. exc.__traceback__ = None self._handle_exception(exc) return False
def code_exec (self): """Execute waiting command. Called every timeout period.""" self.ready.acquire () if self._kill: gtk.main_quit () if len(self.new_cmds) > 0: self.ready.notify () if self.cmd: self.cmd += self.new_cmds[0] #self.command_updated(self.cmd) else: self.cmd = Command(self.new_cmds[0]) self.command_created(self.cmd) del self.new_cmds[0] code = None try: code = codeop.compile_command (self.cmd.command[:-1]) try: if code: exec (code, self.globs, self.locs) self.completer.update (self.locs) self.cmd.finished = True self.command_updated(self.cmd) self.cmd = None else: self.cmd.finished = False self.command_updated(self.cmd) except Exception as ex: #traceback.print_exc () self.cmd.exec_error = ex self.command_updated(self.cmd) self.cmd = None except Exception as ex: #traceback.print_exc () self.cmd.compile_error = ex self.command_updated(self.cmd) self.cmd = None self.ready.release() return 1
def test_valid(self): av = self.assertValid av("a = 1\n") av("def x():\n pass\n") av("pass\n") av("3**3\n") av("if 9==3:\n pass\nelse:\n pass\n") av("#a\n#b\na = 3\n") av("#a\n\n \na=3\n") av("a=3\n\n") # special case self.assertEquals(compile_command(""), compile("pass", "<input>", 'single')) av("3**3","eval") av("(lambda z: \n z**3)","eval") av("#a\n#b\na**3","eval")
def exec_output(codestr): import sys import StringIO import traceback import os import codeop buf = StringIO.StringIO() stdin, stdout, stderr = sys.stdin, sys.stdout, sys.stderr sys.stdin = os.devnull sys.stdout = buf sys.stderr = buf #import util startTime = time.time() try: code = codeop.compile_command(codestr) except SyntaxError, e: print 'COMPILE ERROR!'
def get_pythonfunc_code(self, var): if not self.get_flag(var, "python"): raise Exception("%s is not a python function"%(var)) filename = self.get_flag(var, "filename") or "?" lineno = self.get_flag(var, "lineno") or "1" args = self.get_flag(var, "args") or "" body = self.get(var, expand=False) if not body or body.strip() == "": body = " pass" source = "def %s(%s):\n%s\n"%(var, args, body) newlines = "\n" * (lineno - 1) if source in pythonfunc_code_cache: return pythonfunc_code_cache[source] try: code = codeop.compile_command(newlines + source, filename) except SyntaxError, e: print "Syntax error in python function: %s"%(var) print e #print source raise