Skip to content

Commit 544382d

Browse files
authored
Merge pull request #201 from Victorious3/issue174
Correct terminal size on Windows and >= py33
2 parents 3c23d42 + 225af1b commit 544382d

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

CHANGELOG

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
1.7.0 (unreleased)
2+
==================
3+
4+
- fix #174: use ``shutil.get_terminal_size()`` in Python 3.3+ to determine the size of the
5+
terminal, which produces more accurate results than the previous method.
6+
7+
18
1.6.0 (2018-08-27)
29
==================
310

py/_io/terminalwriter.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import sys, os, unicodedata
99
import py
1010
py3k = sys.version_info[0] >= 3
11+
py33 = sys.version_info >= (3, 3)
1112
from py.builtin import text, bytes
1213

1314
win32_and_ctypes = False
@@ -24,10 +25,15 @@
2425

2526

2627
def _getdimensions():
27-
import termios,fcntl,struct
28-
call = fcntl.ioctl(1,termios.TIOCGWINSZ,"\000"*8)
29-
height,width = struct.unpack( "hhhh", call ) [:2]
30-
return height, width
28+
if py33:
29+
import shutil
30+
size = shutil.get_terminal_size()
31+
return size.lines, size.columns
32+
else:
33+
import termios, fcntl, struct
34+
call = fcntl.ioctl(1, termios.TIOCGWINSZ, "\000" * 8)
35+
height, width = struct.unpack("hhhh", call)[:2]
36+
return height, width
3137

3238

3339
def get_terminal_width():

testing/io_/test_terminalwriter.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections import namedtuple
12

23
import py
34
import os, sys
@@ -10,16 +11,22 @@ def test_get_terminal_width():
1011
assert x == terminalwriter.get_terminal_width
1112

1213
def test_getdimensions(monkeypatch):
13-
fcntl = py.test.importorskip("fcntl")
14-
import struct
15-
l = []
16-
monkeypatch.setattr(fcntl, 'ioctl', lambda *args: l.append(args))
17-
try:
18-
terminalwriter._getdimensions()
19-
except (TypeError, struct.error):
20-
pass
21-
assert len(l) == 1
22-
assert l[0][0] == 1
14+
if sys.version_info >= (3, 3):
15+
import shutil
16+
Size = namedtuple('Size', 'lines columns')
17+
monkeypatch.setattr(shutil, 'get_terminal_size', lambda: Size(60, 100))
18+
assert terminalwriter._getdimensions() == (60, 100)
19+
else:
20+
fcntl = py.test.importorskip("fcntl")
21+
import struct
22+
l = []
23+
monkeypatch.setattr(fcntl, 'ioctl', lambda *args: l.append(args))
24+
try:
25+
terminalwriter._getdimensions()
26+
except (TypeError, struct.error):
27+
pass
28+
assert len(l) == 1
29+
assert l[0][0] == 1
2330

2431
def test_terminal_width_COLUMNS(monkeypatch):
2532
""" Dummy test for get_terminal_width

0 commit comments

Comments
 (0)