diff --git a/Lib/test/test_curses.py b/Lib/test/test_curses.py index 5db44ebedd2ee0..6bea7fe116aaf8 100644 --- a/Lib/test/test_curses.py +++ b/Lib/test/test_curses.py @@ -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 @@ -3353,6 +3354,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. diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 401b777be23caa..00f614ce7f1f45 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -7930,7 +7930,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; } @@ -7942,7 +7946,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; } @@ -7953,6 +7961,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;