Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Curses terminal resize problems when Python is in interactive mode #46927

Open
pbazant mannequin opened this issue Apr 23, 2008 · 6 comments
Open

Curses terminal resize problems when Python is in interactive mode #46927

pbazant mannequin opened this issue Apr 23, 2008 · 6 comments
Labels
docs Documentation in the Doc dir extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error

Comments

@pbazant
Copy link
Mannequin

pbazant mannequin commented Apr 23, 2008

BPO 2675
Nosy @bitdancer, @vadmium

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 = None
closed_at = None
created_at = <Date 2008-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 = <Date 2016-05-15.01:05:47.735>
user = 'https://bugs.python.org/pbazant'

bugs.python.org fields:

activity = <Date 2016-05-15.01:05:47.735>
actor = 'martin.panter'
assignee = 'docs@python'
closed = False
closed_date = None
closer = None
components = ['Documentation', 'Extension Modules']
creation = <Date 2008-04-23.15:41:39.440>
creator = 'pbazant'
dependencies = []
files = []
hgrepos = []
issue_num = 2675
keywords = []
message_count = 6.0
messages = ['65700', '109298', '109325', '161020', '161021', '265564']
nosy_count = 5.0
nosy_names = ['pbazant', 'r.david.murray', 'docs@python', 'poq', 'martin.panter']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'needs patch'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue2675'
versions = ['Python 2.7', 'Python 3.5', 'Python 3.6']

@pbazant
Copy link
Mannequin Author

pbazant mannequin commented Apr 23, 2008

When python is in interactive mode, curses does not react to resize
events properly.

test.py:

import curses

def run():
 stdscr=curses.initscr()
 key=0
 while(key!=ord('q')):
  key=stdscr.getch()
  stdscr.addstr(0,0,str(stdscr.getmaxyx())+' '+str(key))
  stdscr.refresh()
 curses.endwin()

run()

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

import os
os.environ['LINES']="blah"
del os.environ['LINES']
os.environ['COLUMNS']="blah"
del os.environ['COLUMNS']

solves the problem for me.
Perhaps the problem has sth to do with python using readline
in interactive mode???
PB

@pbazant pbazant mannequin added the type-bug An unexpected behavior, bug, or error label Apr 23, 2008
@BreamoreBoy
Copy link
Mannequin

BreamoreBoy mannequin commented Jul 5, 2010

Could somebody witht a nix box try this out, I'd do it myself if I had such a beast.

@bitdancer
Copy link
Member

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?

@bitdancer bitdancer added the extension-modules C modules in the Modules dir label Jul 5, 2010
@poq
Copy link
Mannequin

poq mannequin commented 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
Copy link
Mannequin

poq mannequin commented May 17, 2012

bpo-3948 is almost certainly a duplicate.

@vadmium
Copy link
Member

vadmium commented 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.

@vadmium vadmium added the docs Documentation in the Doc dir label May 15, 2016
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error
Projects
Status: Bugs
Development

No branches or pull requests

2 participants