Skip to content

Commit 754cc77

Browse files
gh-155976: Refresh COLORS and COLOR_PAIRS in update_lines_cols
1 parent f381d16 commit 754cc77

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

Lib/test/test_curses.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3047,6 +3047,42 @@ def test_set_term(self):
30473047
self.assertIs(curses.set_term(a), b) # returns the previous one
30483048
self.assertIs(curses.set_term(b), a)
30493049

3050+
def test_set_term_refreshes_lines_cols_colors(self):
3051+
s1 = self.make_pty()
3052+
s2 = self.make_pty()
3053+
a = curses.newterm('xterm', s1, s1)
3054+
if hasattr(curses, 'start_color'):
3055+
try:
3056+
curses.start_color()
3057+
except curses.error:
3058+
pass
3059+
b = curses.newterm('xterm-256color', s2, s2)
3060+
if hasattr(curses, 'start_color'):
3061+
try:
3062+
curses.start_color()
3063+
except curses.error:
3064+
pass
3065+
3066+
curses.set_term(a)
3067+
lines_a, cols_a = curses.LINES, curses.COLS
3068+
colors_a = getattr(curses, 'COLORS', None)
3069+
3070+
curses.set_term(b)
3071+
lines_b, cols_b = curses.LINES, curses.COLS
3072+
colors_b = getattr(curses, 'COLORS', None)
3073+
3074+
self.assertEqual(curses.set_term(a), b)
3075+
self.assertEqual(curses.LINES, lines_a)
3076+
self.assertEqual(curses.COLS, cols_a)
3077+
if colors_a is not None:
3078+
self.assertEqual(curses.COLORS, colors_a)
3079+
3080+
self.assertEqual(curses.set_term(b), a)
3081+
self.assertEqual(curses.LINES, lines_b)
3082+
self.assertEqual(curses.COLS, cols_b)
3083+
if colors_b is not None:
3084+
self.assertEqual(curses.COLORS, colors_b)
3085+
30503086
def test_window_keeps_screen_alive(self):
30513087
# The standard window keeps its screen alive; dropping every other
30523088
# reference and collecting must not invalidate the window.

Modules/_cursesmodule.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7557,6 +7557,32 @@ update_lines_cols(PyObject *private_module)
75577557
goto error;
75587558
}
75597559
Py_DECREF(o);
7560+
7561+
if (curses_start_color_called) {
7562+
o = PyLong_FromUnsignedLongLong((unsigned long long)COLORS);
7563+
if (o == NULL) {
7564+
goto error;
7565+
}
7566+
if (PyDict_SetItemString(exposed_module_dict, "COLORS", o) < 0) {
7567+
goto error;
7568+
}
7569+
if (PyDict_SetItemString(private_module_dict, "COLORS", o) < 0) {
7570+
goto error;
7571+
}
7572+
Py_DECREF(o);
7573+
7574+
o = PyLong_FromUnsignedLongLong((unsigned long long)COLOR_PAIRS);
7575+
if (o == NULL) {
7576+
goto error;
7577+
}
7578+
if (PyDict_SetItemString(exposed_module_dict, "COLOR_PAIRS", o) < 0) {
7579+
goto error;
7580+
}
7581+
if (PyDict_SetItemString(private_module_dict, "COLOR_PAIRS", o) < 0) {
7582+
goto error;
7583+
}
7584+
Py_DECREF(o);
7585+
}
75607586
Py_DECREF(exposed_module);
75617587
return 1;
75627588

0 commit comments

Comments
 (0)