Skip to content

Commit 3a15639

Browse files
committed
refactor(wrap_stdio): remake classes
1 parent a5860a8 commit 3a15639

File tree

3 files changed

+48
-30
lines changed

3 files changed

+48
-30
lines changed

commitizen/wrap_stdio_linux.py

+22-14
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,50 @@
22

33
if sys.platform == "linux": # pragma: no cover
44
import os
5-
from io import IOBase
6-
7-
class WrapStdioLinux:
8-
def __init__(self, stdx: IOBase):
9-
self._fileno = stdx.fileno()
10-
if self._fileno == 0:
11-
fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY)
12-
tty = open(fd, "wb+", buffering=0)
13-
else:
14-
tty = open("/dev/tty", "w") # type: ignore
5+
6+
# from io import IOBase
7+
8+
class WrapStdinLinux:
9+
def __init__(self):
10+
fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY)
11+
tty = open(fd, "wb+", buffering=0)
1512
self.tty = tty
1613

1714
def __getattr__(self, key):
18-
if key == "encoding" and self._fileno == 0:
15+
if key == "encoding":
1916
return "UTF-8"
2017
return getattr(self.tty, key)
2118

2219
def __del__(self):
2320
self.tty.close()
2421

22+
class WrapStdoutLinux:
23+
def __init__(self):
24+
tty = open("/dev/tty", "w")
25+
self.tty = tty
26+
27+
def __getattr__(self, key):
28+
return getattr(self.tty, key)
29+
30+
def __del__(self):
31+
self.tty.close()
32+
2533
backup_stdin = None
2634
backup_stdout = None
2735
backup_stderr = None
2836

2937
def _wrap_stdio():
3038
global backup_stdin
3139
backup_stdin = sys.stdin
32-
sys.stdin = WrapStdioLinux(sys.stdin)
40+
sys.stdin = WrapStdinLinux()
3341

3442
global backup_stdout
3543
backup_stdout = sys.stdout
36-
sys.stdout = WrapStdioLinux(sys.stdout)
44+
sys.stdout = WrapStdoutLinux()
3745

3846
global backup_stderr
3947
backup_stderr = sys.stderr
40-
sys.stderr = WrapStdioLinux(sys.stderr)
48+
sys.stderr = WrapStdoutLinux()
4149

4250
def _unwrap_stdio():
4351
global backup_stdin

commitizen/wrap_stdio_unix.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@
55
import selectors
66
from asyncio import (
77
DefaultEventLoopPolicy,
8+
SelectorEventLoop,
89
get_event_loop_policy,
910
set_event_loop_policy,
1011
)
1112
from io import IOBase
1213

13-
class CZEventLoopPolicy(DefaultEventLoopPolicy): # pragma: no cover
14-
def get_event_loop(self):
15-
self.set_event_loop(self._loop_factory(selectors.SelectSelector()))
16-
return self._local._loop
17-
1814
class WrapStdioUnix:
1915
def __init__(self, stdx: IOBase):
2016
self._fileno = stdx.fileno()
@@ -33,6 +29,7 @@ def __getattr__(self, key):
3329
def __del__(self):
3430
self.tty.close()
3531

32+
# backup_event_loop = None
3633
backup_event_loop_policy = None
3734
backup_stdin = None
3835
backup_stdout = None
@@ -41,7 +38,10 @@ def __del__(self):
4138
def _wrap_stdio():
4239
global backup_event_loop_policy
4340
backup_event_loop_policy = get_event_loop_policy()
44-
set_event_loop_policy(CZEventLoopPolicy())
41+
42+
event_loop = DefaultEventLoopPolicy()
43+
event_loop.set_event_loop(SelectorEventLoop(selectors.SelectSelector()))
44+
set_event_loop_policy(event_loop)
4545

4646
global backup_stdin
4747
backup_stdin = sys.stdin

tests/test_wrap_stdio.py

+20-10
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,52 @@ def test_warp_stdio_exists():
1212
if sys.platform == "win32": # pragma: no cover
1313
pass
1414
elif sys.platform == "linux":
15-
from commitizen.wrap_stdio_linux import WrapStdioLinux
15+
from commitizen.wrap_stdio_linux import WrapStdinLinux, WrapStdoutLinux
1616

17-
def test_wrap_stdio_linux(mocker):
17+
def test_wrap_stdin_linux(mocker):
1818

1919
tmp_stdin = sys.stdin
20-
tmp_stdout = sys.stdout
21-
tmp_stderr = sys.stderr
2220

2321
mocker.patch("os.open")
2422
readerwriter_mock = mocker.mock_open(read_data="data")
2523
mocker.patch("builtins.open", readerwriter_mock, create=True)
2624

2725
mocker.patch.object(sys.stdin, "fileno", return_value=0)
28-
mocker.patch.object(sys.stdout, "fileno", return_value=1)
29-
mocker.patch.object(sys.stdout, "fileno", return_value=2)
3026

3127
wrap_stdio.wrap_stdio()
3228

3329
assert sys.stdin != tmp_stdin
34-
assert isinstance(sys.stdin, WrapStdioLinux)
30+
assert isinstance(sys.stdin, WrapStdinLinux)
3531
assert sys.stdin.encoding == "UTF-8"
3632
assert sys.stdin.read() == "data"
3733

34+
wrap_stdio.unwrap_stdio()
35+
36+
assert sys.stdin == tmp_stdin
37+
38+
def test_wrap_stdout_linux(mocker):
39+
40+
tmp_stdout = sys.stdout
41+
tmp_stderr = sys.stderr
42+
43+
mocker.patch("os.open")
44+
readerwriter_mock = mocker.mock_open(read_data="data")
45+
mocker.patch("builtins.open", readerwriter_mock, create=True)
46+
47+
wrap_stdio.wrap_stdio()
48+
3849
assert sys.stdout != tmp_stdout
39-
assert isinstance(sys.stdout, WrapStdioLinux)
50+
assert isinstance(sys.stdout, WrapStdoutLinux)
4051
sys.stdout.write("stdout")
4152
readerwriter_mock().write.assert_called_with("stdout")
4253

4354
assert sys.stderr != tmp_stderr
44-
assert isinstance(sys.stderr, WrapStdioLinux)
55+
assert isinstance(sys.stderr, WrapStdoutLinux)
4556
sys.stdout.write("stderr")
4657
readerwriter_mock().write.assert_called_with("stderr")
4758

4859
wrap_stdio.unwrap_stdio()
4960

50-
assert sys.stdin == tmp_stdin
5161
assert sys.stdout == tmp_stdout
5262
assert sys.stderr == tmp_stderr
5363

0 commit comments

Comments
 (0)