def CMDshell(args): """Starts a shell with api.* in..""" logging_utils.prepare_logging(None) logging_utils.set_console_level(logging.DEBUG) from bot_code import bot_main from api import os_utilities from api import platforms local_vars = { 'bot_main': bot_main, 'json': json, 'os_utilities': os_utilities, 'platforms': platforms, } # Can't use: from api.platforms import * local_vars.update( (k, v) for k, v in platforms.__dict__.iteritems() if not k.startswith('_')) if args: for arg in args: exec code.compile_command(arg) in local_vars else: code.interact( 'Locals:\n ' + '\n '.join( sorted(local_vars)), None, local_vars) return 0
def validate_python_code(string: str, fieldname=None, is_expression: bool = True) -> None: """ Validate python code fields by using compile_command to ensure that expression is valid python. args: fieldname: name of field being validated. is_expression: true for validating simple single line python expression, else validated as script. """ if not string: return try: compile_command(string, symbol="eval" if is_expression else "exec") except SyntaxError as se: line_no = se.lineno - 1 or 0 offset = se.offset - 1 or 0 error_line = string if is_expression else string.split("\n")[line_no] msg = (frappe._("{} Invalid python code on line {}") .format(fieldname + ":" if fieldname else "", line_no+1)) msg += f"<br><pre>{error_line}</pre>" msg += f"<pre>{' ' * offset}^</pre>" frappe.throw(msg, title=frappe._("Syntax Error")) except Exception as e: frappe.msgprint(frappe._("{} Possibly invalid python code. <br>{}") .format(fieldname + ": " or "", str(e)), indicator="orange")
def evalCmd(msg): global srcStr srcStr += msg['cmd'] try: codeObj = code.compile_command(srcStr, '<stdin>') if codeObj: try: # if srcStr is an expr, eval it and store any non-None result in _ codeObj = code.compile_command(srcStr, '<stdin>', 'eval') if codeObj is None: # empty line return with restricted: result = eval(codeObj, restrictedScope) if result is not None: print repr(result) restrictedScope['_'] = result except SyntaxError: # non-expressions are just exec'd and have no effect on _ with restricted: exec codeObj in restrictedScope srcStr = '' else: srcStr += '\n' except: try: (exc_type, exc_value) = sys.exc_info()[:2] traceback.print_exception(exc_type, exc_value, None) finally: exc_type = exc_value = None srcStr = ''
def perform_execute_script_input(self, text): if self.input_ready_state == ScriptingProviderInputReadyState.NotReadyForInput: return ScriptingProviderExecuteResult.InvalidScriptInput if self.input_ready_state == ScriptingProviderInputReadyState.ReadyForScriptProgramInput: if len(text) == 0: return ScriptingProviderExecuteResult.SuccessfulScriptExecution self.input_ready_state = ScriptingProviderInputReadyState.NotReadyForInput self.interpreter.add_input(text) return ScriptingProviderExecuteResult.SuccessfulScriptExecution try: if isinstance(text, str): result = code.compile_command(text) else: result = code.compile_command(text.decode("charmap")) except: result = False if result is None: # Command is not complete, ask for more input return ScriptingProviderExecuteResult.IncompleteScriptInput self.input_ready_state = ScriptingProviderInputReadyState.NotReadyForInput self.interpreter.execute(text) return ScriptingProviderExecuteResult.SuccessfulScriptExecution
def compile_lines(self, lines): '''Carefully call code.compile_command and return the result.''' # # The compile command saves a lot of guessing... # https://docs.python.org/2/library/code.html#code.compile_command try: interp = self.interpreter source = '\n'.join(lines).rstrip() + '\n' return code.compile_command(source) except SyntaxError: # When pasting, try to separate lines with semicolons. if len(lines) > 1: try: source = ';'.join(lines).rstrip() + '\n' return code.compile_command(source) except SyntaxError: interp.showsyntaxerror() except Exception: interp.showtraceback() else: interp.showsyntaxerror() except Exception: interp.showtraceback() # # End the previous editing if there is any error. self.multiLine = False return 'error'
def CMDshell(args): """Starts a shell with api.* in..""" logging_utils.prepare_logging(None) logging_utils.set_console_level(logging.DEBUG) from bot_code import bot_main from api import os_utilities from api import platforms local_vars = { 'bot_main': bot_main, 'json': json, 'os_utilities': os_utilities, 'platforms': platforms, } # Can't use: from api.platforms import * local_vars.update((k, v) for k, v in platforms.__dict__.iteritems() if not k.startswith('_')) if args: for arg in args: exec code.compile_command(arg) in local_vars else: code.interact('Locals:\n ' + '\n '.join(sorted(local_vars)), None, local_vars) return 0
def _test(grammar): print("grammar: {}".format(grammar)) print("\nmutating local") xs = dict(foo = Foo()) i = code.InteractiveInterpreter(locals = xs) c = code.compile_command("foo.hoo()", "<input>", grammar) i.runcode(c) print(xs['foo'].x) print("\nmutating local, n>1 calls") i = code.InteractiveInterpreter(locals = xs) c = code.compile_command("foo.hoo(), foo.hoo()", "<input>", grammar) i.runcode(c) print(xs['foo'].x) print("\nimport os") try: code.compile_command("import os", "<input>", grammar) except Exception: traceback.print_exc() print("\nimport__(os)") c = code.compile_command("__import__('os').system('echo pwn')", "<input>", grammar) i.runcode(c) print("")
def push(self, line): if not line.strip() and len(self.buffer) == 0: # Accumulate next code blocks until they are executable (and execute them) while True: try: b = self.blocks_iter.next() except StopIteration: if self.code_block: b = [] self.is_executable = True is_compilable = True else: msg = "No more demo code available. Execute '%{0} [FILENAMES]' to reload\n" self.write(msg.format(RELOAD_FILES_CMD)) return False else: try: is_compilable = code.compile_command( "".join(b), "<stdin>", "exec") is not None except SyntaxError: is_compilable = False if self.is_executable and is_compilable: code_to_print = "".join(self.code_block) if self.color: try: # Pygments imports from pygments import highlight from pygments.lexers import PythonLexer from pygments.formatters import Terminal256Formatter code_to_print = highlight( "".join(self.code_block), PythonLexer(), Terminal256Formatter(style=FORMATTER_STYLE)) except ImportError: pass print code_to_print map( super(DemoConsole, self).push, [ line[:-1] if line[-1] == "\n" else line for line in self.code_block if line != '\n' ]) super(DemoConsole, self).push("\n") self.code_block = b self.is_executable = True return False if self.code_block: self.code_block.extend(['\n'] * self.blanks) self.code_block.extend(b) try: self.is_executable = code.compile_command( "".join(self.code_block), "<stdin>", "exec") is not None except Exception, e: import traceback print "EXCEPTION WHILE TRYING TO COMPILE:" print "".join(self.code_block) print traceback.format_exc() return False
def __nj_evaluate_expression__(expression): try: compiled_expression = code.compile_command(expression, '<input>', 'eval') except SyntaxError: compiled_expression = code.compile_command(expression, '<input>', 'exec') if compiled_expression is None: return True else: eval(compiled_expression, globals()) return False
def is_compilable(self, line_of_code): try: line_of_code = self.remove_identation(line_of_code) code.compile_command(line_of_code, "<string>", "exec") return True except Exception as bug: # print(bug) if self.is_else(line_of_code) or self.is_return(line_of_code): return True return False
def is_valid(self, line_of_code): exceptional_keywords = ["else", "return", "yield"] try: line_of_code = self.remove_identation(line_of_code) code.compile_command(line_of_code) return True except Exception as bug: if self.is_else(line_of_code) or self.is_return(line_of_code): return True return False
def main(self): __script__ = "" last = "" __co__ = None env = {} self.send(" ") while True: for __line__ in self.Inbox("inbox"): _ = None __script__ = __script__ + __line__ + "\n" try: __co__ = code.compile_command(__script__, "<stdin>", "exec") except: f = StringIO.StringIO() traceback.print_exc(file=f) self.send("EPIC FAIL", "outbox") self.send(f.getvalue()) f.close() self.send(repr(__script__)) __script__ = "" if __line__[:1] != " ": if __co__: try: __co__ = code.compile_command( "_=" + __script__, "<stdin>", "exec") except: pass sent = False try: pre = env.get("_", None) env.update(globals()) env.update(locals()) env["_"] = pre exec __co__ in env if env["_"]: self.send(env["_"]) env["_"] = None sent = True except: f = StringIO.StringIO() traceback.print_exc(file=f) self.send("EPIC FAIL") self.send(f.getvalue()) f.close() sent = True __script__ = "" if not sent: self.send(" ") else: last = __line__ else: last = __line__
def push(self, line): if not line.strip() and len(self.buffer) == 0: # Accumulate next code blocks until they are executable (and execute them) while True: try: b = self.blocks_iter.next() except StopIteration: if self.code_block: b = [] self.is_executable = True is_compilable = True else: msg = "No more demo code available. Execute '%{0} [FILENAMES]' to reload\n" self.write(msg.format(RELOAD_FILES_CMD)) return False else: try: is_compilable = code.compile_command("".join(b), "<stdin>", "exec") is not None except SyntaxError: is_compilable = False if self.is_executable and is_compilable: code_to_print = "".join(self.code_block) if self.color: try: # Pygments imports from pygments import highlight from pygments.lexers import PythonLexer from pygments.formatters import Terminal256Formatter code_to_print = highlight( "".join(self.code_block), PythonLexer(), Terminal256Formatter(style=FORMATTER_STYLE) ) except ImportError: pass print code_to_print map( super(DemoConsole, self).push, [line[:-1] if line[-1] == "\n" else line for line in self.code_block if line != "\n"], ) super(DemoConsole, self).push("\n") self.code_block = b self.is_executable = True return False if self.code_block: self.code_block.extend(["\n"] * self.blanks) self.code_block.extend(b) try: self.is_executable = code.compile_command("".join(self.code_block), "<stdin>", "exec") is not None except Exception, e: import traceback print "EXCEPTION WHILE TRYING TO COMPILE:" print "".join(self.code_block) print traceback.format_exc() return False
def _command(): """prompts and execute commands within the current python3 environement""" compiledCode = None userCode = "" line = "" while True: try: line = input(">>>") #get first line in a multiline codeblock except KeyboardInterrupt: traceback.print_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) break if line == "": break userCode += line try: compiledCode = code.compile_command(userCode) #if first line compiles, the codeblock was a one liner, skip to executing it while compiledCode == None: #get lines until codeblock compiles, syntax error is raised, or "" is entered line = input("...") ''' try: line = input("...") except Exception: traceback.print_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) line = "" ''' if line == "": userCode += "\n" else: userCode += line compiledCode = code.compile_command(userCode) except KeyboardInterrupt: traceback.print_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) break except Exception: compiledCode = None userCode = "" line = "" traceback.print_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) #traceback.print_last() #NOTE: will not work, raises an exception while printing an exception if compiledCode != None: # execute codeblock iff compiles, incase codeblock raises an error in compiliation resulting in compiledCode == None try: exec(compiledCode, globals()) except Exception: traceback.print_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) #traceback.print_last() #NOTE: will not work, raises an exception while printing an exception finally: compiledCode = None userCode = "" line = ""
def main(self): __script__ = "" __SCRIPT__ = self.console() last = "" __co__ = None env = {} self.send(" ") while True: for __line__ in self.Inbox("inbox"): _ = None __script__ = __script__ + __line__ + "\n" try: __co__ = code.compile_command(__script__, "<stdin>", "exec") except: f = StringIO.StringIO() traceback.print_exc(file=f) self.send( "EPIC FAIL", "outbox" ) self.send( f.getvalue() ) f.close() self.send( repr(__script__) ) __script__ = "" if __line__[:1] != " ": if __co__: try: __co__ = code.compile_command("_="+__script__, "<stdin>", "exec") except: pass sent = False try: pre = env.get("_", None) env.update(globals()) env.update(locals()) env["_"] = pre exec __co__ in env if env["_"]: self.send( env["_"] ) env["_"] = None sent = True except: f = StringIO.StringIO() traceback.print_exc(file=f) self.send( "EPIC FAIL" ) self.send( f.getvalue() ) f.close() sent = True __script__ = "" if not sent: self.send( " " ) else: last = __line__ else: last = __line__
def main(self): __script__ = "" __SCRIPT__ = self.console() last = "" __co__ = None env = {} try: for __line__ in __SCRIPT__: _ = None __script__ = __script__ + __line__ + "\n" try: __co__ = code.compile_command(__script__, "<stdin>", "exec") except: f = StringIO.StringIO() traceback.print_exc(file=f) print("EPIC FAIL") print(f.getvalue()) f.close() print(__script__) print(repr(__script__)) __script__ = "" if __line__[:1] != " ": if __co__: print("\nok") try: __co__ = code.compile_command( "_=" + __script__, "<stdin>", "exec") except: pass try: pre = env.get("_", None) env.update(globals()) env.update(locals()) env["_"] = pre exec __co__ in env if env["_"]: print(env["_"]) env["_"] = None except: f = StringIO.StringIO() traceback.print_exc(file=f) print("EPIC FAIL") print(f.getvalue()) f.close() __script__ = "" else: last = __line__ else: last = __line__ except EOFError: pass
def main(self): __script__ = "" __SCRIPT__ = self.console() last = "" __co__ = None env = {} try: for __line__ in __SCRIPT__: _ = None __script__ = __script__ + __line__ + "\n" try: __co__ = code.compile_command(__script__, "<stdin>", "exec") except: f = StringIO.StringIO() traceback.print_exc(file=f) print "EPIC FAIL" print f.getvalue() f.close() print __script__ print repr(__script__) __script__ = "" if __line__[:1] != " ": if __co__: print "\nok" try: __co__ = code.compile_command("_="+__script__, "<stdin>", "exec") except: pass try: pre = env.get("_", None) env.update(globals()) env.update(locals()) env["_"] = pre exec __co__ in env if env["_"]: print env["_"] env["_"] = None except: f = StringIO.StringIO() traceback.print_exc(file=f) print "EPIC FAIL" print f.getvalue() f.close() __script__ = "" else: last = __line__ else: last = __line__ except EOFError: pass
def _compileStatement(self): try: return code.compile_command('\n'.join(self.__statement) + '\n', self.__source) except SyntaxError: self.__chunkResult += self._getSyntaxError() self._forgetStatement()
def compile_and_run(self, python_code_to_execute, dontcompile=False): code_obj = None self.at_prompt = False try: if dontcompile: self.runcode(python_code_to_execute) else: try: code_obj = code.compile_command(python_code_to_execute) except SyntaxError as exc_instance: raise RuntimeError(str(exc_instance)) else: if code_obj is None: # input is incomplete raise EOFError else: self.runcode(code_obj) if self.error.tell() > 0: error_string = self.error.getvalue() self.error = io.StringIO() raise RuntimeError(error_string) finally: self.at_prompt = True
def debug_timing_test_pycode_from_a_dialog( ): #bruce 051117 # TODO: rewrite this to call grab_text_using_dialog (should be easy) title = "debug: time python code" label = "one line of python to compile and exec REPEATEDLY in debug.py's globals()\n(or use @@@ to fake \\n for more lines)" from PyQt4.Qt import QInputDialog parent = None text, ok = QInputDialog.getText(parent, title, label) # parent argument needed only in Qt4 [bruce 070329, more info above] if not ok: print "time python code code: cancelled" return # fyi: type(text) == <class '__main__.qt.QString'> command = str(text) command = command.replace("@@@",'\n') print "trying to time the exec or eval of command:",command from code import compile_command try: try: mycode = compile( command + '\n', '<string>', 'exec') #k might need '\n\n' or '' or to be adaptive in length? # 'single' means print value if it's an expression and value is not None; for timing we don't want that so use 'eval' # or 'exec' -- but we don't know which one is correct! So try exec, if that fails try eval. print "exec" # this happens even for an expr like 2+2 -- why? except SyntaxError: print "try eval" # i didn't yet see this happen mycode = compile_command( command + '\n', '<string>', 'eval') except: print_compact_traceback("exception in compile_command: ") return if mycode is None: print "incomplete command:",command return # mycode is now a code object print_exec_timing_explored(mycode)
def listToCodeObj(code_list, locals_dict): ''' Converts code_list, a list of Python strings, into a function object. code_list should only rely on one parameter, "parameters", which consists of a list. ''' function_name = "stringFunction" function_declare = "def " + function_name + "(parameters):" #prepend some whitespace to each line for i in range(0, len(code_list)): code_list[i] = STD_WHITESPACE + code_list[i] #add the define to the beginning of the list code_list.insert(0, function_declare) #convert the list into a string delimited by '\n's code_string = "" for line in code_list: code_string = code_string + line + '\n' #finally ok to compile it code_obj = compile_command(code_string, "sim_file") #, "exec") #exec code_obj in globals(), locals_dict return code_obj
def onConsoleCommand(self, command): if command == ' ': command = '' try: cmd = code.compile_command(self.commandbuffer + "\n" + command) except BaseException as e: self.commandbuffer = '' return str(e) if cmd is None: self.commandbuffer += "\n" + command return '' self.commandbuffer = '' oldout = sys.stdout class console_file(object): def __init__(self, copy = None): self.buffer = '' self.copy = copy def write(self, string): parts = (self.buffer + string).split("\n") self.buffer = parts.pop() for p in parts: horizons.main.fife.console.println(p) self.copy.write(string) def __del__(self): if len(self.buffer) > 0: self.write('\n') sys.stdout = console_file(oldout) try: exec cmd in globals() except BaseException as e: sys.stdout = oldout return str(e) sys.stdout = oldout return ''
def compile_and_run(self, client_uuid, python_code_to_execute): code_obj = None try: code_obj = code.compile_command(python_code_to_execute) except SyntaxError, exc_instance: raise RuntimeError(str(exc_instance))
def _runsource(self, source): """Tries to run the source code (asks for more lines as needed). """ # Ignore empty line, continue interactive prompt if not source: return False # Handle ctrl+D (End Of File) input, stop interactive prompt if source == "EOF": self.conn.close() # Do this so OS prompt is on a new line self.stdout.write("\n") # Quit the run loop self.stop = True return False # Handle ipm-specific commands if source.split()[0] in Interactive.ipmcommands: cmd.Cmd.onecmd(self, source) return False # Attempt to compile the code try: codeobj = code.compile_command(source, COMPILE_FN, COMPILE_MODE) except Exception, e: self.stdout.write("%s:%s\n" % (e.__class__.__name__, e)) return
def interact(self, verbose=False, debug=False): try: while True: line = self.raw_input('$ ') if len(line.strip()) > 0: code_ast, code_args = self.parser.parse(line, debug=debug) if code_ast is None: print "%s" % code_args else: code_py = "(%s)" % self.parser.codegen(code_ast) if code_args[0] == 'args' and code_args[1] is not None: code_py += "%s" % (code_args[1], ) try: code_co = code.compile_command(code_py) except: code_co = None if code_co is None: print "Code (%s) did not compile" % (code_py) else: if verbose: print code_ast self.dump_ast(code_ast) print code_py try: exec code_co except: e, v, s = sys.exc_info() print "Runtime error (%s)" % v else: print "%s" % code_py if verbose: self.dump_ast(code_ast) print except EOFError: pass
def __call__(self, buf): c = code.compile_command(buf, "<console>") if c is None: return CMD_UNFINISHED exec c in self.scope return CMD_HANDLED
def run_command_string(self, command, end_is_visible = True): "Used for both interactive commands and script files" # Clear undo/redo buffer, we don't want prompts and output in there. self.te.setUndoRedoEnabled(False) if len(self.multiline_command) > 0: # multi-line command can only be ended with a blank line. if len(command) == 0: command = self.multiline_command + "\n" # execute it now else: self.multiline_command = self.multiline_command + "\n" + command command = "" # skip execution until next time # Add carriage return, so output will appear on subsequent line. # (It would be too late if we waited for plainTextEdit # to process the <Return>) self.te.moveCursor(QTextCursor.End) self.append("") # We consumed the key event, so we have to add the newline. if len(command) > 0: self.prompt = self.regular_prompt try: co = code.compile_command(command, filename='<console>') if co is None: # incomplete command self.multiline_command = command self.prompt = self.more_prompt else: exec co in console_context._context() self.multiline_command = "" except: # Exception e: print_exc() self.multiline_command = "" self.place_new_prompt(end_is_visible)
def handle(self): if self.client_address not in self.server.authenticated and not self.tryLogin( ): return import code, io, traceback buf = [] while True: data = self.request.recv(1024).rstrip() if data == '\x04' or data == 'exit' or data == 'quit': # \x04 == ^D return buf.append(data) orig_displayhook, orig_stdout = sys.displayhook, sys.stdout sio = io.StringIO() continuation = False #print "buffer:",buf try: comp = code.compile_command('\n'.join(buf)) if comp: sys.displayhook = self.displayhook sys.stdout = sio exec(comp) self.request.send(sio.getvalue()) buf = [] else: self.request.send('... ') continuation = True except: self.request.send(traceback.format_exc()) buf = [] finally: sys.displayhook, sys.stdout = orig_displayhook, orig_stdout if not continuation: self.request.send('\n>>> ')
def connect(self, sid=None): if sid == None: r = self._request('POST', 'sessions/py') if self._check(r, 201, 'POST new session failed'): sid = int(r.read()) else: return False r = self._request('GET', 'sessions/py/%d' % sid) if self._check(r, 200, 'GET session failed'): while True: buf = raw_input('>>> ') try: while not code.compile_command(buf): buf = buf + '\n' + raw_input('... ') if "exit()" == buf : self.close() break r = self._request('PUT', 'sessions/py/%d' % sid, buf) self._check(r, 200, 'PUT statement failed') result = r.read() if result and len(result.strip()) > 0: print result, if not result[-1] == '\n': print except SyntaxError, e: print e
def _handle_line(self, sock, line): peer = sock.getpeername() outp = repr(peer) + ">> " + line cmd = None try: cmd = code.compile_command(line, repr(peer), "eval") self.sofar = "" except SyntaxError as e: outp += repr(e) self.sofar = "" except (OverflowError, ValueError) as e: outp += repr(e) self.sofar = "" if cmd: try: outp += repr(eval(cmd, self.env)) except Exception: outp += traceback.format_exc().replace("\n", "\r\n") else: # incomplete command # self.sofar += line pass if self.sofar: outp += "\r\n... " else: outp += "\r\n>>> " for client in self.connected: client.sendall(outp) print repr(outp)
def exec_python_script(self): """ Choose a python source and execute it """ filename = qt.QtGui.QFileDialog.getOpenFileName( self, "Python Script", "Python script (*.py)") filename = str(filename) if(not filename): return # Try if IPython try: file = open(filename, 'r') src = "" for f in file: src += f self.interpreterWidget.get_interpreter().runcode(src, hidden=False) file.close() except: file = open(filename, 'r') sources = '' compiled = None import code for line in file: sources += line compiled = code.compile_command(sources, filename) if(compiled): self.interpreterWidget.get_interpreter().runcode(compiled) sources = '' file.close() sources = ''
def run_one(self, input): """Run one input and collect all forms of output. """ retdict = {} try: ret = code.compile_command(input) except (SyntaxError, OverflowError, ValueError): etype, value, tb = sys.exc_info() retdict['traceback'] = self.excepthook.text(etype, value, tb) return retdict if ret is None: return None trap = OutputTrap.OutputTrap() try: trap.trap_all() exec ret in self.namespace trap.release_all() except: etype, value, tb = sys.exc_info() retdict['traceback'] = self.excepthook.text(etype, value, tb) return retdict if self.last_repr is not None: retdict['output'] = self.last_repr self.last_repr = None stdout = trap.out.getvalue() if stdout: retdict['stdout'] = stdout stderr = trap.err.getvalue() if stderr: retdict['stderr'] = stderr return retdict
def run_command(self, text): """Process the next line of input. This is called when the user presses ENTER in the console widget. If this new text completes a command, the command is executed, otherwise this text is added to a buffer awaiting the next line. @param text: The next line of input. Does not include the newline. """ if text: self.history.append(text) self.history_curr_index = len(self.history) self.new_line(text, self.line.prompt) self.buffer.append(text) command = '\n'.join(self.buffer) sys.stdout = self sys.stderr = self code = None try: code = compile_command(command) except Exception, e: self.handle_exception(e) self.buffer = []
def handle(self): if self.client_address not in self.server.authenticated and not self.tryLogin(): return import code,io,traceback buf=[] while True: data = self.request.recv(1024).rstrip() if data=='\x04' or data=='exit' or data=='quit': # \x04 == ^D return buf.append(data) orig_displayhook,orig_stdout=sys.displayhook,sys.stdout sio=io.StringIO() continuation=False #print "buffer:",buf try: comp=code.compile_command('\n'.join(buf)) if comp: sys.displayhook=self.displayhook sys.stdout=sio future.utils.exec_(comp) self.request.send(sio.getvalue()) buf=[] else: self.request.send('... '); continuation=True except: self.request.send(traceback.format_exc()) buf=[] finally: sys.displayhook,sys.stdout=orig_displayhook,orig_stdout if not continuation: self.request.send('\n>>> ')
def process_line(self): command = self.get_input() self.append_text( "\n", "stdout" ) if command: self.history.insert( 0, command ) self.cur_history_index = -1 if (command and command[-1] in (":", "\\"))\ or (self.in_block and command and ( command[0] in (' ', '\t'))): self.pending_command += command + '\n' if command[-1] == ':': self.in_block = True else: full_command = self.pending_command + ("%s\n" % command) try: compiled = code.compile_command( full_command ) self.run_command( None, compiled ) except SyntaxError: self.print_error( full_command ) if self.pending_command: self.pending_command = '' self.in_block = False self.write_prompt()
def eval_line(self, line): ''' evaluate a single line of code; returns None if more lines required to compile, a string if input is complete ''' try: cmd = code.compile_command("\n".join(self.sofar + [line])) if cmd: # complete command self.sofar = [] buff = StringIO() sys.stdout = buff sys.stderr = buff try: sys.displayhook = self._displayhook exec cmd in self.locals sys.displayhook = _sys_displayhook return buff.getvalue() except: return traceback.format_exc() else: # incomplete command self.sofar.append(line) return except (OverflowError, ValueError, SyntaxError) as e: self.sofar = [] return repr(e) finally: # ensure sys.stdout / sys.stderr back to normal sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ sys.displayhook = _sys_displayhook
def exec_python_script(self): """ Choose a python source and execute it """ filename = qt.QtGui.QFileDialog.getOpenFileName( self, "Python Script", "Python script (*.py)") filename = str(filename) if (not filename): return # Try if IPython try: file = open(filename, 'r') src = "" for f in file: src += f self.interpreterWidget.get_interpreter().runcode(src, hidden=False) file.close() except: file = open(filename, 'r') sources = '' compiled = None import code for line in file: sources += line compiled = code.compile_command(sources, filename) if (compiled): self.interpreterWidget.get_interpreter().runcode(compiled) sources = '' file.close() sources = ''
def processSource(self, source): """Runs some source code in the object's context. The return value will be True if the code is valid but incomplete, or False if the code is complete (whether by error or not). If the code is complete, the "output" attribute will have the text output of execution (stdout and stderr). """ self.fresh() user = users.get_current_user() if not user: user = '******' user = '******' % (user, os.environ['REMOTE_ADDR']) source = self.getPending() + source logging.info('Compiling for: %s >>> %s' % (user, source)) try: bytecode = code.compile_command(source, '<string>', 'single') except BaseException, e: self.setPending('') self.exc_type = type(e) self.err = traceback.format_exc() logging.info('Compile error for: %s\n%s' % (user, self.err.strip())) return False # Code execution completed (the hard way).
def console_exec(thread_id, frame_id, expression, dbg): """returns 'False' in case expression is partially correct """ frame = pydevd_vars.find_frame(thread_id, frame_id) is_multiline = expression.count('@LINE@') > 1 try: expression = str(expression.replace('@LINE@', '\n')) except UnicodeEncodeError as e: expression = expression.replace('@LINE@', '\n') # Not using frame.f_globals because of https://sourceforge.net/tracker2/?func=detail&aid=2541355&group_id=85796&atid=577329 # (Names not resolved in generator expression in method) # See message: http://mail.python.org/pipermail/python-list/2009-January/526522.html updated_globals = {} updated_globals.update(frame.f_globals) updated_globals.update( frame.f_locals ) # locals later because it has precedence over the actual globals if PYTEST_RUN_CONFIG: enable_pytest_output() if IPYTHON: need_more = exec_code(CodeFragment(expression), updated_globals, updated_globals, dbg) if not need_more: update_frame_local_variables_and_save(frame, updated_globals) return need_more interpreter = ConsoleWriter() if not is_multiline: try: code = compile_command(expression) except (OverflowError, SyntaxError, ValueError): # Case 1 interpreter.showsyntaxerror() return False if code is None: # Case 2 return True else: code = expression # Case 3 try: # It is important that globals and locals we pass to the exec function are the same object. # Otherwise generator expressions can confuse their scope. Passing updated_globals dictionary seems to be a safe option here # because it contains globals and locals in the right precedence. # See: https://stackoverflow.com/questions/15866398/why-is-a-python-generator-confusing-its-scope-with-global-in-an-execd-script. Exec(code, updated_globals, updated_globals) except SystemExit: raise except: interpreter.showtraceback() else: update_frame_local_variables_and_save(frame, updated_globals) return False
def eventFilter(self, obj: Qt.QObject, event: Qt.QEvent): if obj is not self.ui.input_box: return super().eventFilter(obj, event) if event.type() != Qt.QEvent.KeyPress: return super().eventFilter(obj, event) if (event.key() == Qt.Qt.Key_Up and event.modifiers() & Qt.Qt.ControlModifier == Qt.Qt.ControlModifier): self._restore_from_history(self._history_index - 1) return True if (event.key() == Qt.Qt.Key_Down and event.modifiers() & Qt.Qt.ControlModifier == Qt.Qt.ControlModifier): self._restore_from_history(self._history_index + 1) return True if ((event.key() == Qt.Qt.Key_Enter or event.key() == Qt.Qt.Key_Return) and event.modifiers() & Qt.Qt.ShiftModifier != Qt.Qt.ShiftModifier): try: result = code.compile_command(self.ui.input_box.toPlainText(), "<python console>", "single") except SyntaxError: result = True if result: # force execution self._execute_triggered() return True return super().eventFilter(obj, event)
def handle(self): if self.client_address not in self.server.authenticated and not self.tryLogin(): return import code, cStringIO, traceback buf = [] while True: data = self.request.recv(1024).rstrip() if data == "\x04" or data == "exit" or data == "quit": # \x04 == ^D return buf.append(data) orig_displayhook, orig_stdout = sys.displayhook, sys.stdout sio = cStringIO.StringIO() continuation = False # print "buffer:",buf try: comp = code.compile_command("\n".join(buf)) if comp: sys.displayhook = self.displayhook sys.stdout = sio exec comp self.request.send(sio.getvalue()) buf = [] else: self.request.send("... ") continuation = True except: self.request.send(traceback.format_exc()) buf = [] finally: sys.displayhook, sys.stdout = orig_displayhook, orig_stdout if not continuation: self.request.send("\n>>> ")
def _probeTracebackSkip(self): try: eval(code.compile_command('raise Exception\n')) except Exception: _, _, eTb = sys.exc_info() self.__skipTb = len(traceback.extract_tb(eTb)) - 1
def debug_cmd(self, protocol, caller, source, command, raw_args, parsed_args): """ Command handler for the debug command """ self.source = source self.caller = caller self.protocol = protocol try: c_obj = code.compile_command(raw_args) except SyntaxError as e: self.output(__("Syntax error: %s") % e.text) return except (OverflowError, ValueError) as e: self.output(__("Invalid literal: %s") % e.msg) return except Exception as e: self.output("%s" % e) return try: self.interpreter.runcode(c_obj) except Exception as e: self.output("%s" % e) return
def runner(line): line = line + "\n" global_vars["current_bounce"] = global_vars["current_bounce"] + line console = global_vars["interactive_python"] ui_out = global_vars["ui_out"] retval = True if ((count_ws(global_vars["current_bounce"]) >= count_ws(line)) \ & (not line.endswith("\\\n"))): try: co = code.compile_command(global_vars["current_bounce"],"<input>","exec") except: co = 1 retval = False else: co = 0 if co: sys.stdout = console sys.stderr = console console.runsource(global_vars["current_bounce"]) sys.stdout = sout sys.stderr = serr if not global_vars["current_bounce"].strip(): retval = True global_vars["current_bounce"] = "" return retval else: return True
def connect(self, sid=None): if sid == None: r = self._request('POST', 'sessions/py') if self._check(r, 201, 'POST new session failed'): sid = int(r.read()) else: return False r = self._request('GET', 'sessions/py/%d' % sid) if self._check(r, 200, 'GET session failed'): while True: buf = raw_input('>>> ') try: while not code.compile_command(buf): buf = buf + '\n' + raw_input('... ') if "exit()" == buf: self.close() break r = self._request('PUT', 'sessions/py/%d' % sid, buf) self._check(r, 200, 'PUT statement failed') result = r.read() if result and len(result.strip()) > 0: print result, if not result[-1] == '\n': print except SyntaxError, e: print e
def run(self): print "Welcome to Splunk SDK's Python interactive shell" print "%s connected to %s:%s" % ( self.service.username, self.service.host, self.service.port) while True: try: input = raw_input("> ") except EOFError: print "\n\nThanks for using Splunk>.\n" return if input is None: return if len(input) == 0: continue # Ignore try: # Gather up lines until we have a fragment that compiles while True: co = compile_command(input) if co is not None: break input = input + '\n' + raw_input(". ") # Keep trying except SyntaxError: self.showsyntaxerror() continue except Exception, e: print "Error: %s" % e continue self.runcode(co)
def interact(self, verbose=False, debug=False): try: while True: line = self.raw_input('$ ') if len(line.strip()) > 0: code_ast, code_args = self.parser.parse(line, debug=debug) if code_ast is None: print "%s" % code_args else: code_py = "(%s)" % self.parser.codegen(code_ast) if code_args[0] == 'args' and code_args[1] is not None: code_py += "%s" % (code_args[1],) try: code_co = code.compile_command(code_py) except: code_co = None if code_co is None: print "Code (%s) did not compile" % (code_py) else: if verbose: print code_ast self.dump_ast(code_ast) print code_py try: exec code_co except: e, v, s = sys.exc_info() print "Runtime error (%s)" % v else: print "%s" % code_py if verbose: self.dump_ast(code_ast) print except EOFError: pass
def pushcode(self, codeobj, filename, lineCnt): # so.write("PYTHON CODE\n") lines = codeobj.split("\n") # for line in lines: # so.write("line\n") # so.write(line) # so.write("\n") # get the indentation of the first line ws = re.compile("^(\s*)(.*)") match = ws.match(lines[0]) ws = match.group(1) nws = match.group(2) if (len(nws) == 0): wslen = 0 else: wslen = len(ws) codeobj = "" # so.write("NUMINDENTATION is " + `wslen` + '\n') for line in lines: # so.write("indented line\n") # so.write(line[wslen:]) # so.write("\n"); line = line[wslen:] codeobj += line + "\n" # codeobj = "".join(lines) try: c = code.compile_command(codeobj, '<string>', 'exec') self.runcode(c) except Exception, err: raise type( err), str(err) + " in code segment starting on line " + str( lineCnt) + ' in pyv file ' + filename
def prase_text_complete(code): if "\n" in code: try: return compile_command(code, "<input>", "exec") is not None except Exception: return True else: if len(code.strip()) == 0: return True elif code[0] == "?" or code[-1] == "?": return True else: try: return compile_command(code, "<input>", "single") is not None except Exception: return True
def run(self): json_opts = {'namedtuple_as_object': False} while True: data = self.input_stream.recv_multipart() code_id = data[0].decode('ascii') code_text = data[1].decode('utf8') self.user_module.__builtins__._sorna_emit = self.emit if self.input_supported: self.user_module.__builtins__.input = self.handle_input getpass.getpass = partial(self.handle_input, password=True) try: code_obj = code.compile_command(code_text, symbol='exec') except (OverflowError, IndentationError, SyntaxError, ValueError, TypeError, MemoryError) as e: exc_type, exc_val, tb = sys.exc_info() user_tb = type(self).strip_traceback(tb) err_str = ''.join(traceback.format_exception(exc_type, exc_val, user_tb)) hdr_str = 'Traceback (most recent call last):\n' if not err_str.startswith('Traceback ') else '' self.emit(ConsoleRecord('stderr', hdr_str + err_str)) self.emit(ControlRecord('finished')) else: sys.stdout, orig_stdout = self.stdout_emitter, sys.stdout sys.stderr, orig_stderr = self.stderr_emitter, sys.stderr try: exec(code_obj, self.user_ns) except Exception as e: # strip the first frame exc_type, exc_val, tb = sys.exc_info() user_tb = type(self).strip_traceback(tb) traceback.print_exception(exc_type, exc_val, user_tb) finally: self.emit(ControlRecord('finished')) sys.stdout = orig_stdout sys.stderr = orig_stderr
def compileReverseSourceCode(self): errorMessage = None try: compiledCode = code.compile_command(self.sourceCodeReverse, "string", "exec") except SyntaxError: errorMessage = traceback.format_exc().rstrip() return errorMessage