bpo-42681: Fix test_curses failures related to color pairs (GH-24089) · python/cpython@59f9b4e

@@ -47,6 +47,7 @@ def wrapped(self, *args, **kwargs):

4747

return wrapped

48484949

term = os.environ.get('TERM')

50+

SHORT_MAX = 0x7fff

50515152

# If newterm was supported we could use it instead of initscr and not exit

5253

@unittest.skipIf(not term or term == 'unknown',

@@ -327,11 +328,20 @@ def bad_colors2(self):

327328

def bad_pairs(self):

328329

return (-1, -2**31 - 1, 2**31, -2**63 - 1, 2**63, 2**64)

329330331+

def test_start_color(self):

332+

if not curses.has_colors():

333+

self.skipTest('requires colors support')

334+

curses.start_color()

335+

if verbose:

336+

print(f'COLORS = {curses.COLORS}', file=sys.stderr)

337+

print(f'COLOR_PAIRS = {curses.COLOR_PAIRS}', file=sys.stderr)

338+330339

@requires_colors

331340

def test_color_content(self):

332341

self.assertEqual(curses.color_content(curses.COLOR_BLACK), (0, 0, 0))

333342

curses.color_content(0)

334-

curses.color_content(curses.COLORS - 1)

343+

maxcolor = curses.COLORS - 1

344+

curses.color_content(maxcolor)

335345336346

for color in self.bad_colors():

337347

self.assertRaises(ValueError, curses.color_content, color)

@@ -352,11 +362,12 @@ def test_init_color(self):

352362

curses.init_color(0, 1000, 1000, 1000)

353363

self.assertEqual(curses.color_content(0), (1000, 1000, 1000))

354364355-

old = curses.color_content(curses.COLORS - 1)

356-

curses.init_color(curses.COLORS - 1, *old)

357-

self.addCleanup(curses.init_color, curses.COLORS - 1, *old)

358-

curses.init_color(curses.COLORS - 1, 0, 500, 1000)

359-

self.assertEqual(curses.color_content(curses.COLORS - 1), (0, 500, 1000))

365+

maxcolor = curses.COLORS - 1

366+

old = curses.color_content(maxcolor)

367+

curses.init_color(maxcolor, *old)

368+

self.addCleanup(curses.init_color, maxcolor, *old)

369+

curses.init_color(maxcolor, 0, 500, 1000)

370+

self.assertEqual(curses.color_content(maxcolor), (0, 500, 1000))

360371361372

for color in self.bad_colors():

362373

self.assertRaises(ValueError, curses.init_color, color, 0, 0, 0)

@@ -365,13 +376,25 @@ def test_init_color(self):

365376

self.assertRaises(ValueError, curses.init_color, 0, 0, comp, 0)

366377

self.assertRaises(ValueError, curses.init_color, 0, 0, 0, comp)

367378379+

def get_pair_limit(self):

380+

pair_limit = curses.COLOR_PAIRS

381+

if hasattr(curses, 'ncurses_version'):

382+

if curses.has_extended_color_support():

383+

pair_limit += 2*curses.COLORS + 1

384+

if (not curses.has_extended_color_support()

385+

or (6, 1) <= curses.ncurses_version < (6, 2)):

386+

pair_limit = min(pair_limit, SHORT_MAX)

387+

return pair_limit

388+368389

@requires_colors

369390

def test_pair_content(self):

370391

if not hasattr(curses, 'use_default_colors'):

371392

self.assertEqual(curses.pair_content(0),

372393

(curses.COLOR_WHITE, curses.COLOR_BLACK))

373394

curses.pair_content(0)

374-

curses.pair_content(curses.COLOR_PAIRS - 1)

395+

maxpair = self.get_pair_limit() - 1

396+

if maxpair > 0:

397+

curses.pair_content(maxpair)

375398376399

for pair in self.bad_pairs():

377400

self.assertRaises(ValueError, curses.pair_content, pair)

@@ -384,11 +407,15 @@ def test_init_pair(self):

384407385408

curses.init_pair(1, 0, 0)

386409

self.assertEqual(curses.pair_content(1), (0, 0))

387-

curses.init_pair(1, curses.COLORS - 1, curses.COLORS - 1)

388-

self.assertEqual(curses.pair_content(1),

389-

(curses.COLORS - 1, curses.COLORS - 1))

390-

curses.init_pair(curses.COLOR_PAIRS - 1, 2, 3)

391-

self.assertEqual(curses.pair_content(curses.COLOR_PAIRS - 1), (2, 3))

410+

maxcolor = curses.COLORS - 1

411+

curses.init_pair(1, maxcolor, 0)

412+

self.assertEqual(curses.pair_content(1), (maxcolor, 0))

413+

curses.init_pair(1, 0, maxcolor)

414+

self.assertEqual(curses.pair_content(1), (0, maxcolor))

415+

maxpair = self.get_pair_limit() - 1

416+

if maxpair > 1:

417+

curses.init_pair(maxpair, 0, 0)

418+

self.assertEqual(curses.pair_content(maxpair), (0, 0))

392419393420

for pair in self.bad_pairs():

394421

self.assertRaises(ValueError, curses.init_pair, pair, 0, 0)

@@ -582,6 +609,8 @@ def test_update_lines_cols(self):

582609

@requires_curses_func('ncurses_version')

583610

def test_ncurses_version(self):

584611

v = curses.ncurses_version

612+

if verbose:

613+

print(f'ncurses_version = {curses.ncurses_version}', flush=True)

585614

self.assertIsInstance(v[:], tuple)

586615

self.assertEqual(len(v), 3)

587616

self.assertIsInstance(v[0], int)