Pexpect and buffering

Discussion in 'Python' started by jim.hefferon@gmail.com, Dec 15, 2012.

  1. Guest

    Hello,

    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.
    Jim

    The program:
    --------------------------------------------------
    #!/usr/bin/python
    import sys, os, re, pprint
    import pexpect

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

    child = pexpect.spawn('python') # start the repl
    # child = pexpect.spawn('python', maxread=1) # makes no difference
    child.expect([PROMPT])
    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)
    child.sendline(cmd)
    dex = child.expect([PROMPT, PROMPT_CONTINUE])
    print " child.before=",pprint.pformat(child.before)
    print " child.after=",pprint.pformat(child.after)
    r.append(child.before)
    print "r=",pprint.pformat(r)
    -----------------------------------------

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

    $ ./minex.py
    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']
    , Dec 15, 2012
    #1
    1. Advertising

  2. Chris Rebert Guest

    On Dec 15, 2012 4:51 AM, <> wrote:
    >
    > Hello,
    >
    > 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.

    Why are you doing this in the first place? Why invoke an external Python
    shell when you're in a Python program to begin with? Seems terribly
    unnecessarily roundabout.
    Chris Rebert, Dec 16, 2012
    #2
    1. Advertising

  3. Guest

    Sorry to reply to my own post, but I believe I have my answer and I want to help anyone who might google their way here: I need to change PROMPT and PROMPT_CONTINUE to be regular expressions, for instance by escaping the periods.
    , Dec 16, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-15?Q?J=F6rg?= Maier

    cygwin python with Tkinter and pexpect module

    =?ISO-8859-15?Q?J=F6rg?= Maier, Sep 6, 2003, in forum: Python
    Replies:
    2
    Views:
    530
    =?ISO-8859-15?Q?J=F6rg?= Maier
    Sep 7, 2003
  2. Adrian Casey

    pexpect and OpenVMS

    Adrian Casey, May 20, 2004, in forum: Python
    Replies:
    2
    Views:
    393
    Cameron Laird
    May 21, 2004
  3. Gil_H
    Replies:
    2
    Views:
    552
    Gil_H
    Apr 4, 2007
  4. Geoff Bache
    Replies:
    4
    Views:
    537
  5. Saqib Ali
    Replies:
    1
    Views:
    353
    Roy Smith
    Dec 26, 2011
Loading...

Share This Page