python / python/cpython

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

未關閉
#155,976 2 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

extension-modules topic-curses type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

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

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 curses.set_term() 和 update_lines_cols() 開始,接著檢視 Doc/library/curses.rst 中記載的 LINES、COLS、COLORS 和 COLOR_PAIRS 行為。使用兩個畫面執行提供的偽終端重現程式;完成標準是切換後模組值與目前畫面相符,包括尺寸和色彩限制。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
cli
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
描述清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。