Brodie Rao avatar Brodie Rao committed 9bc6432

Fix subprocess.Popen.communicate(input=...) not working with str objects under Python 3

Comments (0)

Files changed (1)

 
 import difflib
 import itertools
+import locale
 import optparse
 import os
 import re
         return None
     return lambda: signal.signal(signal.SIGPIPE, signal.SIG_DFL)
 
+def encodeinput(s):
+    """Encode s so it can be used as subprocess input.
+
+    For Python 2, this returns the string as-is. For Python 3, it encodes it
+    based on the locale (to match open() decoding in the same manner).
+    """
+    if sys.version_info == 2:
+        return s
+    return s.encode(locale.getpreferredencoding())
+
 def test(path, shell, indent=2):
     """Run test at path and return input, output, and diff.
 
             after.setdefault(pos, []).append(line)
     stdin.append('echo "\n%s %s $?"\n' % (salt, i + 1))
 
-    output = p.communicate(input=''.join(stdin))[0]
+    output = p.communicate(input=encodeinput(''.join(stdin)))[0]
     if p.returncode == 80:
         return (refout, None, [])
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.