summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Kluyver <takowl@gmail.com>2014-01-07 15:08:07 -0800
committerThomas Kluyver <takowl@gmail.com>2014-01-07 15:08:07 -0800
commit693cb29ec3e5a55304720e73553b57d7c0c66f8f (patch)
treeb1e753d2fe77187d9bcc3132be01372302cb7719
parent0b99c3a5fd34c68a58ec4ea07f4957579133e6f7 (diff)
parentd4ca2467e50af52b450bd3c46a79fb5c4d8dd22f (diff)
downloadpexpect-693cb29ec3e5a55304720e73553b57d7c0c66f8f.tar.gz
Merge pull request #31 from takluyver/stdout-bytes-py3
Allow importing when sys.stdout is reassigned on Python 3
-rw-r--r--pexpect/__init__.py9
1 files changed, 8 insertions, 1 deletions
diff --git a/pexpect/__init__.py b/pexpect/__init__.py
index ea39397..18f4102 100644
--- a/pexpect/__init__.py
+++ b/pexpect/__init__.py
@@ -284,7 +284,14 @@ class spawn(object):
def _chr(c):
return bytes([c])
linesep = os.linesep.encode('ascii')
- write_to_stdout = sys.stdout.buffer.write
+
+ @staticmethod
+ def write_to_stdout(b):
+ try:
+ return sys.stdout.buffer.write(b)
+ except AttributeError:
+ # If stdout has been replaced, it may not have .buffer
+ return sys.stdout.write(b.decode('ascii', 'replace'))
else:
allowed_string_types = (basestring,) # analysis:ignore
_chr = staticmethod(chr)