Skip to content

Commit d24f50c

Browse files
authored
Merge pull request #38 from minrk/flush-on-exit
2 parents b30ae5d + 2682eb4 commit d24f50c

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

wurlitzer.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,19 @@ def _finish_handle(self):
143143
"""Finish handle, if anything should be done when it's all wrapped up."""
144144
pass
145145

146-
def __enter__(self):
147-
# flush anything out before starting
146+
def _flush(self):
147+
"""flush sys.stdout/err and low-level FDs"""
148+
if self._stdout and sys.stdout:
149+
sys.stdout.flush()
150+
if self._stderr and sys.stderr:
151+
sys.stderr.flush()
152+
148153
libc.fflush(c_stdout_p)
149154
libc.fflush(c_stderr_p)
155+
156+
def __enter__(self):
157+
# flush anything out before starting
158+
self._flush()
150159
# setup handle
151160
self._setup_handle()
152161
self._control_r, self._control_w = os.pipe()
@@ -172,8 +181,7 @@ def flush_main():
172181
msg = flush_queue.get()
173182
if msg == 'stop':
174183
return
175-
libc.fflush(c_stdout_p)
176-
libc.fflush(c_stderr_p)
184+
self._flush()
177185

178186
flush_thread = threading.Thread(target=flush_main)
179187
flush_thread.daemon = True
@@ -241,9 +249,9 @@ def forwarder():
241249
return self.handle
242250

243251
def __exit__(self, exc_type, exc_value, traceback):
244-
# flush the underlying C buffers
245-
libc.fflush(c_stdout_p)
246-
libc.fflush(c_stderr_p)
252+
# flush before exiting
253+
self._flush()
254+
247255
# signal output is complete on control pipe
248256
os.write(self._control_w, b'\1')
249257
self.thread.join()

0 commit comments

Comments
 (0)