Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Lib/test/test_curses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3047,6 +3047,42 @@ def test_set_term(self):
self.assertIs(curses.set_term(a), b) # returns the previous one
self.assertIs(curses.set_term(b), a)

def test_set_term_refreshes_lines_cols_colors(self):
s1 = self.make_pty()
s2 = self.make_pty()
a = curses.newterm('xterm', s1, s1)
if hasattr(curses, 'start_color'):
try:
curses.start_color()
except curses.error:
pass
b = curses.newterm('xterm-256color', s2, s2)
if hasattr(curses, 'start_color'):
try:
curses.start_color()
except curses.error:
pass

curses.set_term(a)
lines_a, cols_a = curses.LINES, curses.COLS
colors_a = getattr(curses, 'COLORS', None)

curses.set_term(b)
lines_b, cols_b = curses.LINES, curses.COLS
colors_b = getattr(curses, 'COLORS', None)

self.assertEqual(curses.set_term(a), b)
self.assertEqual(curses.LINES, lines_a)
self.assertEqual(curses.COLS, cols_a)
if colors_a is not None:
self.assertEqual(curses.COLORS, colors_a)

self.assertEqual(curses.set_term(b), a)
self.assertEqual(curses.LINES, lines_b)
self.assertEqual(curses.COLS, cols_b)
if colors_b is not None:
self.assertEqual(curses.COLORS, colors_b)

def test_window_keeps_screen_alive(self):
# The standard window keeps its screen alive; dropping every other
# reference and collecting must not invalidate the window.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix an issue where :func:`curses.set_term` did not update :const:`curses.COLORS` and :const:`curses.COLOR_PAIRS` when switching terminal screens.
26 changes: 26 additions & 0 deletions Modules/_cursesmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -7557,6 +7557,32 @@ update_lines_cols(PyObject *private_module)
goto error;
}
Py_DECREF(o);

if (curses_start_color_called) {
o = PyLong_FromUnsignedLongLong((unsigned long long)COLORS);
if (o == NULL) {
goto error;
}
if (PyDict_SetItemString(exposed_module_dict, "COLORS", o) < 0) {
goto error;
}
if (PyDict_SetItemString(private_module_dict, "COLORS", o) < 0) {
goto error;
}
Py_DECREF(o);

o = PyLong_FromUnsignedLongLong((unsigned long long)COLOR_PAIRS);
if (o == NULL) {
goto error;
}
if (PyDict_SetItemString(exposed_module_dict, "COLOR_PAIRS", o) < 0) {
goto error;
}
if (PyDict_SetItemString(private_module_dict, "COLOR_PAIRS", o) < 0) {
goto error;
}
Py_DECREF(o);
}
Py_DECREF(exposed_module);
return 1;

Expand Down
Loading