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
40 changes: 40 additions & 0 deletions Lib/test/test_curses.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
check_disallow_instantiation, MISSING_C_DOCSTRINGS,
gc_collect, SHORT_TIMEOUT)
from test.support.import_helper import import_module
from test.support import os_helper

# Optionally test curses module. This currently requires that the
# 'curses' resource be given on the regrtest command line using the -u
Expand Down Expand Up @@ -3324,6 +3325,45 @@ 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()

with os_helper.EnvironmentVarGuard() as env:
env['LINES'] = '25'
env['COLUMNS'] = '80'
try:
a = curses.newterm('xterm', s1, s1)
except curses.error:
self.skipTest('no xterm terminfo entry')
try:
curses.start_color()
except curses.error:
pass

env['LINES'] = '30'
env['COLUMNS'] = '100'
try:
b = curses.newterm('xterm-256color', s2, s2)
except curses.error:
self.skipTest('no xterm-256color terminfo entry')
try:
curses.start_color()
except curses.error:
pass

curses.set_term(a)
self.assertEqual((curses.LINES, curses.COLS), a.stdscr.getmaxyx())
self.assertNotEqual((curses.LINES, curses.COLS), b.stdscr.getmaxyx())
if hasattr(curses, 'COLORS'):
self.assertEqual(curses.COLORS, 8)

self.assertEqual(curses.set_term(b), a)
self.assertEqual((curses.LINES, curses.COLS), b.stdscr.getmaxyx())
self.assertNotEqual((curses.LINES, curses.COLS), a.stdscr.getmaxyx())
if hasattr(curses, 'COLORS'):
self.assertEqual(curses.COLORS, 256)

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
38 changes: 36 additions & 2 deletions Modules/_cursesmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -7890,7 +7890,11 @@ update_lines_cols(PyObject *private_module)
goto error;
}

o = PyLong_FromLong(LINES);
int lines = LINES;
if (stdscr != NULL) {
lines = getmaxy(stdscr);
}
o = PyLong_FromLong(lines);
if (o == NULL) {
goto error;
}
Expand All @@ -7902,7 +7906,11 @@ update_lines_cols(PyObject *private_module)
}
Py_DECREF(o);

o = PyLong_FromLong(COLS);
int cols = COLS;
if (stdscr != NULL) {
cols = getmaxx(stdscr);
}
o = PyLong_FromLong(cols);
if (o == NULL) {
goto error;
}
Expand All @@ -7913,6 +7921,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