Skip to content

Commit

Permalink
tmux_cmd: Modernize to use text=True (#560)
Browse files Browse the repository at this point in the history
Resolves #558.
  • Loading branch information
tony authored Feb 2, 2025
2 parents 4bb3158 + 2a4901d commit 6630533
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
12 changes: 12 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ $ pip install --user --upgrade --pre libtmux

<!-- To maintainers and contributors: Please add notes for the forthcoming version above -->

### 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
Expand Down
15 changes: 0 additions & 15 deletions src/libtmux/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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

Expand Down
12 changes: 6 additions & 6 deletions src/libtmux/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit 6630533

Please sign in to comment.