refine py.process.cmdexec handling wrt unicode on all python versions

--HG--
branch : trunk
This commit is contained in:
holger krekel 2010-06-25 10:30:15 +02:00
parent 4d75c703a0
commit f856db29dc
3 changed files with 14 additions and 3 deletions

View File

@ -48,6 +48,8 @@ Bug fixes / Maintenance
- don't print empty lines when showing junitxml-filename - don't print empty lines when showing junitxml-filename
- add optional boolean ignore_errors parameter to py.path.local.remove - add optional boolean ignore_errors parameter to py.path.local.remove
- fix terminal writing on win32/python2.4 - fix terminal writing on win32/python2.4
- py.process.cmdexec() now tries harder to return properly encoded unicode objects
on all python versions
Changes between 1.3.0 and 1.3.1 Changes between 1.3.0 and 1.3.1
================================================== ==================================================

View File

@ -8,18 +8,25 @@ import py
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
def cmdexec(cmd): 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. raise cmdexec.ExecutionFailed exeception if the command failed.
the exception will provide an 'err' attribute containing the exception will provide an 'err' attribute containing
the error-output from the command. 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, process = subprocess.Popen(cmd, shell=True,
universal_newlines=True, universal_newlines=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate() out, err = process.communicate()
out = py.builtin._totext(out, sys.stdout.encoding) if sys.version_info[0] < 3: # on py3 we get unicode strings, on py2 not
err = py.builtin._totext(err, sys.stderr.encoding) 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() status = process.poll()
if status: if status:
raise ExecutionFailed(status, status, cmd, out, err) raise ExecutionFailed(status, status, cmd, out, err)

View File

@ -8,11 +8,13 @@ class Test_exec_cmd:
def test_simple(self): def test_simple(self):
out = cmdexec('echo hallo') out = cmdexec('echo hallo')
assert out.strip() == 'hallo' assert out.strip() == 'hallo'
assert py.builtin._istext(out)
def test_simple_newline(self): def test_simple_newline(self):
import sys import sys
out = cmdexec(r"""%s -c "print ('hello')" """ % sys.executable) out = cmdexec(r"""%s -c "print ('hello')" """ % sys.executable)
assert out == 'hello\n' assert out == 'hello\n'
assert py.builtin._istext(out)
def test_simple_error(self): def test_simple_error(self):
py.test.raises (cmdexec.Error, cmdexec, 'exit 1') py.test.raises (cmdexec.Error, cmdexec, 'exit 1')