##// END OF EJS Templates
Decode output from unix subprocesses using platform default encoding.
Thomas Kluyver -
Show More
@@ -19,17 +19,12 b' from __future__ import print_function'
19 import subprocess as sp
19 import subprocess as sp
20 import sys
20 import sys
21
21
22 # Third-party
22 from IPython.external import pexpect
23 # We ship our own copy of pexpect (it's a single file) to minimize dependencies
24 # for users, but it's only used if we don't find the system copy.
25 try:
26 import pexpect
27 except ImportError:
28 from IPython.external import pexpect
29
23
30 # Our own
24 # Our own
31 from .autoattr import auto_attr
25 from .autoattr import auto_attr
32 from ._process_common import getoutput
26 from ._process_common import getoutput
27 from IPython.utils import text
33
28
34 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
35 # Function definitions
30 # Function definitions
@@ -132,6 +127,9 b' class ProcessHandler(object):'
132 -------
127 -------
133 int : child's exitstatus
128 int : child's exitstatus
134 """
129 """
130 # Get likely encoding for the output.
131 enc = text.getdefaultencoding()
132
135 pcmd = self._make_cmd(cmd)
133 pcmd = self._make_cmd(cmd)
136 # Patterns to match on the output, for pexpect. We read input and
134 # Patterns to match on the output, for pexpect. We read input and
137 # allow either a short timeout or EOF
135 # allow either a short timeout or EOF
@@ -155,7 +153,7 b' class ProcessHandler(object):'
155 # res is the index of the pattern that caused the match, so we
153 # res is the index of the pattern that caused the match, so we
156 # know whether we've finished (if we matched EOF) or not
154 # know whether we've finished (if we matched EOF) or not
157 res_idx = child.expect_list(patterns, self.read_timeout)
155 res_idx = child.expect_list(patterns, self.read_timeout)
158 print(child.before[out_size:], end='')
156 print(child.before[out_size:].decode(enc, 'replace'), end='')
159 flush()
157 flush()
160 if res_idx==EOF_index:
158 if res_idx==EOF_index:
161 break
159 break
@@ -171,7 +169,7 b' class ProcessHandler(object):'
171 try:
169 try:
172 out_size = len(child.before)
170 out_size = len(child.before)
173 child.expect_list(patterns, self.terminate_timeout)
171 child.expect_list(patterns, self.terminate_timeout)
174 print(child.before[out_size:], end='')
172 print(child.before[out_size:].decode(enc, 'replace'), end='')
175 sys.stdout.flush()
173 sys.stdout.flush()
176 except KeyboardInterrupt:
174 except KeyboardInterrupt:
177 # Impatient users tend to type it multiple times
175 # Impatient users tend to type it multiple times
General Comments 0
You need to be logged in to leave comments. Login now