From 09773b39f4b791ea89e17711f214ac8f98d34269 Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Wed, 9 Sep 2015 12:22:39 +0100 Subject: Restore send/write methods to fdpexpect Closes gh-174 I'm adding these back, but I don't want to document them or encourage their use - if you're working with an fd, I think it's clearer to use os.write() directly rather than Pexpect's wrappers. I haven't added sendintr, sendeof or sendcontrol, because I don't think they really make sense for a general fd, and you can easily do the equivalent things explicitly if you need to. --- pexpect/fdpexpect.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'pexpect/fdpexpect.py') diff --git a/pexpect/fdpexpect.py b/pexpect/fdpexpect.py index 96ca2e1..2b51e24 100644 --- a/pexpect/fdpexpect.py +++ b/pexpect/fdpexpect.py @@ -84,3 +84,27 @@ class fdspawn(SpawnBase): def terminate (self, force=False): # pragma: no cover raise ExceptionPexpect('This method is not valid for file descriptors.') + + # These four methods are left around for backwards compatibility, but not + # documented as part of fdpexpect. You're encouraged to use os.write# + # directly. + def send(self, s): + "Write to fd, return number of bytes written" + s = self._coerce_send_string(s) + self._log(s, 'send') + + return os.write(self.child_fd, s) + + def sendline(self, s): + "Write to fd with trailing newline, return number of bytes written" + s = self._coerce_send_string(s) + return self.send(s + self.linesep) + + def write(self, s): + "Write to fd, return None" + self.send(s) + + def writelines(self, sequence): + "Call self.write() for each item in sequence" + for s in sequence: + self.write(s) -- cgit v1.2.1 From b485f7e2bd673f85950bc15b37908884be806fcb Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Sat, 12 Sep 2015 11:43:46 +0100 Subject: Add encoding parameter for fdspawn Closes gh-92 --- pexpect/fdpexpect.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'pexpect/fdpexpect.py') diff --git a/pexpect/fdpexpect.py b/pexpect/fdpexpect.py index 2b51e24..ca8cf07 100644 --- a/pexpect/fdpexpect.py +++ b/pexpect/fdpexpect.py @@ -32,7 +32,8 @@ class fdspawn(SpawnBase): descriptor. For example, you could use it to read through a file looking for patterns, or to control a modem or serial device. ''' - def __init__ (self, fd, args=None, timeout=30, maxread=2000, searchwindowsize=None, logfile=None): + def __init__ (self, fd, args=None, timeout=30, maxread=2000, searchwindowsize=None, + logfile=None, encoding=None, codec_errors='strict'): '''This takes a file descriptor (an int) or an object that support the fileno() method (returning an int). All Python file-like objects support fileno(). ''' @@ -50,7 +51,8 @@ class fdspawn(SpawnBase): self.args = None self.command = None - SpawnBase.__init__(self, timeout, maxread, searchwindowsize, logfile) + SpawnBase.__init__(self, timeout, maxread, searchwindowsize, logfile, + encoding=encoding, codec_errors=codec_errors) self.child_fd = fd self.own_fd = False self.closed = False @@ -93,7 +95,8 @@ class fdspawn(SpawnBase): s = self._coerce_send_string(s) self._log(s, 'send') - return os.write(self.child_fd, s) + b = self._encoder.encode(s, final=False) + return os.write(self.child_fd, b) def sendline(self, s): "Write to fd with trailing newline, return number of bytes written" -- cgit v1.2.1