summaryrefslogtreecommitdiff
path: root/cmd2.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2017-08-07 23:22:11 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2017-08-07 23:22:11 -0400
commit3a3c867c0715e03e30cc30bea7b08604af3aacdc (patch)
treed5c6a69fd6c826d20803abb10f323087b852ca70 /cmd2.py
parentde9cda230d3623f7c5c73e4b1a3827e906160208 (diff)
downloadcmd2-git-3a3c867c0715e03e30cc30bea7b08604af3aacdc.tar.gz
Changed how the pipe file descriptors get opened on Python 3 vs 2
Open them in text mode in Python 3 so self.stdout.write() expects normal Python 3 (unicode) strings. Open them in binary mode in Python 2 so self.stdout.write() expects normal Python 2 (byte) strings. Also fixed a unit test mocking issue on Python 2 to account for the fact that Python 2.7 requires the subprocess32 module instead of subprocess.
Diffstat (limited to 'cmd2.py')
-rwxr-xr-xcmd2.py11
1 files changed, 9 insertions, 2 deletions
diff --git a/cmd2.py b/cmd2.py
index 7e9481db..9a1cb4e8 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -776,11 +776,18 @@ class Cmd(cmd.Cmd):
# Create a pipe with read and write sides
read_fd, write_fd = os.pipe()
+ # Make sure that self.stdout.write() expects unicode strings in Python 3 and byte strings in Python 2
+ write_mode = 'w'
+ read_mode = 'r'
+ if six.PY2:
+ write_mode = 'wb'
+ read_mode = 'rb'
+
# Open each side of the pipe and set stdout accordingly
# noinspection PyTypeChecker
- self.stdout = io.open(write_fd, 'w')
+ self.stdout = io.open(write_fd, write_mode)
# noinspection PyTypeChecker
- subproc_stdin = io.open(read_fd, 'r')
+ subproc_stdin = io.open(read_fd, read_mode)
# If you don't set shell=True, subprocess failure will throw an exception
try: