pexpect / pexpect

A Python module for controlling interactive programs in a pseudo-terminal
http://pexpect.readthedocs.io/
Other
2.63k stars 478 forks source link

'str' does not support the buffer interface #408

Open sangmeshcp opened 7 years ago

sangmeshcp commented 7 years ago

Facing the following error:

File "/lib/python3.4/site-packages/pexpect/spawnbase.py", line 321, in expect timeout, searchwindowsize, async) File "/lib/python3.4/site-packages/pexpect/spawnbase.py", line 345, in expect_list return exp.expect_loop(timeout) File "/lib/python3.4/site-packages/pexpect/expect.py", line 99, in expect_loop incoming = spawn.read_nonblocking(spawn.maxread, timeout) File "/lib/python3.4/site-packages/pexpect/pty_spawn.py", line 465, in read_nonblocking return super(spawn, self).read_nonblocking(size) File "/auto/cafy_dev/cafykit/exec/lib/python3.4/site-packages/pexpect/spawnbase.py", line 163, in read_nonblocking self._log(s, 'read') File "/lib/python3.4/site-packages/pexpect/spawnbase.py", line 121, in _log self.logfile.write(s) TypeError: 'str' does not support the buffer interface

Code: fout = open('output.log','wb') pexpect.spawn(connect_str,encoding='utf-8',logfile=fout)

takluyver commented 7 years ago

When you pass an encoding to spawn, it operates in unicode mode, so the API works with str rather than bytes. That means you need to open your log file in text mode to correspond with it:

fout = open('output.log', 'w', encoding='utf-8')
pexpect.spawn(connect_str,encoding='utf-8',logfile=fout)
sangmeshcp commented 7 years ago

If I do that .. then i face this problem

File "/test.py", line 144, in _i_need_to_get_this_working pexpect.TIMEOUT]) File "/lib/python3.4/site-packages/pexpect/spawnbase.py", line 321, in expect timeout, searchwindowsize, async) File "/lib/python3.4/site-packages/pexpect/spawnbase.py", line 345, in expect_list return exp.expect_loop(timeout) File "/lib/python3.4/site-packages/pexpect/expect.py", line 99, in expect_loop incoming = spawn.read_nonblocking(spawn.maxread, timeout) File "/lib/python3.4/site-packages/pexpect/pty_spawn.py", line 465, in read_nonblocking return super(spawn, self).read_nonblocking(size) File "/lib/python3.4/site-packages/pexpect/spawnbase.py", line 162, in read_nonblocking s = self._decoder.decode(s, final=False) File "/lib/python3.4/codecs.py", line 319, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

takluyver commented 7 years ago

That means that the data coming back from the subprocess is not valid UTF-8. You can either:

  1. Change the subprocess (or its environment) to produce UTF-8 output, if you can control that.
  2. Specify the encoding for spawn() to match what the subprocess will use for output.
  3. Don't specify an encoding at all, and read bytes from the subprocess rather than unicode.
sangmeshcp commented 7 years ago

@takluyver
1) I dont have control 2) encoding is not fixed .. different devices have different encodings 3) if i dont specify encoding then im stuck with this error def _log(self, s, direction): if self.logfile is not None:

      self.logfile.write(s)

E TypeError: must be str, not bytes as i use python 3.4

sangmeshcp commented 7 years ago
def get(self, timeout=None):
    self.wait(timeout)
    if not self.ready():
        raise TimeoutError
    if self._success:
        return self._value
    else:
      raise self._value

E UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte