Skip to content

Commit 45a353a

Browse files
gh-155976: Update LINES and COLS from stdscr in curses set_term
1 parent 7b2b20c commit 45a353a

2 files changed

Lines changed: 38 additions & 30 deletions

File tree

Lib/test/test_curses.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3325,40 +3325,40 @@ def test_set_term(self):
33253325
self.assertIs(curses.set_term(b), a)
33263326

33273327
def test_set_term_refreshes_lines_cols_colors(self):
3328+
from test.support import os_helper
33283329
s1 = self.make_pty()
33293330
s2 = self.make_pty()
3330-
a = curses.newterm('xterm', s1, s1)
3331-
if hasattr(curses, 'start_color'):
3332-
try:
3333-
curses.start_color()
3334-
except curses.error:
3335-
pass
3336-
b = curses.newterm('xterm-256color', s2, s2)
3337-
if hasattr(curses, 'start_color'):
3338-
try:
3339-
curses.start_color()
3340-
except curses.error:
3341-
pass
3331+
3332+
with os_helper.EnvironmentVarGuard() as env:
3333+
env['LINES'] = '25'
3334+
env['COLUMNS'] = '80'
3335+
a = curses.newterm('xterm', s1, s1)
3336+
if hasattr(curses, 'start_color'):
3337+
try:
3338+
curses.start_color()
3339+
except curses.error:
3340+
pass
3341+
3342+
env['LINES'] = '30'
3343+
env['COLUMNS'] = '100'
3344+
b = curses.newterm('xterm-256color', s2, s2)
3345+
if hasattr(curses, 'start_color'):
3346+
try:
3347+
curses.start_color()
3348+
except curses.error:
3349+
pass
33423350

33433351
curses.set_term(a)
3344-
lines_a, cols_a = curses.LINES, curses.COLS
3345-
colors_a = getattr(curses, 'COLORS', None)
3346-
3347-
curses.set_term(b)
3348-
lines_b, cols_b = curses.LINES, curses.COLS
3349-
colors_b = getattr(curses, 'COLORS', None)
3350-
3351-
self.assertEqual(curses.set_term(a), b)
3352-
self.assertEqual(curses.LINES, lines_a)
3353-
self.assertEqual(curses.COLS, cols_a)
3354-
if colors_a is not None:
3355-
self.assertEqual(curses.COLORS, colors_a)
3352+
self.assertEqual((curses.LINES, curses.COLS), a.stdscr.getmaxyx())
3353+
self.assertNotEqual((curses.LINES, curses.COLS), b.stdscr.getmaxyx())
3354+
if hasattr(curses, 'COLORS'):
3355+
self.assertEqual(curses.COLORS, 8)
33563356

33573357
self.assertEqual(curses.set_term(b), a)
3358-
self.assertEqual(curses.LINES, lines_b)
3359-
self.assertEqual(curses.COLS, cols_b)
3360-
if colors_b is not None:
3361-
self.assertEqual(curses.COLORS, colors_b)
3358+
self.assertEqual((curses.LINES, curses.COLS), b.stdscr.getmaxyx())
3359+
self.assertNotEqual((curses.LINES, curses.COLS), a.stdscr.getmaxyx())
3360+
if hasattr(curses, 'COLORS'):
3361+
self.assertEqual(curses.COLORS, 256)
33623362

33633363
def test_window_keeps_screen_alive(self):
33643364
# The standard window keeps its screen alive; dropping every other

Modules/_cursesmodule.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7890,7 +7890,11 @@ update_lines_cols(PyObject *private_module)
78907890
goto error;
78917891
}
78927892

7893-
o = PyLong_FromLong(LINES);
7893+
int lines = LINES;
7894+
if (stdscr != NULL) {
7895+
lines = getmaxy(stdscr);
7896+
}
7897+
o = PyLong_FromLong(lines);
78947898
if (o == NULL) {
78957899
goto error;
78967900
}
@@ -7902,7 +7906,11 @@ update_lines_cols(PyObject *private_module)
79027906
}
79037907
Py_DECREF(o);
79047908

7905-
o = PyLong_FromLong(COLS);
7909+
int cols = COLS;
7910+
if (stdscr != NULL) {
7911+
cols = getmaxx(stdscr);
7912+
}
7913+
o = PyLong_FromLong(cols);
79067914
if (o == NULL) {
79077915
goto error;
79087916
}

0 commit comments

Comments
 (0)