summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Kluyver <takowl@gmail.com>2013-09-25 17:51:33 -0700
committerThomas Kluyver <takowl@gmail.com>2013-09-25 17:51:33 -0700
commit9eb6df99667b23fb503cff5d454604ffefeb6cbe (patch)
treee24ee1f1eb42ad24351b51422f295c2764f095bd
parent4c19cc491fa2ad0ee1cad29078411e0b05a52e28 (diff)
downloadpexpect-git-9eb6df99667b23fb503cff5d454604ffefeb6cbe.tar.gz
Fix interact_unicode test
-rw-r--r--pexpect/__init__.py10
-rw-r--r--tests/interact_unicode.py2
-rwxr-xr-xtests/test_ctrl_chars.py3
-rwxr-xr-xtests/test_interact.py28
4 files changed, 29 insertions, 14 deletions
diff --git a/pexpect/__init__.py b/pexpect/__init__.py
index fb09ae0..8af06ff 100644
--- a/pexpect/__init__.py
+++ b/pexpect/__init__.py
@@ -284,10 +284,12 @@ class spawn(object):
def _chr(c):
return bytes([c])
linesep = os.linesep.encode('ascii')
+ write_to_stdout = sys.stdout.buffer.write
else:
allowed_string_types = (basestring,) # analysis:ignore
_chr = staticmethod(chr)
linesep = os.linesep
+ write_to_stdout = sys.stdout.write
encoding = None
@@ -1588,11 +1590,13 @@ class spawn(object):
'''
# Flush the buffer.
- self.stdout.write(self.buffer.decode('ascii'))
+ self.write_to_stdout(self.buffer)
self.stdout.flush()
self.buffer = self.string_type()
mode = tty.tcgetattr(self.STDIN_FILENO)
tty.setraw(self.STDIN_FILENO)
+ if PY3:
+ escape_character = escape_character.encode('latin-1')
try:
self.__interact_copy(escape_character, input_filter, output_filter)
finally:
@@ -1637,7 +1641,7 @@ class spawn(object):
data = self.__interact_read(self.STDIN_FILENO)
if input_filter:
data = input_filter(data)
- i = data.rfind(self._coerce_expect_string(escape_character))
+ i = data.rfind(escape_character)
if i != -1:
data = data[:i]
self.__interact_writen(self.child_fd, data)
@@ -1717,6 +1721,8 @@ class spawnu(spawn):
allowed_string_types = (unicode, )
_chr = staticmethod(unichr)
linesep = os.linesep.decode('ascii')
+ # This can handle unicode in both Python 2 and 3
+ write_to_stdout = sys.stdout.write
def __init__(self, *args, **kwargs):
self.encoding = kwargs.pop('encoding', 'utf-8')
diff --git a/tests/interact_unicode.py b/tests/interact_unicode.py
index 80276cb..2cd1bbb 100644
--- a/tests/interact_unicode.py
+++ b/tests/interact_unicode.py
@@ -5,7 +5,7 @@ Just like interact.py, but using spawnu instead of spawn
import pexpect
def main():
- p = pexpect.spawn('cat')
+ p = pexpect.spawnu('cat')
p.interact()
if __name__ == '__main__':
diff --git a/tests/test_ctrl_chars.py b/tests/test_ctrl_chars.py
index 8e55284..4d632f7 100755
--- a/tests/test_ctrl_chars.py
+++ b/tests/test_ctrl_chars.py
@@ -18,6 +18,8 @@ PEXPECT LICENSE
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
'''
+from __future__ import print_function
+
import pexpect
import unittest
import PexpectTestCase
@@ -76,6 +78,7 @@ class TestCtrlChars(PexpectTestCase.PexpectTestCase):
child = pexpect.spawn('python getch.py')
child.delaybeforesend = 0.05
for ctrl in 'abcdefghijklmnopqrstuvwxyz':
+ print(ctrl, end='')
assert child.sendcontrol(ctrl) == 1
# Strange: on travis-ci, getch.py actually displays ^A, not '1' !?
val = ord(ctrl) - (ord('a') - 1)
diff --git a/tests/test_interact.py b/tests/test_interact.py
index 5d071d5..3288b29 100755
--- a/tests/test_interact.py
+++ b/tests/test_interact.py
@@ -19,6 +19,8 @@ PEXPECT LICENSE
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
'''
+from __future__ import print_function
+
import pexpect
import unittest
import PexpectTestCase
@@ -41,17 +43,21 @@ class InteractTestCase (PexpectTestCase.PexpectTestCase):
def test_interact_unicode (self):
p = pexpect.spawnu('%s interact_unicode.py' % (self.PYTHONBIN,))
- p.sendline (u'Hello')
- p.sendline (u'theré')
- p.sendline (u'Mr. Pyþon')
- p.expect (u'Hello')
- p.expect (u'theré')
- p.expect (u'Mr. Pyþon')
- assert p.isalive()
- p.sendeof ()
- p.expect (pexpect.EOF)
- assert not p.isalive()
- assert p.exitstatus == 0, (p.exitstatus, p.before)
+ try:
+ p.sendline (u'Hello')
+ p.sendline (u'theré')
+ p.sendline (u'Mr. Pyþon')
+ p.expect (u'Hello')
+ p.expect (u'theré')
+ p.expect (u'Mr. Pyþon')
+ assert p.isalive()
+ p.sendeof ()
+ p.expect (pexpect.EOF)
+ assert not p.isalive()
+ assert p.exitstatus == 0, (p.exitstatus, p.before)
+ except:
+ print(p.before)
+ raise
if __name__ == '__main__':