Skip to content
Myr

Added comments for windows binary support and made it more stable.

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 3269a22 · Browse files · 1 file+26 added−11 removed
This commit

State

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

Diff stats

1 file+26 added−11 removed

  • other:+26 added−11 removed

1 file+26 added−11 removed

modified:click/_compat.py+26 added-11 removed
1import re2import io3import os4import sys5import codecs67
96 unmodified lines
104 iteritems = lambda x: x.iteritems()105106 _identifier_re = re.compile(r'^[a-zA-Z_][a-zA-Z0-9_]*$')107107 def set_binary_mode(f):108 if sys.platform == 'win32':109 import os110 import msvcrt111 msvcrt.setmode(f.fileno(), os.O_BINARY)108 # For Windows we need to force stdout/stdin/stderr to binary if it's109 # fetched for that. This obviously is not the most correct way to do110 # it as it changes global state. Unfortunately there does not seem to111 # be a clear better way to do it as just reopening the file in binary112 # mode does not change anything.113 #114 # An option would be to do what python 3 does and to open the file as115 # binary only, patch it back to the system and then use a wrapper116 # stream that converts newlines. It's not quite clear what's the117 # correct option here.118 if sys.platform == 'win32':119 import msvcrt120 def set_binary_mode(f):121 try:122 fileno = f.fileno()123 except Exception:124 pass125 else:126 msvcrt.setmode(fileno, os.O_BINARY)127 return f128 else:129 set_binary_mode = lambda x: x130131 def isidentifier(x):132 return _identifier_re.search(x) is not None133134 def get_binary_stdin():117 set_binary_mode(sys.stdin)118 return sys.stdin135 return set_binary_mode(sys.stdin)136137 def get_binary_stdout():121 set_binary_mode(sys.stdout)122 return sys.stdout138 return set_binary_mode(sys.stdout)139140 def get_binary_stderr():125 set_binary_mode(sys.stderr)126 return sys.stderr141 return set_binary_mode(sys.stderr)142143 def get_text_stdin(encoding=None, errors=None):144 return _make_text_stream(sys.stdin, encoding, errors)145
222 unmodified lines