From 4a7125ec920c44b487f76c3da6cbd48a85063965 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 29 Apr 2022 20:39:32 +0530 Subject: [PATCH] ssh kitten: Suppress error prints about invalid items in kitty.conf Fixes #4985 --- kittens/ssh/main.py | 5 +++-- kitty/utils.py | 21 +++++++++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/kittens/ssh/main.py b/kittens/ssh/main.py index 4f3ccdb1d..6a6ebd090 100644 --- a/kittens/ssh/main.py +++ b/kittens/ssh/main.py @@ -39,7 +39,7 @@ from kitty.shm import SharedMemory from kitty.types import run_once from kitty.utils import ( SSHConnectionData, expandvars, resolve_abs_or_config_path, - set_echo as turn_off_echo + set_echo as turn_off_echo, suppress_error_logging ) from .completion import complete, ssh_options @@ -152,7 +152,8 @@ def serialize_env(literal_env: Dict[str, str], env: Dict[str, str], base_env: Di @run_once def kitty_opts() -> Options: from kitty.cli import create_default_opts - return create_default_opts() + with suppress_error_logging(): + return create_default_opts() def make_tarfile(ssh_opts: SSHOptions, base_env: Dict[str, str], compression: str = 'gz', literal_env: Dict[str, str] = {}) -> bytes: diff --git a/kitty/utils.py b/kitty/utils.py index 28d49e687..0b35bb72a 100644 --- a/kitty/utils.py +++ b/kitty/utils.py @@ -13,8 +13,8 @@ from contextlib import contextmanager, suppress from functools import lru_cache from time import monotonic from typing import ( - TYPE_CHECKING, Any, Callable, Dict, Generator, Iterable, List, Mapping, - Match, NamedTuple, Optional, Pattern, Tuple, Union, cast + TYPE_CHECKING, Any, Callable, Dict, Generator, Iterable, Iterator, List, + Mapping, Match, NamedTuple, Optional, Pattern, Tuple, Union, cast ) from .constants import ( @@ -122,6 +122,19 @@ def log_error(*a: Any, **k: str) -> None: output(msg) +@contextmanager +def suppress_error_logging() -> Iterator[None]: + before = getattr(log_error, 'redirect', suppress_error_logging) + setattr(log_error, 'redirect', lambda *a: None) + try: + yield + finally: + if before is suppress_error_logging: + delattr(log_error, 'redirect') + else: + setattr(log_error, 'redirect', before) + + def ceil_int(x: float) -> int: return int(math.ceil(x)) @@ -536,7 +549,7 @@ def set_echo(fd: int = -1, on: bool = False) -> Tuple[int, List[Union[int, List[ @contextmanager -def no_echo(fd: int = -1) -> Generator[None, None, None]: +def no_echo(fd: int = -1) -> Iterator[None]: import termios fd, old = set_echo(fd) try: @@ -954,7 +967,7 @@ def path_from_osc7_url(url: str) -> str: if url.startswith('kitty-shell-cwd://'): return '/' + url.split('/', 3)[-1] if url.startswith('file://'): - from urllib.parse import urlparse, unquote + from urllib.parse import unquote, urlparse return unquote(urlparse(url).path) return ''