[pypy-svn] r75575 - pypy/trunk/py/_process

hpk at codespeak.net hpk at codespeak.net
Fri Jun 25 10:28:03 CEST 2010


Author: hpk
Date: Fri Jun 25 10:28:02 2010
New Revision: 75575

Modified:
   pypy/trunk/py/_process/cmdexec.py
Log:
refine unicode handling for py.process.cmdexec 


Modified: pypy/trunk/py/_process/cmdexec.py
==============================================================================
--- pypy/trunk/py/_process/cmdexec.py	(original)
+++ pypy/trunk/py/_process/cmdexec.py	Fri Jun 25 10:28:02 2010
@@ -8,20 +8,25 @@
 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)



More information about the Pypy-commit mailing list