You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Curses terminal resize problems when Python is in interactive mode #46927
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2008-04-23.15:41:39.440>labels= ['extension-modules', 'type-bug', 'docs']
title='Curses terminal resize problems when Python is in interactive mode'updated_at=<Date2016-05-15.01:05:47.735>user='https://bugs.python.org/pbazant'
When this is run directly, everything is ok. When it is called via
execfile() from the interactive prompt, it shows the right screen size
after the first keypress, but behaves oddly after the resize.
IMHO, the following happens:
For some reason, env. variables LINES and COLUMNS are set but they are
not reflected in the os.environ structure nor they respond to screen
size changes. If these variables are set then the ncurses library (see
man pages) uses their values instead of getting the term size via
ioctl.
The ncurses library receives a SIGWINCH and sees that LINES
and COLUMNS are set. However, their values are same as the
screen dimensions before the resize, so it is perplexed why there is
a SIGWINCH if the screen did not change and it just ungetchs an ERR
and ncurses internal structures are not changed appropriately.
From the resizeterm man page:
"If the environment variables LINES or COLUMNS are set, this overrides
the library's use of the window size obtained from the operating
system. Thus, even if a SIGWINCH is received, no screen size change may
be recorded. In that case, no KEY_RESIZE is queued for the next call to
getch; an ERR will be returned instead."
Just to confirm: curses SIGWINCH handling is still (2.7/3.2) broken after importing readline.
Readline seems to set the LINES/COLUMNS environment vars, and this confuses curses, even if curses is imported after readline.
Clearing the LINES/COLUMNS vars after import readline fixes the issue:
os.environ['LINES'] = os.environ['COLUMNS'] = ''
or
os.unsetenv('LINES'); os.unsetenv('COLUMNS')
(or other variations).
I spent a couple hours tearing my hair out over this, before I found this report.
It may be possible for Python to work around this readline behavior by saving LINES/COLUMNS and restoring them after initializing readline.
Or maybe this should just be documented somewhere.
For the record, this is my interpretation of Pavel’s demo:
If you press a key (other than lowercase “q”) or resize the terminal, it displays the terminal dimensions in the top-left corner. Without Readline, the dimensions are updated when the terminal resizes.
When Readline is used (interactive mode, or simply “import readline”), the reported dimensions are static, which I understand is the bug.
I've been tracking a similar issue for a few days now: importing readline module is adding LINES and COLUMNS variables to the environment. Values are set to the terminal size at time of module import and never updated.
I tested again the original reproducer script, on today main branch. I do confirm it now shows correct values, both while executed stand-alone of from an interactive session (using exec(open("/home/mathieu/tmp/test.py").read())).
So I believe we can close this PR, as fixed by the above PR.
Activity
pbazant commentedon Apr 23, 2008
When python is in interactive mode, curses does not react to resize
events properly.
test.py:
When this is run directly, everything is ok. When it is called via
execfile() from the interactive prompt, it shows the right screen size
after the first keypress, but behaves oddly after the resize.
IMHO, the following happens:
For some reason, env. variables LINES and COLUMNS are set but they are
not reflected in the os.environ structure nor they respond to screen
size changes. If these variables are set then the ncurses library (see
man pages) uses their values instead of getting the term size via
ioctl.
The ncurses library receives a SIGWINCH and sees that LINES
and COLUMNS are set. However, their values are same as the
screen dimensions before the resize, so it is perplexed why there is
a SIGWINCH if the screen did not change and it just ungetchs an ERR
and ncurses internal structures are not changed appropriately.
From the resizeterm man page:
"If the environment variables LINES or COLUMNS are set, this overrides
the library's use of the window size obtained from the operating
system. Thus, even if a SIGWINCH is received, no screen size change may
be recorded. In that case, no KEY_RESIZE is queued for the next call to
getch; an ERR will be returned instead."
Executing
solves the problem for me.
Perhaps the problem has sth to do with python using readline
in interactive mode???
PB
BreamoreBoy commentedon Jul 5, 2010
Could somebody witht a nix box try this out, I'd do it myself if I had such a beast.
bitdancer commentedon Jul 5, 2010
Well, I can run it, but I have no idea what it is telling me or how it demonstrates a bug. Pavel, is this still a problem in 2.6/2.7?
poq commentedon May 17, 2012
Just to confirm: curses SIGWINCH handling is still (2.7/3.2) broken after importing readline.
Readline seems to set the LINES/COLUMNS environment vars, and this confuses curses, even if curses is imported after readline.
Clearing the LINES/COLUMNS vars after import readline fixes the issue:
os.environ['LINES'] = os.environ['COLUMNS'] = ''
or
os.unsetenv('LINES'); os.unsetenv('COLUMNS')
(or other variations).
I spent a couple hours tearing my hair out over this, before I found this report.
It may be possible for Python to work around this readline behavior by saving LINES/COLUMNS and restoring them after initializing readline.
Or maybe this should just be documented somewhere.
poq commentedon May 17, 2012
bpo-3948 is almost certainly a duplicate.
vadmium commentedon May 15, 2016
For the record, this is my interpretation of Pavel’s demo:
If you press a key (other than lowercase “q”) or resize the terminal, it displays the terminal dimensions in the top-left corner. Without Readline, the dimensions are updated when the terminal resizes.
When Readline is used (interactive mode, or simply “import readline”), the reported dimensions are static, which I understand is the bug.
Documentating this quirk may be a good start.
mbriand commentedon May 4, 2025
I've been tracking a similar issue for a few days now: importing
readlinemodule is addingLINESandCOLUMNSvariables to the environment. Values are set to the terminal size at time of module import and never updated.Reproduce
So with this reproduce code:
If I run it, we can see the both environment variables are set:
The problem is, if we do resize the terminal or change font size during the sleep() call, LINES and COLUMNS values are not updated correctly.
You can confirm this is indeed linked to the readline module import, if I remove it, these two variables are not set.
Analysis
I've been tracking where these variables are set, and this actually comes from the readline C library. This behaviour can be changed from the cpython source code using rl_change_environment: https://git.savannah.gnu.org/cgit/readline.git/tree/doc/readline.info#n3201
I made the following modification in cpython, and the variables are not set anymore.
Fix ?
Now I would be happy to open a PR with this change, but I have some doubts: I'm not sure there would not be any unexpected side effect with this.
pythongh-46927: Prevent readline from overriding environment
pythongh-46927: Prevent readline from overriding environment
pythongh-46927: Prevent readline from overriding environment
pythongh-46927: Prevent readline from overriding environment
pythongh-46927: Detect rl_change_environment with a configure probe
gh-46927: Prevent readline from overriding environment variables (GH-…
mbriand commentedon Jul 10, 2026
I tested again the original reproducer script, on today
mainbranch. I do confirm it now shows correct values, both while executed stand-alone of from an interactive session (usingexec(open("/home/mathieu/tmp/test.py").read())).So I believe we can close this PR, as fixed by the above PR.