Skip to content

Commit

Permalink
Rework the "Not enough space" workaround for Python 2 on Windows 7 an…
Browse files Browse the repository at this point in the history
…d below
  • Loading branch information
segevfiner committed Sep 21, 2018
1 parent 7a5b8c8 commit c612c20
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
7 changes: 6 additions & 1 deletion click/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,11 @@ def get_binary_stdin():
return set_binary_mode(sys.stdin)

def get_binary_stdout():
_wrap_std_stream('stdout')
return set_binary_mode(sys.stdout)

def get_binary_stderr():
_wrap_std_stream('stderr')
return set_binary_mode(sys.stderr)

def get_text_stdin(encoding=None, errors=None):
Expand All @@ -198,12 +200,14 @@ def get_text_stdin(encoding=None, errors=None):
return _make_text_stream(sys.stdin, encoding, errors)

def get_text_stdout(encoding=None, errors=None):
_wrap_std_stream('stdout')
rv = _get_windows_console_stream(sys.stdout, encoding, errors)
if rv is not None:
return rv
return _make_text_stream(sys.stdout, encoding, errors)

def get_text_stderr(encoding=None, errors=None):
_wrap_std_stream('stderr')
rv = _get_windows_console_stream(sys.stderr, encoding, errors)
if rv is not None:
return rv
Expand Down Expand Up @@ -533,7 +537,7 @@ def should_strip_ansi(stream=None, color=None):
# Windows has a smaller terminal
DEFAULT_COLUMNS = 79

from ._winconsole import _get_windows_console_stream
from ._winconsole import _get_windows_console_stream, _wrap_std_stream

def _get_argv_encoding():
import locale
Expand Down Expand Up @@ -595,6 +599,7 @@ def _get_argv_encoding():
return getattr(sys.stdin, 'encoding', None) or get_filesystem_encoding()

_get_windows_console_stream = lambda *x: None
_wrap_std_stream = lambda *x: None


def term_len(x):
Expand Down
20 changes: 11 additions & 9 deletions click/_winconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def __repr__(self):
)


class StreamWrapper(object):
class WindowsChunkedWriter(object):
"""
Wraps a stream (such as stdout), acting as a transparent proxy for all
attribute access apart from method 'write()' which we wrap to write in
Expand All @@ -225,6 +225,16 @@ def write(self, text):
written += to_write


_wrapped_std_streams = set()


def _wrap_std_stream(name):
# Python 2 & Windows 7 and below
if PY2 and sys.getwindowsversion()[:2] <= (6, 1) and name not in _wrapped_std_streams:
setattr(sys, name, WindowsChunkedWriter(getattr(sys, name)))
_wrapped_std_streams.add(name)


def _get_text_stdin(buffer_stream):
text_stream = _NonClosingTextIOWrapper(
io.BufferedReader(_WindowsConsoleReader(STDIN_HANDLE)),
Expand All @@ -233,21 +243,13 @@ def _get_text_stdin(buffer_stream):


def _get_text_stdout(buffer_stream):
if PY2:
buffer_stream = StreamWrapper(buffer_stream)
sys.stdout = StreamWrapper(sys.stdout)

text_stream = _NonClosingTextIOWrapper(
_WindowsConsoleWriter(STDOUT_HANDLE),
'utf-16-le', 'strict', line_buffering=True)
return ConsoleStream(text_stream, buffer_stream)


def _get_text_stderr(buffer_stream):
if PY2:
buffer_stream = StreamWrapper(buffer_stream)
sys.stderr = StreamWrapper(sys.stderr)

text_stream = _NonClosingTextIOWrapper(
_WindowsConsoleWriter(STDERR_HANDLE),
'utf-16-le', 'strict', line_buffering=True)
Expand Down

0 comments on commit c612c20

Please sign in to comment.