title: Python pexpect sendline contents being buffered?
body: <p>I have the following code in Python using the pexpect module to run a perl script which asks a series of questions. I have used this for quite some time and works well until now.</p>
  
  <h2>pexpect python</h2>
  
  <pre><code>index = child.expect(['Question 1:'])
  os.system('sleep 2')
  print 'Question 1:' + 'answer1'
  child.sendline('answer1')
  
  index = child.expect(['Question 2:'])
  os.system('sleep 2')
  print 'Question 1:' + 'answer2'
  child.sendline('answer2')
  
  index = child.expect(['Question 3:'])
  os.system('sleep 2')
  print 'Question 1:' + 'answer2'
  child.sendline('answer2')
  </code></pre>
  
  <p>At this point, i have some code that checks if error will output when Question 2 &amp; Question 3 does not match. I checked the pexpect log and the statements being sent are exactly what i want (number of bytes and string). </p>
  
  <p>However when I check what gets accepted by the perl code its getting the following:</p>
  
  <p>Question 1: 'answer 1'     &lt;-- CORRECT</p>
  
  <p>Question 2: 'answer 1'     &lt;-- INCORRECT</p>
  
  <p>Question 3: 'answer 2answer 2'     &lt;-- its being combined into one for some reason</p>
  
  <p>Any ideas out there? I'm not using anything special when spawning my pexpect object: <code>child=pexpect.spawn(cmd,timeout=140)</code></p>
  
  <p>Edit: Added the perl code function that asks for Question 2 &amp; 3</p>
  
  <h2><strong>sub getpw</strong></h2>
  
  <pre><code>
  sub getpw
  {   my ($name, $var, $encoding) = @_;
      my $pw = $$var;
  
      PWD: while(1) {
          system("/bin/stty -echo");
          getvar($name, \$pw, 1);
          print "\n";
          system("/bin/stty echo");
          return if $pw eq $$var && length($pw) == 80;
          if (length($pw) > 32) {
              print STDERR "ERROR: Password cannot exceed 32 characters, please reenter.\n";
              next PWD;
          }
  
          return if $pw eq $$var;
  
          my $pw2;
          system("/bin/stty -echo");
          getvar("Repeat password", \$pw2, 1);
          print "\n";
          system("/bin/stty echo");
          print "#1: ";
          print $pw;
          print "\n";
          print "#2: ";
          print $pw2;
          if ($pw2 ne $pw) {
              print STDERR "ERROR: Passwords do not match, please reenter.\n";
              next PWD;
          }
          last;
      }
  
      # Escape dangerous shell characters
      $pw =~ s/([ ;\*\|`&\$!#\(\)\[\]\{\}:'"])/\\$1/g;
  
      my $correctlength=80;
      my $encoded=`$AVTAR --quiet --encodepass=$pw`;
  
      chomp($encoded);
      if($? == 0 && length($encoded) == $correctlength) {
          $$var = $encoded;
      } else {
          print "Warning: Password could not be encoded.\n";
          $$var = $pw;
      }
  }
  </code></pre>
  
  <h2>sub getvar</h2>
  
  <pre><code>sub getvar
  {   my ($name, $var, $hide) = @_;
      my $default = $$var;
      while(1) {
          if($default) {
              $default = "*****" if $hide;
              print "$name [$default]: ";
          } else {
              print "$name: ";
          }
          my $val = &lt;STDIN&gt;;
          chomp $val;
          ### $val =~ s/ //g;  # do not mess with the password
          $$var = $val if $val;
          last if $$var;
          print "ERROR: You must enter a value\n";
      }
  }
  </code></pre>