from code import InteractiveConsole code = ''' x = 5 y = 10 print(x + y) ''' console = InteractiveConsole() console.runcode(code)
def do_py(self, arg): """ :: Usage: py py COMMAND Arguments: COMMAND the command to be executed Description: The command without a parameter will be executed and the interactive python mode is entered. The python mode can be ended with ``Ctrl-D`` (Unix) / ``Ctrl-Z`` (Windows), ``quit()``,'`exit()``. Non-python commands can be issued with ``cmd("your command")``. If the python code is located in an external file it can be run with ``run("filename.py")``. In case a COMMAND is provided it will be executed and the python interpreter will return to the command shell. This code is copied from Cmd2. """ self.pystate['self'] = self arg = arg.strip() localvars = (self.locals_in_py and self.pystate) or {} interp = InteractiveConsole(locals=localvars) interp.runcode('import sys, os;sys.path.insert(0, os.getcwd())') if arg: interp.runcode(arg) else: def quit(): raise EmbeddedConsoleExit def onecmd(arg): return self.onecmd(arg + '\n') def run(arg): try: f = open(arg) interp.runcode(f.read()) f.close() except IOError, e: self.perror(e) self.pystate['quit'] = quit self.pystate['exit'] = quit self.pystate['cmd'] = onecmd self.pystate['run'] = run try: cprt = 'Type "help", "copyright", "credits" or "license" for more information.' keepstate = Statekeeper(sys, ('stdin', 'stdout')) sys.stdout = self.stdout sys.stdin = self.stdin interp.interact(banner="Python %s on %s\n%s\n(%s)\n%s" % (sys.version, sys.platform, cprt, self.__class__.__name__, self.do_py.__doc__)) except EmbeddedConsoleExit: pass keepstate.restore()
def do_py(self, arg): """ :: Usage: py py COMMAND Arguments: COMMAND the command to be executed Description: The command without a parameter will be executed and the interactive python mode is entered. The python mode can be ended with ``Ctrl-D`` (Unix) / ``Ctrl-Z`` (Windows), ``quit()``,'`exit()``. Non-python commands can be issued with ``cmd("your command")``. If the python code is located in an external file it can be run with ``run("filename.py")``. In case a COMMAND is provided it will be executed and the python interpreter will return to the command shell. This code is copied from Cmd2. """ self.pystate['self'] = self arg = arg.strip() localvars = (self.locals_in_py and self.pystate) or {} interp = InteractiveConsole(locals=localvars) interp.runcode('import sys, os;sys.path.insert(0, os.getcwd())') if arg: interp.runcode(arg) else: def quit(): raise EmbeddedConsoleExit def onecmd(arg): return self.onecmd(arg + '\n') def run(arg): try: f = open(arg) interp.runcode(f.read()) f.close() except IOError as e: self.perror(e) self.pystate['quit'] = quit self.pystate['exit'] = quit self.pystate['cmd'] = onecmd self.pystate['run'] = run try: cprt = 'Type "help", "copyright", "credits" or "license" for more information.' keepstate = Statekeeper(sys, ('stdin', 'stdout')) sys.stdout = self.stdout sys.stdin = self.stdin interp.interact(banner="Python %s on %s\n%s\n(%s)\n%s" % (sys.version, sys.platform, cprt, self.__class__.__name__, self.do_py.__doc__)) except EmbeddedConsoleExit: pass keepstate.restore()
def runcode(self, codez): out = StringIO() sys.stdout = out sys.stderr = out InteractiveConsole.runcode(self, codez) sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ return out.getvalue()
def runcode(self, code): sys.stdout = StringIO() InteractiveConsole.runcode(self, code) output = sys.stdout.getvalue() sys.stdout = sys.__stdout__ if len(output) > 0: getch(' ') self.write(output)
def main(): if len(sys.argv) > 1: f = sys.argv[1] code = open(f).read() lazy_exec(code) else: from code import InteractiveConsole console = InteractiveConsole(LazyEnv()) console.runcode("import readline") console.interact()
def runcode(self, code): fake_stdout = StringIO() sys.stdout = fake_stdout sys.stderr = fake_stdout try: InteractiveConsole.runcode(self, code) finally: sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ self.write(fake_stdout.getvalue())
def runcode(self, code): sys.stdout, sys.stderr = StringIO(), StringIO() InteractiveConsole.runcode(self, code) output, errors = sys.stdout.getvalue(), sys.stderr.getvalue() sys.stdout, sys.stderr = sys.__stdout__, sys.__stderr__ if output or errors: self.wait_for_user_input() if output: self.write(output) if errors: self.write(errors)
def runcode(self, code): # preserve stdout/stderr oldstdout = sys.stdout oldstderr = sys.stderr sys.stdout = self.handle sys.stderr = self.handle InteractiveConsole.runcode(self, code) sys.stdout = oldstdout sys.stderr = oldstderr
def do_py(self, line): ''' py <command>: Executes a Python command. py: Enters interactive Python mode. End with ``Ctrl-D`` (Unix) / ``Ctrl-Z`` (Windows), ``quit()``, '`exit()``. Non-python commands can be issued with ``cmd("your command")``. Run python code from external files with ``run("filename.py")`` ''' self.pystate['self'] = self line = self.rebuildline(line) arg = line.strip() localvars = (self.locals_in_py and self.pystate) or {} from code import InteractiveConsole, InteractiveInterpreter interp = InteractiveConsole(locals=localvars) interp.runcode('import sys, os;sys.path.insert(0, os.getcwd())') if arg.strip(): interp.runcode(arg) else: def quit(): raise EmbeddedConsoleExit def onecmd_plus_hooks(arg): return self.onecmd_plus_hooks(arg + '\n') def run(arg): try: file = open(arg) interp.runcode(file.read()) file.close() except IOError as e: self.perror(e) self.pystate['quit'] = quit self.pystate['exit'] = quit self.pystate['cmd'] = onecmd_plus_hooks self.pystate['run'] = run try: cprt = 'Type "help", "copyright", "credits" or "license" for more information.' keepstate = Statekeeper(sys, ('stdin', 'stdout')) sys.stdout = self.stdout sys.stdin = self.stdin interp.interact(banner="Python %s on %s\n%s\n(%s)\n%s" % (sys.version, sys.platform, cprt, self.__class__.__name__, self.do_py.__doc__)) except EmbeddedConsoleExit: pass keepstate.restore()
def runcode(self, code): stdout = sys.stdout sys.stdout = self._dummyStdOut try: return InteractiveConsole.runcode(self, code) finally: sys.stdout = stdout
def runcode(self, code): sys.stdout = self.stdout sys.stderr = self.stderr sys.excepthook = sys.__excepthook__ result = InteractiveConsole.runcode(self, code) sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ return result
def main(): x = 5 print('Entering an interactive Python shell ...') interp = InteractiveConsole(locals=locals()) interp.runcode('import sys, os;sys.path.insert(0, os.getcwd())') interp.runcode('x = 7') print('x = {}'.format(x)) cprt = 'Type "help", "copyright", "credits" or "license" for more information.' try: interp.interact(banner="Python %s on %s\n%s\n" % (sys.version, sys.platform, cprt)) except SystemExit: print("Exited InteractiveConsole") print('x = {}. Leaving main program.'.format(x))
def python(self): from code import interact, InteractiveConsole try: import readline import rlcompleter readline.set_completer(rlcompleter.Completer(self.ns).complete) readline.parse_and_bind('tab:complete') except: pass console = InteractiveConsole(locals=self.ns) if self.cmd: console.runcode(self.cmd) elif self.script: console.runcode(open(self.script).read()) if self.interact: console.interact(banner='') else: console.interact(banner=self.banner)
def runcode(self, code): """ Overrides and captures stdout and stdin from InteractiveConsole. """ sys.stdout = self.stream sys.stderr = self.stream sys.excepthook = sys.__excepthook__ result = InteractiveConsole.runcode(self, code) sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ return result
def runcode(self, code): """%s""" % InteractiveConsole.runcode.__doc__ # we need to fix-up that method as we are given a C-ext module # in case of cython (instead of a code-object) import types if isinstance(code, types.ModuleType): # slam the content of the module into our local namespace for k,v in code.__dict__.iteritems(): if not k.startswith('__'): self.locals[k] = v else: return InteractiveConsole.runcode(self, code)
def runcode(self, code): """%s""" % InteractiveConsole.runcode.__doc__ # we need to fix-up that method as we are given a C-ext module # in case of cython (instead of a code-object) import types if isinstance(code, types.ModuleType): # slam the content of the module into our local namespace for k, v in code.__dict__.iteritems(): if not k.startswith('__'): self.locals[k] = v else: return InteractiveConsole.runcode(self, code)
def runcode(self, code): stime=time.time() try: return InteractiveConsole.runcode(self, code) finally: self.needed = time.time()-stime
def runcode(self, code): stime = time.time() try: return InteractiveConsole.runcode(self, code) finally: self.needed = time.time() - stime
def runcode(self, code): with self.game._lock: InteractiveConsole.runcode(self, code)