Skip to content

Commit 6630533

Browse files
authored
tmux_cmd: Modernize to use text=True (#560)
Resolves #558.
2 parents 4bb3158 + 2a4901d commit 6630533

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

CHANGES

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ $ pip install --user --upgrade --pre libtmux
1515

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

18+
### Bug fixes
19+
20+
- `tmux_cmd`: Migrate to to `text=True`
21+
22+
This deprecates usage of `console_to_str()` and `str_from_console()`.
23+
24+
Resolves #558 via #560.
25+
26+
- compat: Remove `console_to_str()` and `str_from_console()`
27+
28+
These are both deprecated artifacts of libtmux' Python 2.x compatiblity layer.
29+
1830
## libtmux 0.41.0 (2025-02-02)
1931

2032
### Fixes

src/libtmux/_compat.py

-15
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@
77
console_encoding = sys.stdout.encoding
88

99

10-
def console_to_str(s: bytes) -> str:
11-
"""From pypa/pip project, pip.backwardwardcompat. License MIT."""
12-
try:
13-
return s.decode(console_encoding, "ignore")
14-
except UnicodeDecodeError:
15-
return s.decode("utf_8", "ignore")
16-
17-
1810
# TODO Consider removing, reraise does not seem to be called anywhere
1911
def reraise(
2012
tp: t.Type[BaseException],
@@ -26,13 +18,6 @@ def reraise(
2618
raise value
2719

2820

29-
def str_from_console(s: t.Union[str, bytes]) -> str:
30-
try:
31-
return str(s)
32-
except UnicodeDecodeError:
33-
return str(s, encoding="utf_8") if isinstance(s, bytes) else s
34-
35-
3621
import re
3722
from typing import Iterator, List, Tuple
3823

src/libtmux/common.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import typing as t
1616

1717
from . import exc
18-
from ._compat import LooseVersion, console_to_str, str_from_console
18+
from ._compat import LooseVersion
1919

2020
if t.TYPE_CHECKING:
2121
from collections.abc import Callable
@@ -226,7 +226,7 @@ def __init__(self, *args: t.Any) -> None:
226226

227227
cmd = [tmux_bin]
228228
cmd += args # add the command arguments to cmd
229-
cmd = [str_from_console(c) for c in cmd]
229+
cmd = [str(c) for c in cmd]
230230

231231
self.cmd = cmd
232232

@@ -235,6 +235,8 @@ def __init__(self, *args: t.Any) -> None:
235235
cmd,
236236
stdout=subprocess.PIPE,
237237
stderr=subprocess.PIPE,
238+
text=True,
239+
errors="backslashreplace",
238240
)
239241
stdout, stderr = self.process.communicate()
240242
returncode = self.process.returncode
@@ -244,14 +246,12 @@ def __init__(self, *args: t.Any) -> None:
244246

245247
self.returncode = returncode
246248

247-
stdout_str = console_to_str(stdout)
248-
stdout_split = stdout_str.split("\n")
249+
stdout_split = stdout.split("\n")
249250
# remove trailing newlines from stdout
250251
while stdout_split and stdout_split[-1] == "":
251252
stdout_split.pop()
252253

253-
stderr_str = console_to_str(stderr)
254-
stderr_split = stderr_str.split("\n")
254+
stderr_split = stderr.split("\n")
255255
self.stderr = list(filter(None, stderr_split)) # filter empty values
256256

257257
if "has-session" in cmd and len(self.stderr) and not stdout_split:

0 commit comments

Comments
 (0)