Skip to content

Commit 51ea308

Browse files
fschulzegaborbernat
authored andcommitted
Fix tox-dev#426 by writing directly to stdout buffer if possible to prevent str vs bytes issues. (tox-dev#739)
* Fix tox-dev#426 by writing directly to stdout buffer if possible to prevent str vs bytes issues. * Don't use assert in production code.
1 parent 659fd14 commit 51ea308

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

CONTRIBUTORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Alexandre Conrad
55
Allan Feldman
66
Andrii Soldatenko
77
Anthon van der Neuth
8+
Anthony Sottile
89
Asmund Grammeltwedt
910
Barry Warsaw
1011
Bartolome Sanchez Salado

changelog/426.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Write directly to stdout buffer if possible to prevent str vs bytes issues - by @asottile

tox/session.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,25 @@ def popen(self, args, cwd=None, env=None, redirect=True, returnout=False, ignore
159159
self.report.logpopen(popen, env=env)
160160
try:
161161
if resultjson and not redirect:
162-
assert popen.stderr is None # prevent deadlock
162+
if popen.stderr is not None:
163+
# prevent deadlock
164+
raise ValueError("stderr must not be piped here")
165+
# we read binary from the process and must write using a
166+
# binary stream
167+
buf = getattr(sys.stdout, 'buffer', sys.stdout)
163168
out = None
164169
last_time = time.time()
165170
while 1:
166171
# we have to read one byte at a time, otherwise there
167172
# might be no output for a long time with slow tests
168173
data = fin.read(1)
169174
if data:
170-
sys.stdout.write(data)
175+
buf.write(data)
171176
if b'\n' in data or (time.time() - last_time) > 1:
172177
# we flush on newlines or after 1 second to
173178
# provide quick enough feedback to the user
174179
# when printing a dot per test
175-
sys.stdout.flush()
180+
buf.flush()
176181
last_time = time.time()
177182
elif popen.poll() is not None:
178183
if popen.stdout is not None:

0 commit comments

Comments
 (0)