FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.8] [3.9] bpo-42681: Fix test_curses failures related to color pairs (GH-24089) (GH-24113) by serhiy-storchaka · Pull Request #24116 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
33 changes: 26 additions & 7 deletions Lib/test/test_curses.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,20 @@ def bad_colors(self):
def bad_pairs(self):
return (-2**31 - 1, 2**31, -2**63 - 1, 2**63, 2**64)

def test_start_color(self):
if not curses.has_colors():
self.skipTest('requires colors support')
curses.start_color()
if verbose:
print(f'COLORS = {curses.COLORS}', file=sys.stderr)
print(f'COLOR_PAIRS = {curses.COLOR_PAIRS}', file=sys.stderr)

@requires_colors
def test_color_content(self):
self.assertEqual(curses.color_content(curses.COLOR_BLACK), (0, 0, 0))
curses.color_content(0)
curses.color_content(min(curses.COLORS - 1, SHORT_MAX))
maxcolor = min(curses.COLORS - 1, SHORT_MAX)
curses.color_content(maxcolor)

for color in self.bad_colors():
self.assertRaises(OverflowError, curses.color_content, color)
Expand Down Expand Up @@ -364,13 +373,18 @@ def test_init_color(self):
self.assertRaises(curses.error, curses.init_color, 0, 0, comp, 0)
self.assertRaises(curses.error, curses.init_color, 0, 0, 0, comp)

def get_pair_limit(self):
return min(curses.COLOR_PAIRS, SHORT_MAX)

@requires_colors
def test_pair_content(self):
if not hasattr(curses, 'use_default_colors'):
self.assertEqual(curses.pair_content(0),
(curses.COLOR_WHITE, curses.COLOR_BLACK))
curses.pair_content(0)
curses.pair_content(min(curses.COLOR_PAIRS - 1, SHORT_MAX))
maxpair = self.get_pair_limit() - 1
if maxpair > 0:
curses.pair_content(maxpair)

for pair in self.bad_pairs():
self.assertRaises(OverflowError, curses.pair_content, pair)
Expand All @@ -385,11 +399,14 @@ def test_init_pair(self):
curses.init_pair(1, 0, 0)
self.assertEqual(curses.pair_content(1), (0, 0))
maxcolor = min(curses.COLORS - 1, SHORT_MAX)
curses.init_pair(1, maxcolor, maxcolor)
self.assertEqual(curses.pair_content(1), (maxcolor, maxcolor))
maxpair = min(curses.COLOR_PAIRS - 1, SHORT_MAX)
curses.init_pair(maxpair, 2, 3)
self.assertEqual(curses.pair_content(maxpair), (2, 3))
curses.init_pair(1, maxcolor, 0)
self.assertEqual(curses.pair_content(1), (maxcolor, 0))
curses.init_pair(1, 0, maxcolor)
self.assertEqual(curses.pair_content(1), (0, maxcolor))
maxpair = self.get_pair_limit() - 1
if maxpair > 1:
curses.init_pair(maxpair, 0, 0)
self.assertEqual(curses.pair_content(maxpair), (0, 0))

for pair in self.bad_pairs():
self.assertRaises(OverflowError, curses.init_pair, pair, 0, 0)
Expand Down Expand Up @@ -587,6 +604,8 @@ def test_update_lines_cols(self):
@requires_curses_func('ncurses_version')
def test_ncurses_version(self):
v = curses.ncurses_version
if verbose:
print(f'ncurses_version = {curses.ncurses_version}', flush=True)
self.assertIsInstance(v[:], tuple)
self.assertEqual(len(v), 3)
self.assertIsInstance(v[0], int)
Expand Down
26 changes: 18 additions & 8 deletions Modules/_cursesmodule.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2592,13 +2592,18 @@ _curses_color_content_impl(PyObject *module, short color_number)
PyCursesInitialised;
PyCursesInitialisedColor;

if (color_content(color_number, &r, &g, &b) != ERR)
return Py_BuildValue("(iii)", r, g, b);
else {
PyErr_SetString(PyCursesError,
"Argument 1 was out of range. Check value of COLORS.");
if (color_content(color_number, &r, &g, &b) == ERR) {
if (color_number >= COLORS) {
PyErr_SetString(PyCursesError,
"Argument 1 was out of range. Check value of COLORS.");
}
else {
PyErr_SetString(PyCursesError, "color_content() returned ERR");
}
return NULL;
}

return Py_BuildValue("(iii)", r, g, b);
}

/*[clinic input]
Expand Down Expand Up @@ -3617,9 +3622,14 @@ _curses_pair_content_impl(PyObject *module, short pair_number)
PyCursesInitialised;
PyCursesInitialisedColor;

if (pair_content(pair_number, &f, &b)==ERR) {
PyErr_SetString(PyCursesError,
"Argument 1 was out of range. (0..COLOR_PAIRS-1)");
if (pair_content(pair_number, &f, &b) == ERR) {
if (pair_number >= COLOR_PAIRS) {
PyErr_SetString(PyCursesError,
"Argument 1 was out of range. (0..COLOR_PAIRS-1)");
}
else {
PyErr_SetString(PyCursesError, "pair_content() returned ERR");
}
return NULL;
}

Expand Down

Back | FazBrowse Home | New Git URL