#28 Error sending unicode strings to spawned process

v1.0 (example)
closed-fixed
nobody
None
5
2014-03-07
2009-09-05
Mathew Oakes
No

I need to pass unicode strings. Pexpect (2.3, ubuntu package) fails thus:

>>> spokentext = u'Nation . Search the FOX Nation . czars \xb7 Health care \xb7 town halls \xb7 tea parties ...'
>>> p = pexpect.spawn('festival')
>>> p.expect('festival> ')
0
>>> p.sendline( spokentext )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/dist-packages/pexpect.py", line 961, in sendline
n = self.send(s)
File "/usr/lib/python2.6/dist-packages/pexpect.py", line 953, in send
c = os.write(self.child_fd, s)

Suggested fix from python mailing list works:
changing line 953 in the pexpect file to:

c = os.write(self.child_fd, s.encode("utf-8"))

Discussion

  • Nilo Menezes
    Nilo Menezes
    2009-11-08

    The fix solves the problem, but remember to pass a binary file ("wb") to logfile_read, if you plan to receive UTF-8 chars.

     
  • Thomas Kluyver
    Thomas Kluyver
    2014-03-07

    Pexpect 3 handles unicode properly.

     
  • Thomas Kluyver
    Thomas Kluyver
    2014-03-07

    • status: open --> closed-fixed
    • Group: --> v1.0 (example)