aboutsummaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
authorHolger Krekel <holger.krekel@gmail.com>2010-06-25 08:28:02 +0000
committerHolger Krekel <holger.krekel@gmail.com>2010-06-25 08:28:02 +0000
commit5ac9215aff5b1dae1d3690203dd649aa51405f5f (patch)
tree3536fad623d68c7df66c78caa13f8b08a65e8407 /py
parentGrammatical typo (diff)
downloadpypy-5ac9215aff5b1dae1d3690203dd649aa51405f5f.tar.gz
pypy-5ac9215aff5b1dae1d3690203dd649aa51405f5f.tar.bz2
pypy-5ac9215aff5b1dae1d3690203dd649aa51405f5f.zip
refine unicode handling for py.process.cmdexec
Diffstat (limited to 'py')
-rw-r--r--py/_process/cmdexec.py15
1 files changed, 10 insertions, 5 deletions
diff --git a/py/_process/cmdexec.py b/py/_process/cmdexec.py
index df9dd1bfd2..43c71629c7 100644
--- a/py/_process/cmdexec.py
+++ b/py/_process/cmdexec.py
@@ -8,20 +8,25 @@ import py
from subprocess import Popen, PIPE
def cmdexec(cmd):
- """ return output of executing 'cmd' in a separate process.
+ """ return unicode output of executing 'cmd' in a separate process.
raise cmdexec.ExecutionFailed exeception if the command failed.
the exception will provide an 'err' attribute containing
the error-output from the command.
+ if the subprocess module does not provide a proper encoding/unicode strings
+ sys.getdefaultencoding() will be used, if that does not exist, 'UTF-8'.
"""
process = subprocess.Popen(cmd, shell=True,
universal_newlines=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
- out = py.builtin._totext(out, sys.stdout.encoding or
- sys.getdefaultencoding())
- err = py.builtin._totext(err, sys.stderr.encoding or
- sys.getdefaultencoding())
+ if sys.version_info[0] < 3: # on py3 we get unicode strings, on py2 not
+ try:
+ default_encoding = sys.getdefaultencoding() # jython may not have it
+ except AttributeError:
+ default_encoding = sys.stdout.encoding or 'UTF-8'
+ out = unicode(out, process.stdout.encoding or default_encoding)
+ err = unicode(err, process.stderr.encoding or default_encoding)
status = process.poll()
if status:
raise ExecutionFailed(status, status, cmd, out, err)