Python 诅咒 init_pair 实现与文档不匹配

Python Curses init_pair implementation not matching the documentation

我一直在尝试使用 Curses 颜色,运行 遇到了一些问题。

正如 init_pair 的文档所述,第一个参数(对数)应介于 1curses.COLOR_PAIRS - 1 之间。执行 print(curses.COLOR, curses.COLOR_PAIR) 会产生 256 65536,因此有人会认为调用 courses.init_pair(40000, 1, 53)(运行dom 示例)会起作用,但我得到一个错误:

Traceback (most recent call last):
  File "4-colors.py", line 38, in <module>
    curses.wrapper(main)
  File "/usr/lib/python3.8/curses/__init__.py", line 105, in wrapper
    return func(stdscr, *args, **kwds)
  File "4-colors.py", line 18, in main
    curses.init_pair(40000, 1, 53)
OverflowError: signed short integer is greater than maximum

果然,the implementation of init_color(我希望我正在查看正确的文件)检查颜色对编号是否在签名短的范围内。

为什么?有没有办法绕过这个并使用我的所有颜色 终端,不只是任意一半?


MWE 的完整源代码:

import curses

def main(window):
    curses.start_color()
    curses.use_default_colors()

    curses.init_pair(40000, 1, 53)

curses.wrapper(main)

问题是 Python 的 curses 绑定没有更新以说明 ncurses 6.1 中所做的更改:在那之前,能力值 (numbers) 通过 to/from curses 库限制为带符号的 16 位数字。 ncurses 6.1 扩展了,虽然要有效地使用像 COLOR_PAIRS 这样的东西,有必要使用 ncurses 6.1

中引入的功能扩展之一

在给定的例子中,例如,而不是调用init_pair,其C原型是

int init_pair(short pair, short f, short b);

有人会用 init_extended_pair:

int init_extended_pair(int pair, int f, int b);

当然,总是 应用程序负责验证它们处理的数字。显示的示例说明了 Python 运行时异常(而不是限制调用者看到的颜色对的数量)。