Core pexpect components¶
spawn class¶
Controlling the child process¶
Handling unicode¶
By default, spawn
is a bytes interface: its read methods return bytes,
and its write/send and expect methods expect bytes. If you pass the encoding
parameter to the constructor, it will instead act as a unicode interface:
strings you send will be encoded using that encoding, and bytes received will
be decoded before returning them to you. In this mode, patterns for
expect()
and expect_exact()
should also be unicode.
Changed in version 4.0: spawn
provides both the bytes and unicode interfaces. In Pexpect
3.x, the unicode interface was provided by a separate spawnu
class.
For backwards compatibility, some Unicode is allowed in bytes mode: the send methods will encode arbitrary unicode as UTF-8 before sending it to the child process, and its expect methods can accept ascii-only unicode strings.
Note
Unicode handling with pexpect works the same way on Python 2 and 3, despite the difference in names. I.e.:
- Bytes mode works with
str
on Python 2, andbytes
on Python 3, - Unicode mode works with
unicode
on Python 2, andstr
on Python 3.