python / python/cpython

`set_term()` does not refresh `curses.LINES`, `COLS` or `COLORS`

Open
#155,976 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

extension-modules topic-curses type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug description:

curses.LINES, curses.COLS, curses.COLORS and curses.COLOR_PAIRS are copied into the module namespace when a terminal is initialized, and set_term() does not refresh them, so after a switch they describe a different terminal. curses.newwin(curses.LINES, curses.COLS) then sizes a window for the wrong screen, and a colour checked against curses.COLORS is rejected by init_pair(). screen.use() behaves the same way.

import curses, fcntl, os, struct, termios

def screen(kind, rows, cols):
    slave = os.openpty()[1]
    fcntl.ioctl(slave, termios.TIOCSWINSZ, struct.pack('HHHH', rows, cols, 0, 0))
    return curses.newterm(kind, slave, slave)

small = screen('xterm', 18, 60)              # 8 colours
curses.start_color()
big = screen('xterm-256color', 32, 120)      # 256 colours
curses.start_color()
curses.set_term(small)

print('current screen', small.stdscr.getmaxyx(), curses.ncurses_version)
print('LINES, COLS   ', (curses.LINES, curses.COLS))
print('COLORS        ', curses.COLORS)
try:
    curses.init_pair(1, curses.COLORS - 1, 0)
except ValueError as exc:
    print('init_pair     ', exc)
$ ./python repro.py
current screen (18, 60) curses.ncurses_version(major=6, minor=6, patch=20251231)
LINES, COLS    (32, 120)
COLORS         256
init_pair      Color number is greater than COLORS-1 (7).

Expected: (18, 60) and 8, the values of the screen that is current. Doc/library/curses.rst calls LINES "the height of the screen" and COLORS "the maximum number of colors the terminal can support", with nothing about them following one screen only.

set_term() does call the internal update_lines_cols(), but that helper re-reads the ncurses LINES/COLS globals, which set_term() leaves alone, and nothing copies COLORS/COLOR_PAIRS, whose C value did change.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-155994

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with curses.set_term() and update_lines_cols(), then review the documented LINES, COLS, COLORS and COLOR_PAIRS behavior in Doc/library/curses.rst. Run the supplied pseudo-terminal reproducer with two screens; done means the module values match the current screen after switching, including dimensions and color limits.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.