I am using Python 3.7 on a Debian Linux 9 box with the standard readline and curses modules. The following code should output 'True' when run inside an xterm and the xterm is resized:
import readline
import os
import curses
terminal_resized = False
def main(stdscr):
global terminal_resized
ch = stdscr.getch()
if ch == curses.KEY_RESIZE:
terminal_resized = True
os.unsetenv('LINES')
os.unsetenv('COLUMNS')
curses.wrapper(main)
print(terminal_resized)
However, the output is 'False', indicating that the call to getch() is not returning KEY_RESIZE. Indeed, it is returning -1 instead.
Note that the code works as expected if I don't import the readline module.
In Googling for solutions to this problem, I encountered a post from 2016 indicating that there's a conflict when importing both the readline and curses modules. Basically, the readline module sets the 'LINES' and 'COLUMNS' environment variables and this interferes with the built-in SIGWINCH signal handler of ncurses which is what is ultimately responsible for getch() returning KEY_RESIZE when the terminal is resized. This is the reason why I have those calls to unsetenv() in there.
However, those unsetenv() calls apparently have no effect in 2019. Indeed, when I try to print out all of the environment variables after importing readline I see no reference to 'LINES' or 'COLUMNS' in the output. I put those calls to unsetenv() in there anyway, though, to see if it would do anything useful.
Does anybody know how to get the curses getch() method to return KEY_RESIZE like it is supposed to when the readline module is also imported in Python 3.7?