File tree 1 file changed +15
-0
lines changed
1 file changed +15
-0
lines changed Original file line number Diff line number Diff line change 1
1
import os
2
2
import sys
3
+ import ctypes
3
4
4
5
from typing import Any , Dict
5
6
@@ -16,6 +17,14 @@ class suppress_stdout_stderr(object):
16
17
# this context manager inside of a __del__ method
17
18
sys = sys
18
19
os = os
20
+ libc = ctypes .CDLL (None )
21
+ try :
22
+ c_stdout = ctypes .c_void_p .in_dll (libc , "stdout" )
23
+ c_stderr = ctypes .c_void_p .in_dll (libc , "stderr" )
24
+ except :
25
+ # macOS
26
+ c_stdout = ctypes .c_void_p .in_dll (libc , "__stdoutp" )
27
+ c_stderr = ctypes .c_void_p .in_dll (libc , "__stderrp" )
19
28
20
29
def __init__ (self , disable : bool = True ):
21
30
self .disable = disable
@@ -25,6 +34,9 @@ def __enter__(self):
25
34
if self .disable :
26
35
return self
27
36
37
+ self .libc .fflush (self .c_stdout )
38
+ self .libc .fflush (self .c_stderr )
39
+
28
40
self .old_stdout_fileno_undup = STDOUT_FILENO
29
41
self .old_stderr_fileno_undup = STDERR_FILENO
30
42
@@ -45,6 +57,9 @@ def __exit__(self, *_):
45
57
if self .disable :
46
58
return
47
59
60
+ self .libc .fflush (self .c_stdout )
61
+ self .libc .fflush (self .c_stderr )
62
+
48
63
# Check if sys.stdout and sys.stderr have fileno method
49
64
self .sys .stdout = self .old_stdout
50
65
self .sys .stderr = self .old_stderr
You can’t perform that action at this time.
0 commit comments