Skip to content
Myr

Set console to binary on windows as fallback

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 64c4567 · Browse files · 2 files+11 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+11 added−7 removed

  • other:+11 added−7 removed

2 files+11 added−7 removed

modified:click/_compat.py+3 added-7 removed
155 unmodified lines
156 # An option would be to do what Python 3 does and to open the file as157 # binary only, patch it back to the system, and then use a wrapper158 # stream that converts newlines. It's not quite clear what's the159 # correct option here.160 #161 # This code also lives in _winconsole for the fallback to the console162 # emulation stream.163 if WIN:164 import msvcrt165 def set_binary_mode(f):166 try:
2 unmodified lines
169 pass170 else:171 msvcrt.setmode(fileno, os.O_BINARY)172 return f170171 class WindowsConsole(_FixupStream):172 def __repr__(self):173 return '<%s name=%r>' % (174 self.__class__.__name__,175 self.name,176 )173 else:174 set_binary_mode = lambda x: x175176 def isidentifier(x):
466 unmodified lines
modified:click/_winconsole.py+8 added
7 unmodified lines
8# the entire interpreter but just work in our little world of9# echo and prmopt.1011import io12import os13import sys14import zlib15import time16import ctypes17import msvcrt18from click._compat import _NonClosingTextIOWrapper, text_type, PY219from ctypes import byref, POINTER, pythonapi, c_int, c_char, c_char_p, \20 c_void_p, py_object, c_ssize_t, c_ulong, windll, WINFUNCTYPE21from ctypes.wintypes import LPWSTR, LPCWSTR
235 unmodified lines
257 if not PY2:258 f = getattr(f, 'buffer')259 if f is None:260 return None261 else:262 # If we are on Python 2 we need to set the stream that we263 # deal with to binary mode as otherwise the exercise if a264 # bit moot. The same problems apply as for265 # get_binary_stdin and friends from _compat.266 msvcrt.setmode(f.fileno(), os.O_BINARY)267 return func(f)