Why is pexpect acting funny with sendline() and expect()?

Discussion in 'Python' started by saqib.ali.75@gmail.com, Dec 26, 2012.

  1. Guest

    I am running Solaris 5-10, python 2.6.2 and pexpect 2.4

    I have the very simple python script below which exercises the functionality of sending and receiving text from the shell.

    My understanding is that pexepect([pexpect.TIMEOUT, x,y,z], timeout=w) will return the index of the match that it found *since the last time pexpect was called*, but if it takes longer than w seconds, it will return 0.


    Here is my very simple script:


    #!/usr/bin/env python

    import pexpect
    myPrompt = " % "

    myShell = pexpect.spawn("/bin/tcsh")
    print "Sending 'JUNK-0' to shell"
    x = myShell.sendline("JUNK-0")
    y = myShell.expect([pexpect.TIMEOUT], timeout=1)
    print "y = %s" % y
    print myShell.before
    print "=" * 80
    print "\n\n"

    for i in range(2):
    print "i = %d" % (i+1)
    print "Sending 'JUNK-%d' to shell" % (i+1)
    x = myShell.sendline("JUNK-%d" % (i+1))
    y = myShell.expect([pexpect.TIMEOUT, myPrompt], timeout=10)
    print "y = %s" % y
    print myShell.before
    print "=" * 80
    print "\n\n"



    FYI, my shell prompt is "myMachine % ", however in this script I have simply used " % " to keep it generic.

    When I run it, I see the following output:




    Sending 'JUNK-0' to shell
    y = 0
    JUNK-0
    myMachine % JUNK-0
    JUNK-0: Command not found.
    myMachine %
    ================================================================================



    i = 1
    Sending 'JUNK-1' to shell
    y = 1
    JUNK-0
    myMachine
    ================================================================================



    i = 2
    Sending 'JUNK-2' to shell
    y = 1
    JUNK-0
    JUNK-0: Command not found.
    myMachine
    ================================================================================




    Why do I see "JUNK-0" consistently recurring in the output? It should be consumed by the first myShell.expect() statement, but it keeps showing up. Why??
    , Dec 26, 2012
    #1
    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. philoso
    Replies:
    5
    Views:
    463
    Sudsy
    Nov 21, 2003
  2. skye.shaw
    Replies:
    0
    Views:
    411
    skye.shaw
    Aug 11, 2005
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,850
    Smokey Grindel
    Dec 2, 2006
  4. Saqib Ali
    Replies:
    1
    Views:
    361
    Roy Smith
    Dec 26, 2011
  5. Daniel Fetchinson

    lambda in list comprehension acting funny

    Daniel Fetchinson, Jul 11, 2012, in forum: Python
    Replies:
    45
    Views:
    687
    Robert Miles
    Aug 16, 2012
Loading...

Share This Page