Skip to content

tmux_cmd: Modernize to use text=True #560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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