rjmcguire / python-on-a-chip

Automatically exported from code.google.com/p/python-on-a-chip
Other
0 stars 0 forks source link

Seperate the guts of ipm's onecmd() and it's while loop #202

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
onecmd() requires a while loop but we can peel out all the guts of the function 
so ipm.Interactive can be used by programs designed to work with Python's 
code.InteractiveInterpreter as well as cmd.Cmd.

Here is my version of the onecmd() function split into two functions -- the 
second being runsource() which is compatible with 
code.InteractiveInterpreter.runsource().

    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

        # If the line was incomplete, return with True to indicate more lines needed
        if not codeobj:
            return True

        # We have a a complete line of code -- execute it

        # DEBUG: Uncomment the next line to print the statement's bytecodes
        #dis.disco(codeobj)

        # Convert to a code image
        try:
            codeimg = self.pic.co_to_str(codeobj)

        # Print any conversion errors
        except Exception, e:
            self.stdout.write("%s:%s\n" % (e.__class__.__name__, e))

        # Otherwise send the image and print the reply
        else:

            # DEBUG: Uncomment the next line to print the size of the code image
            # print "DEBUG: len(codeimg) = ", len(codeimg)
            # DEBUG: Uncomment the next line to print the code image
            # print "DEBUG: codeimg = ", repr(codeimg)

            try:
                self.conn.write(codeimg)
            except Exception, e:
                self.stdout.write(
                    "Connection write error, type Ctrl+%s to quit.\n" % EOF_KEY)

            try:
                for c in self.conn.read():
                    self.stdout.write(c)
            except Exception, e:
                self.stdout.write(
                    "Connection read error, type Ctrl+%s to quit.\n" % EOF_KEY)

        return False

    def onecmd(self, line):
        """Gathers one interactive line of input (gets more lines as needed).
        """

        isIncomplete = self.runsource(line)

        # If the line was incomplete, get more input and try again
        if isIncomplete:
            while isIncomplete:
                self.stdout.write(IPM_PROMPT2)
                line += self.stdin.readline()
                isIncomplete = self.runsource(line)

        return self.stop

Original issue reported on code.google.com by j...@houseoftechnology.org on 6 May 2011 at 4:59

GoogleCodeExporter commented 9 years ago
JT, I like this suggestion that it makes the guts useful to two different 
callers.  Can you tell me the advantage(s) of using 
code.InteractiveInterpreter.runsource() and what compelled you to make this 
change?

Original comment by dwhall...@gmail.com on 9 May 2011 at 7:47

GoogleCodeExporter commented 9 years ago
This change was the least invasive way that I could figure out how to have ipm 
running under a wxPython GUI.  They include a python shell program that uses 
InteractiveInterpreter and I kludged your cmd based interpreter onto an 
InteractiveInterpreter instance by replacing the runsource function.

Original comment by j...@missioncognition.net on 9 May 2011 at 10:06

GoogleCodeExporter commented 9 years ago
This issue was closed by revision a6b05e377226.

Original comment by dwhall...@gmail.com on 15 May 2011 at 8:09

GoogleCodeExporter commented 9 years ago

Original comment by dwhall...@gmail.com on 15 May 2011 at 8:16

GoogleCodeExporter commented 9 years ago
These changes are the cause of issue 206

Original comment by dwhall...@gmail.com on 26 Jun 2011 at 3:41