Skip to content

Commit

Permalink
print: decide based on the fd if we should bypass the file object
Browse files Browse the repository at this point in the history
This makes it work with active colorama which monkey patches
sys.stdout/stderr
  • Loading branch information
lazka committed Aug 20, 2016
1 parent ddc1d44 commit 6df3160
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions senf/_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def print_(*objects, **kwargs):
if end == "\n":
end = os.linesep

if os.name == "nt" and file in (sys.__stdout__, sys.__stderr__):
if os.name == "nt":
_print_windows(objects, sep, end, file, flush)
else:
_print_default(objects, sep, end, file, flush)
Expand Down Expand Up @@ -105,12 +105,12 @@ def _print_default(objects, sep, end, file, flush):
def _print_windows(objects, sep, end, file, flush):
"""The windows implementation of print_()"""

if file is sys.__stdout__:
fileno = file.fileno()
h = winapi.INVALID_HANDLE_VALUE
if fileno == 1:
h = winapi.GetStdHandle(winapi.STD_OUTPUT_HANDLE)
elif file is sys.__stderr__:
elif fileno == 2:
h = winapi.GetStdHandle(winapi.STD_ERROR_HANDLE)
else:
assert 0

if h == winapi.INVALID_HANDLE_VALUE:
return _print_default(objects, sep, end, file, flush)
Expand Down Expand Up @@ -138,8 +138,6 @@ def _print_windows(objects, sep, end, file, flush):
# make sure we flush before we apply any console attributes
file.flush()

fileno = file.fileno()

# try to force a utf-8 code page
old_cp = winapi.GetConsoleOutputCP()
encoding = "utf-8"
Expand Down

0 comments on commit 6df3160

Please sign in to comment.