summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjquast <contact@jeffquast.com>2014-06-08 17:02:59 -0700
committerjquast <contact@jeffquast.com>2014-06-08 17:02:59 -0700
commit8aea7ddba7b8d9b1698f17a1895dcaf6730f79d9 (patch)
treeb0ff51652bab9f030537522bf63c36e273c7b21b
parent8b04d95b94ce59e9a2a8ad91585e91ef40ec1aa0 (diff)
downloadpexpect-8aea7ddba7b8d9b1698f17a1895dcaf6730f79d9.tar.gz
PR #66: TypeError thrown by spawnu.readline()
Submitted by @auntieNeo, fixes exception, "TypeError: got <type 'str'> ('\r\n') as pattern" in spawnu.readline(). Bytes b'\r\n' was concatenated to u'unicode', causing an exception to be thrown when using readline().
-rw-r--r--doc/history.rst2
-rw-r--r--pexpect/__init__.py8
-rwxr-xr-xtests/test_misc.py7
-rw-r--r--tests/test_unicode.py9
4 files changed, 24 insertions, 2 deletions
diff --git a/doc/history.rst b/doc/history.rst
index 81ad48b..245b24c 100644
--- a/doc/history.rst
+++ b/doc/history.rst
@@ -14,6 +14,8 @@ Version 3.3
* Removed the ``pexpect.psh`` module. This was never documented, and we found
no evidence that people use it. The new :mod:`pexpect.replwrap` module
provides a more flexible alternative.
+* Fixed ``TypeError: got <type 'str'> ('\r\n') as pattern`` in ``readline()``
+ method of ``spawnu`` (:ghissue:`67`).
Version 3.2
```````````
diff --git a/pexpect/__init__.py b/pexpect/__init__.py
index f27e3fe..dab85c0 100644
--- a/pexpect/__init__.py
+++ b/pexpect/__init__.py
@@ -284,6 +284,7 @@ class spawn(object):
def _chr(c):
return bytes([c])
linesep = os.linesep.encode('ascii')
+ crlf = '\r\n'.encode('ascii')
@staticmethod
def write_to_stdout(b):
@@ -296,6 +297,7 @@ class spawn(object):
allowed_string_types = (basestring,) # analysis:ignore
_chr = staticmethod(chr)
linesep = os.linesep
+ crlf = '\r\n'
write_to_stdout = sys.stdout.write
encoding = None
@@ -970,9 +972,9 @@ class spawn(object):
if size == 0:
return self.string_type()
# delimiter default is EOF
- index = self.expect([b'\r\n', self.delimiter])
+ index = self.expect([self.crlf, self.delimiter])
if index == 0:
- return self.before + b'\r\n'
+ return self.before + self.crlf
else:
return self.before
@@ -1730,11 +1732,13 @@ class spawnu(spawn):
allowed_string_types = (str, )
_chr = staticmethod(chr)
linesep = os.linesep
+ crlf = '\r\n'
else:
string_type = unicode
allowed_string_types = (unicode, )
_chr = staticmethod(unichr)
linesep = os.linesep.decode('ascii')
+ crlf = '\r\n'.decode('ascii')
# This can handle unicode in both Python 2 and 3
write_to_stdout = sys.stdout.write
diff --git a/tests/test_misc.py b/tests/test_misc.py
index 7569070..93ff930 100755
--- a/tests/test_misc.py
+++ b/tests/test_misc.py
@@ -49,6 +49,13 @@ class TestCaseMisc(PexpectTestCase.PexpectTestCase):
remaining = child.read().replace(_CAT_EOF, b'')
self.assertEqual(remaining, b'abc\r\n')
+ def test_readline_bin_echo(self):
+ # given,
+ child = pexpect.spawn('echo', ['input', ])
+
+ # exercise,
+ assert child.readline() == b'input' + child.crlf
+
def test_readline (self):
'''See the note in test_readlines() for an explaination as to why
I allow line3 and line4 to return multiple patterns.
diff --git a/tests/test_unicode.py b/tests/test_unicode.py
index 38e758d..256057d 100644
--- a/tests/test_unicode.py
+++ b/tests/test_unicode.py
@@ -134,6 +134,15 @@ class UnicodeTests(PexpectTestCase.PexpectTestCase):
p.sendeof()
p.expect('▁▂▃▄▅▆▇█')
+ def test_readline_bin_echo(self):
+ # Test using readline() with spawnu objects. pexpect 3.2 had threw
+ # a TypeError when concatenating a bytestring to a unicode type.
+
+ # given,
+ child = pexpect.spawnu('echo', ['input', ])
+
+ # exercise,
+ assert child.readline() == 'input' + child.crlf
if __name__ == '__main__':
unittest.main()