Skip to content

Commit 0eb133f

Browse files
committed
Fix subprocess encoding for Python 3.3/3.4 tests
The issue was that Python 3.3/3.4 don't have subprocess.run() so they use the legacy Popen path, but unlike Python 2.7, they need bytes for stdin, not strings. Fixed by checking Python version separately from subprocess.run feature detection and encoding strings to bytes for Python 3.x versions.
1 parent 89b4031 commit 0eb133f

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

test/subprocess_compat.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Subprocess compatibility utilities for Python 2.7/3.x."""
22
import subprocess
3+
import sys
34

45

56
def run_subprocess(cmd, timeout=None, input_data=None, env=None):
@@ -10,11 +11,13 @@ def run_subprocess(cmd, timeout=None, input_data=None, env=None):
1011
return subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
1112
input=input_bytes, timeout=timeout, env=env)
1213
else:
13-
# Python 2.7, 3.3, 3.4 - use str as-is, no timeout support
14+
# Python 2.7, 3.3, 3.4 - no subprocess.run, no timeout support
1415
popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
1516
stdin=subprocess.PIPE if input_data else None, env=env)
1617
# For Python 3.3/3.4, communicate() doesn't support timeout
17-
# We'll ignore timeout for older versions - tests should be fast enough
18+
# Also, Python 3.x needs bytes for stdin, Python 2.x needs str
19+
if input_data and sys.version_info[0] >= 3 and isinstance(input_data, str):
20+
input_data = input_data.encode('utf-8')
1821
stdout, stderr = popen.communicate(input_data)
1922
# Create a simple result object similar to subprocess.CompletedProcess
2023
class Result:

0 commit comments

Comments
 (0)