Esempio n. 1
0
    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()
Esempio n. 2
0
    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()
Esempio n. 3
0
 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, codez):
     out = StringIO()
     sys.stdout = out
     sys.stderr = out
     InteractiveConsole.runcode(self, codez)
     sys.stdout = sys.__stdout__
     sys.stderr = sys.__stderr__
     return out.getvalue()
Esempio n. 5
0
 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)
Esempio n. 6
0
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()
Esempio n. 7
0
 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())
Esempio n. 8
0
 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)
Esempio n. 9
0
    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
Esempio n. 10
0
    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
Esempio n. 11
0
 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)
Esempio n. 12
0
    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()
Esempio n. 13
0
 def runcode(self, code):
     stdout = sys.stdout
     sys.stdout = self._dummyStdOut
     try:
         return InteractiveConsole.runcode(self, code)
     finally:
         sys.stdout = stdout
Esempio n. 14
0
 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
Esempio n. 15
0
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))
Esempio n. 16
0
    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)
Esempio n. 17
0
 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
Esempio n. 18
0
 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)
Esempio n. 19
0
 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)
Esempio n. 20
0
File: py.py Progetto: rfk/bbfreeze
 def runcode(self, code):
     stime=time.time()
     try:
         return InteractiveConsole.runcode(self, code)
     finally:
         self.needed = time.time()-stime
Esempio n. 21
0
 def runcode(self, code):
     stime = time.time()
     try:
         return InteractiveConsole.runcode(self, code)
     finally:
         self.needed = time.time() - stime
Esempio n. 22
0
 def runcode(self, code):
     with self.game._lock:
         InteractiveConsole.runcode(self, code)