diff --git a/src/polysh/dispatchers.py b/src/polysh/dispatchers.py index 996d19a..e0bd3f8 100644 --- a/src/polysh/dispatchers.py +++ b/src/polysh/dispatchers.py @@ -89,15 +89,12 @@ def update_terminal_size() -> None: place taken by the longest name""" w, h = terminal_size() w = max(w - display_names.max_display_name_length - 2, min(w, 10)) - # python bug http://python.org/sf/1112949 on amd64 - # from ajaxterm.py - bug = struct.unpack('i', struct.pack('I', termios.TIOCSWINSZ))[0] packed_size = struct.pack('HHHH', h, w, 0, 0) term_size = w, h for i in all_instances(): if i.enabled and i.term_size != term_size: i.term_size = term_size - fcntl.ioctl(i.fd, bug, packed_size) + fcntl.ioctl(i.fd, termios.TIOCSWINSZ, packed_size) def format_info(info_list: List[List[bytes]]) -> List[bytes]: diff --git a/tests/test_dispatchers.py b/tests/test_dispatchers.py index a208e1f..61dc4b2 100644 --- a/tests/test_dispatchers.py +++ b/tests/test_dispatchers.py @@ -15,8 +15,15 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import fcntl +import os +import struct +import termios +from types import SimpleNamespace + import pytest +from polysh import dispatchers from polysh.dispatchers import _split_port, format_info @@ -48,3 +55,22 @@ def test_format_info_pads_all_but_the_last_column(): ]) def test_split_port(host, split): assert _split_port(host) == split + + +def test_update_terminal_size_uses_unsigned_request(monkeypatch): + """TIOCSWINSZ does not fit in a signed int on macOS: passing it + converted to a negative number makes the ioctl fail with ENOTTY""" + master, slave = os.openpty() + remote = SimpleNamespace(enabled=True, term_size=(-1, -1), fd=master) + monkeypatch.setattr(dispatchers, 'all_instances', lambda: [remote]) + monkeypatch.setattr(dispatchers, 'terminal_size', lambda: (80, 24)) + monkeypatch.setattr(dispatchers.display_names, 'max_display_name_length', 0) + try: + dispatchers.update_terminal_size() + rows, cols, _, _ = struct.unpack( + 'HHHH', fcntl.ioctl(slave, termios.TIOCGWINSZ, b'\0' * 8) + ) + finally: + os.close(master) + os.close(slave) + assert (cols, rows) == (78, 24)