Skip to content

Commit

Permalink
more robust handling of TIOCGWINSZ struct in _pty_size()
Browse files Browse the repository at this point in the history
  • Loading branch information
ploxiln committed Aug 22, 2021
1 parent 835959b commit 7592faf
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions fabric/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,15 @@ def _pty_size():
import fcntl
import termios
# We want two short unsigned integers (rows, cols)
fmt = 'HH'
# unsigned short (row, col, xpixel, ypixel) (pixel values unused)
fmt = 'HHHH'
# Create an empty (zeroed) buffer for ioctl to map onto. Yay for C!
buffer = struct.pack(fmt, 0, 0)
# Call TIOCGWINSZ to get window size of stdout, returns our filled
# buffer
buffer = struct.pack(fmt, 0, 0, 0, 0)
# Call TIOCGWINSZ to get window size of stdout, returns our filled buffer
try:
result = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ,
buffer)
result = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, buffer)
# Unpack buffer back into Python data types
rows, cols = struct.unpack(fmt, result)
rows, cols, _, _ = struct.unpack(fmt, result)
# Fall back to defaults if TIOCGWINSZ returns unreasonable values
if rows == 0:
rows = default_rows
Expand Down

0 comments on commit 7592faf

Please sign in to comment.