Added printing to stderr to echo
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 aa89d5d · Browse files · 3 files·+34 added−21 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+34 added−21 removed
- other:+34 added−21 removed
Provenance
- Commit
7d76c67- Tree
350a135- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
aa89d5d
3 files changed+34 added−21 removed
modified:click/_compat.py+21 added-18 removed
520 unmodified lines521 except Exception:522 return False523 524 525+def _make_cached_stream_func(src_func, wrapper_func):525-_default_text_cache = WeakKeyDictionary()526+ cache = WeakKeyDictionary()527+ def func():528+ stream = src_func()529+ try:530+ rv = cache.get(stream)531+ except Exception:532+ rv = None533+ if rv is not None:534+ return rv535+ rv = wrapper_func()536+ try:537+ cache[stream] = rv538+ except Exception:539+ pass540+ return rv541+ return func542 543 528-def _default_text_stdout():529- """Like :func:`get_text_stdout` but uses a cache if available to speed530- it up. This will not create streams over and over again.531- """532- stream = sys.stdout533- try:534- rv = _default_text_cache.get(stream)535- except Exception:536- rv = None537- if rv is not None:538- return rv539- rv = get_text_stdout()540- try:541- _default_text_cache[stream] = rv542- except Exception:543- pass544- return rv544+_default_text_stdout = _make_cached_stream_func(545+ lambda: sys.stdout, get_text_stdout)546+_default_text_stderr = _make_cached_stream_func(547+ lambda: sys.stderr, get_text_stderr)548 549 550 binary_streams = {551 'stdin': get_binary_stdin,9 unmodified linesmodified:click/utils.py+12 added-3 removed
3 unmodified lines4 5 from ._compat import text_type, open_stream, get_streerror, string_types, \6 PY2, binary_streams, text_streams, filename_to_ui, \7 auto_wrap_for_ansi, strip_ansi, isatty, _default_text_stdout, \8- is_bytes, WIN8+ _default_text_stderr, is_bytes, WIN9 10 if not PY2:11 from ._compat import _find_binary_writer12 178 unmodified lines191 def __exit__(self, exc_type, exc_value, tb):192 self.close_intelligently()193 194 195-def echo(message=None, file=None, nl=True):195+def echo(message=None, file=None, nl=True, err=False):196 """Prints a message plus a newline to the given file or stdout. On197 first sight, this looks like the print function, but it has improved198 support for handling Unicode and binary data that does not fail no199 matter how badly configured the system is.16 unmodified lines216 .. versionchanged:: 2.0217 Starting with version 2.0 of Click, the echo function will work218 with colorama if it's installed.219 220+ .. versionadded:: 3.0221+ The `err` parameter was added.222+223 :param message: the message to print224 :param file: the file to write to (defaults to ``stdout``)225+ :param err: if set to true the file defaults to ``stderr`` instead of226+ ``stdout``. This is faster and easier than calling227+ :func:`get_text_stderr` yourself.228 :param nl: if set to `True` (the default) a newline is printed afterwards.229 """230 if file is None:231+ if err:232+ file = _default_text_stderr()233+ else:225- file = _default_text_stdout()234+ file = _default_text_stdout()235 236 # Convert non bytes/text into the native string type.237 if message is not None and not isinstance(message, echo_native_types):238 message = text_type(message)136 unmodified linesmodified:CHANGES+1 added
28 unmodified lines29 whitespace stripped.30 - added a way to disable the standalone mode of the `main`31 method on a Click command to be able to handle errors better.32 - added support for returning values from command callbacks.33+- added simplifications for printing to stderr from `echo`.34 35 Version 2.636 -----------37 111 unmodified lines