Pexpect and buffering

jim.hefferon at jim.hefferon at
Sat Dec 15 13:48:53 CET 2012


I'm trying to use pexpect to grab interactions with Python's REPL.  I am having trouble with tracebacks.  Possibly it is related to buffering (hence the subject line) but I admit that's a guess.

At the end of this message is a minimal example program.  It feeds three commands to a python interpreter.  The second command should fail like this.

    >>> a
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'a' is not defined

However, pexpect only returns part of that "Traceback .." message, that is, pexpect is not waiting for the >>> prompt.  I have included the output of the program below, after the program.  

(In case it helps, spawning Python with -u doesn't make any difference.)

I'd be very glad for any suggestions.

The program:
import sys, os, re, pprint
import pexpect

cmds = ['1+2', 'a', '3+4']
PROMPT = '>>> '

child = pexpect.spawn('python')  # start the repl
# child = pexpect.spawn('python', maxread=1)  # makes no difference
print "  initial child.before=",pprint.pformat(child.before)
print "  initial child.after=",pprint.pformat(child.after)

r = [] 
for cmd in cmds:
    print "++++++ cmd=",pprint.pformat(cmd)
    dex = child.expect([PROMPT, PROMPT_CONTINUE])
    print "  child.before=",pprint.pformat(child.before)
    print "  child.after=",pprint.pformat(child.after)
print "r=",pprint.pformat(r)

My screen when I run this (Ubuntu 12.04 with Python 2.7.3).

$ ./
  initial child.before= 'Python 2.7.3 (default, Aug  1 2012, 05:16:07) \r\n[GCC 4.6.3] on linux2\r\nType "help", "copyright", "credits" or "license" for more information.\r\n'
  initial child.after= '>>> '
++++++ cmd= '1+2'
  child.before= '1+2\r\n3\r\n'
  child.after= '>>> '
++++++ cmd= 'a'
  child.before= 'a\r\nTraceb'
  child.after= 'ack '
++++++ cmd= '3+4'
  child.before= '(m'
  child.after= 'ost '
r= ['1+2\r\n3\r\n', 'a\r\nTraceb', '(m']

More information about the Python-list mailing list