From 919f6ff33cd65b0a2b5e021ce682180015431374 Mon Sep 17 00:00:00 2001 From: ayorgo Date: Fri, 25 Aug 2023 12:12:17 +0200 Subject: [PATCH] Call ioctl more defensively --- matplotlib-backend-kitty/__init__.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/matplotlib-backend-kitty/__init__.py b/matplotlib-backend-kitty/__init__.py index 85415f7..c4afd31 100644 --- a/matplotlib-backend-kitty/__init__.py +++ b/matplotlib-backend-kitty/__init__.py @@ -10,6 +10,7 @@ import termios import tty from base64 import standard_b64encode +from contextlib import suppress from subprocess import run from matplotlib import interactive, is_interactive @@ -23,10 +24,14 @@ interactive(True) def term_size_px(): + width_px = height_px = 0 + # try to get terminal size from ioctl - buf = array.array("H", [0, 0, 0, 0]) - fcntl.ioctl(sys.stdout, termios.TIOCGWINSZ, buf) - _, _, width_px, height_px = buf + with suppress(OSError): + buf = array.array('H', [0, 0, 0, 0]) + fcntl.ioctl(sys.stdout, termios.TIOCGWINSZ, buf) + _, _, width_px, height_px = buf + if width_px != 0 and height_px != 0: return height_px, width_px