diff --git a/CHANGES b/CHANGES index 31565cee6..312bf3d5f 100644 --- a/CHANGES +++ b/CHANGES @@ -15,6 +15,18 @@ $ pip install --user --upgrade --pre libtmux +### Bug fixes + +- `tmux_cmd`: Migrate to to `text=True` + + This deprecates usage of `console_to_str()` and `str_from_console()`. + + Resolves #558 via #560. + +- compat: Remove `console_to_str()` and `str_from_console()` + + These are both deprecated artifacts of libtmux' Python 2.x compatiblity layer. + ## libtmux 0.41.0 (2025-02-02) ### Fixes diff --git a/src/libtmux/_compat.py b/src/libtmux/_compat.py index fd6fab772..acf5ddbe6 100644 --- a/src/libtmux/_compat.py +++ b/src/libtmux/_compat.py @@ -7,14 +7,6 @@ console_encoding = sys.stdout.encoding -def console_to_str(s: bytes) -> str: - """From pypa/pip project, pip.backwardwardcompat. License MIT.""" - try: - return s.decode(console_encoding, "ignore") - except UnicodeDecodeError: - return s.decode("utf_8", "ignore") - - # TODO Consider removing, reraise does not seem to be called anywhere def reraise( tp: t.Type[BaseException], @@ -26,13 +18,6 @@ def reraise( raise value -def str_from_console(s: t.Union[str, bytes]) -> str: - try: - return str(s) - except UnicodeDecodeError: - return str(s, encoding="utf_8") if isinstance(s, bytes) else s - - import re from typing import Iterator, List, Tuple diff --git a/src/libtmux/common.py b/src/libtmux/common.py index 83bfb5cd2..db0b4151f 100644 --- a/src/libtmux/common.py +++ b/src/libtmux/common.py @@ -15,7 +15,7 @@ import typing as t from . import exc -from ._compat import LooseVersion, console_to_str, str_from_console +from ._compat import LooseVersion if t.TYPE_CHECKING: from collections.abc import Callable @@ -226,7 +226,7 @@ def __init__(self, *args: t.Any) -> None: cmd = [tmux_bin] cmd += args # add the command arguments to cmd - cmd = [str_from_console(c) for c in cmd] + cmd = [str(c) for c in cmd] self.cmd = cmd @@ -235,6 +235,8 @@ def __init__(self, *args: t.Any) -> None: cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + text=True, + errors="backslashreplace", ) stdout, stderr = self.process.communicate() returncode = self.process.returncode @@ -244,14 +246,12 @@ def __init__(self, *args: t.Any) -> None: self.returncode = returncode - stdout_str = console_to_str(stdout) - stdout_split = stdout_str.split("\n") + stdout_split = stdout.split("\n") # remove trailing newlines from stdout while stdout_split and stdout_split[-1] == "": stdout_split.pop() - stderr_str = console_to_str(stderr) - stderr_split = stderr_str.split("\n") + stderr_split = stderr.split("\n") self.stderr = list(filter(None, stderr_split)) # filter empty values if "has-session" in cmd and len(self.stderr) and not stdout_split: