Skip to content

Commit

Permalink
Added fallback mechanism and failsafe warning to unbuffering of stdout (
Browse files Browse the repository at this point in the history
#168)

* Added fallback mechanism and failsafe warning to unbuffering of stdout

* adjust var name

* don't cover the unsupported operation. It is entirely inside of failsafe

Co-authored-by: doren calliku <dcalliku@gmail.com>
  • Loading branch information
Helveg and pomodoren committed Nov 5, 2020
1 parent 93d9707 commit 21bba07
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions bsb/reporting.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
import warnings, base64, io, sys
import warnings, base64, io, sys, functools

sys.stdout = io.TextIOWrapper(open(sys.stdout.fileno(), "wb", 0), write_through=True)

def wrap_writer(stream, writer):
@functools.wraps(writer)
def wrapped(self, *args, **kwargs):
writer(*args, **kwargs)
self.flush()

return wrapped.__get__(stream)


try:
sys.stdout = io.TextIOWrapper(open(sys.stdout.fileno(), "wb", 0), write_through=True)
except io.UnsupportedOperation: # pragma: nocover
try:
writers = ["write", "writelines"]
for w in writers:
writer = getattr(sys.stdout, w)
wrapped = wrap_writer(sys.stdout, writer)
setattr(sys.stdout, w, wrapped)
except:
warnings.warn(
f"Unable to create unbuffered wrapper around `sys.stdout` ({sys.stdout.__class__.__name__})."
)

_verbosity = 1
_report_file = None
Expand Down

0 comments on commit 21bba07

Please sign in to comment.