| 1 |
"""Tests comparing PyREPL's pure Python curses implementation with the standard curses module.""" |
| 2 |
|
| 3 |
import json |
| 4 |
import os |
| 5 |
import subprocess |
| 6 |
import sys |
| 7 |
import unittest |
| 8 |
from test.support import requires, has_subprocess_support |
| 9 |
from textwrap import dedent |
| 10 |
|
| 11 |
# Only run these tests if curses is available |
| 12 |
requires("curses") |
| 13 |
|
| 14 |
try: |
| 15 |
import _curses |
| 16 |
except ImportError: |
| 17 |
try: |
| 18 |
import curses as _curses |
| 19 |
except ImportError: |
| 20 |
_curses = None |
| 21 |
|
| 22 |
from _pyrepl import terminfo |
| 23 |
|
| 24 |
|
| 25 |
ABSENT_STRING = terminfo.ABSENT_STRING |
| 26 |
CANCELLED_STRING = terminfo.CANCELLED_STRING |
| 27 |
|
| 28 |
|
| 29 |
class TestCursesCompatibility(unittest.TestCase): |
| 30 |
"""Test that PyREPL's curses implementation matches the standard curses behavior. |
| 31 |
|
| 32 |
Python's `curses` doesn't allow calling `setupterm()` again with a different |
| 33 |
$TERM in the same process, so we subprocess all `curses` tests to get correctly |
| 34 |
set up terminfo.""" |
| 35 |
|
| 36 |
@classmethod |
| 37 |
def setUpClass(cls): |
| 38 |
if _curses is None: |
| 39 |
raise unittest.SkipTest( |
| 40 |
"`curses` capability provided to regrtest but `_curses` not importable" |
| 41 |
) |
| 42 |
|
| 43 |
if not has_subprocess_support: |
| 44 |
raise unittest.SkipTest("test module requires subprocess") |
| 45 |
|
| 46 |
# we need to ensure there's a terminfo database on the system and that |
| 47 |
# `infocmp` works |
| 48 |
cls.infocmp("dumb") |
| 49 |
|
| 50 |
def setUp(self): |
| 51 |
self.original_term = os.environ.get("TERM", None) |
| 52 |
|
| 53 |
def tearDown(self): |
| 54 |
if self.original_term is not None: |
| 55 |
os.environ["TERM"] = self.original_term |
| 56 |
elif "TERM" in os.environ: |
| 57 |
del os.environ["TERM"] |
| 58 |
|
| 59 |
@classmethod |
| 60 |
def infocmp(cls, term) -> list[str]: |
| 61 |
all_caps = [] |
| 62 |
try: |
| 63 |
result = subprocess.run( |
| 64 |
["infocmp", "-l1", term], |
| 65 |
capture_output=True, |
| 66 |
text=True, |
| 67 |
check=True, |
| 68 |
) |
| 69 |
except Exception: |
| 70 |
raise unittest.SkipTest("calling `infocmp` failed on the system") |
| 71 |
|
| 72 |
for line in result.stdout.splitlines(): |
| 73 |
line = line.strip() |
| 74 |
if line.startswith("#"): |
| 75 |
if "terminfo" not in line and "termcap" in line: |
| 76 |
# PyREPL terminfo doesn't parse termcap databases |
| 77 |
raise unittest.SkipTest( |
| 78 |
"curses using termcap.db: no terminfo database on" |
| 79 |
" the system" |
| 80 |
) |
| 81 |
elif "=" in line: |
| 82 |
cap_name = line.split("=")[0] |
| 83 |
all_caps.append(cap_name) |
| 84 |
|
| 85 |
return all_caps |
| 86 |
|
| 87 |
def test_setupterm_basic(self): |
| 88 |
"""Test basic setupterm functionality.""" |
| 89 |
# Test with explicit terminal type |
| 90 |
test_terms = ["xterm", "xterm-256color", "vt100", "ansi"] |
| 91 |
|
| 92 |
for term in test_terms: |
| 93 |
with self.subTest(term=term): |
| 94 |
ncurses_code = dedent( |
| 95 |
f""" |
| 96 |
import _curses |
| 97 |
import json |
| 98 |
try: |
| 99 |
_curses.setupterm({repr(term)}, 1) |
| 100 |
print(json.dumps({{"success": True}})) |
| 101 |
except Exception as e: |
| 102 |
print(json.dumps({{"success": False, "error": str(e)}})) |
| 103 |
""" |
| 104 |
) |
| 105 |
|
| 106 |
result = subprocess.run( |
| 107 |
[sys.executable, "-c", ncurses_code], |
| 108 |
capture_output=True, |
| 109 |
text=True, |
| 110 |
) |
| 111 |
ncurses_data = json.loads(result.stdout) |
| 112 |
std_success = ncurses_data["success"] |
| 113 |
|
| 114 |
# Set up with PyREPL curses |
| 115 |
try: |
| 116 |
terminfo.TermInfo(term, fallback=False) |
| 117 |
pyrepl_success = True |
| 118 |
except Exception as e: |
| 119 |
pyrepl_success = False |
| 120 |
pyrepl_error = e |
| 121 |
|
| 122 |
# Both should succeed or both should fail |
| 123 |
if std_success: |
| 124 |
self.assertTrue( |
| 125 |
pyrepl_success, |
| 126 |
f"Standard curses succeeded but PyREPL failed for {term}", |
| 127 |
) |
| 128 |
else: |
| 129 |
# If standard curses failed, PyREPL might still succeed with fallback |
| 130 |
# This is acceptable as PyREPL has hardcoded fallbacks |
| 131 |
pass |
| 132 |
|
| 133 |
def test_setupterm_none(self): |
| 134 |
"""Test setupterm with None (uses TERM from environment).""" |
| 135 |
# Test with current TERM |
| 136 |
ncurses_code = dedent( |
| 137 |
""" |
| 138 |
import _curses |
| 139 |
import json |
| 140 |
try: |
| 141 |
_curses.setupterm(None, 1) |
| 142 |
print(json.dumps({"success": True})) |
| 143 |
except Exception as e: |
| 144 |
print(json.dumps({"success": False, "error": str(e)})) |
| 145 |
""" |
| 146 |
) |
| 147 |
|
| 148 |
result = subprocess.run( |
| 149 |
[sys.executable, "-c", ncurses_code], |
| 150 |
capture_output=True, |
| 151 |
text=True, |
| 152 |
) |
| 153 |
ncurses_data = json.loads(result.stdout) |
| 154 |
std_success = ncurses_data["success"] |
| 155 |
|
| 156 |
try: |
| 157 |
terminfo.TermInfo(None, fallback=False) |
| 158 |
pyrepl_success = True |
| 159 |
except Exception: |
| 160 |
pyrepl_success = False |
| 161 |
|
| 162 |
# Both should have same result |
| 163 |
if std_success: |
| 164 |
self.assertTrue( |
| 165 |
pyrepl_success, |
| 166 |
"Standard curses succeeded but PyREPL failed for None", |
| 167 |
) |
| 168 |
|
| 169 |
def test_tigetstr_common_capabilities(self): |
| 170 |
"""Test tigetstr for common terminal capabilities.""" |
| 171 |
# Test with a known terminal type |
| 172 |
term = "xterm" |
| 173 |
|
| 174 |
# Get ALL capabilities from infocmp |
| 175 |
all_caps = self.infocmp(term) |
| 176 |
|
| 177 |
ncurses_code = dedent( |
| 178 |
f""" |
| 179 |
import _curses |
| 180 |
import json |
| 181 |
_curses.setupterm({repr(term)}, 1) |
| 182 |
results = {{}} |
| 183 |
for cap in {repr(all_caps)}: |
| 184 |
try: |
| 185 |
val = _curses.tigetstr(cap) |
| 186 |
if val is None: |
| 187 |
results[cap] = None |
| 188 |
elif val == -1: |
| 189 |
results[cap] = -1 |
| 190 |
else: |
| 191 |
results[cap] = list(val) |
| 192 |
except BaseException: |
| 193 |
results[cap] = "error" |
| 194 |
print(json.dumps(results)) |
| 195 |
""" |
| 196 |
) |
| 197 |
|
| 198 |
result = subprocess.run( |
| 199 |
[sys.executable, "-c", ncurses_code], |
| 200 |
capture_output=True, |
| 201 |
text=True, |
| 202 |
) |
| 203 |
self.assertEqual( |
| 204 |
result.returncode, 0, f"Failed to run ncurses: {result.stderr}" |
| 205 |
) |
| 206 |
|
| 207 |
ncurses_data = json.loads(result.stdout) |
| 208 |
|
| 209 |
ti = terminfo.TermInfo(term, fallback=False) |
| 210 |
|
| 211 |
# Test every single capability |
| 212 |
for cap in all_caps: |
| 213 |
if cap not in ncurses_data or ncurses_data[cap] == "error": |
| 214 |
continue |
| 215 |
|
| 216 |
with self.subTest(capability=cap): |
| 217 |
ncurses_val = ncurses_data[cap] |
| 218 |
if isinstance(ncurses_val, list): |
| 219 |
ncurses_val = bytes(ncurses_val) |
| 220 |
|
| 221 |
pyrepl_val = ti.get(cap) |
| 222 |
|
| 223 |
self.assertEqual( |
| 224 |
pyrepl_val, |
| 225 |
ncurses_val, |
| 226 |
f"Capability {cap}: ncurses={repr(ncurses_val)}, " |
| 227 |
f"pyrepl={repr(pyrepl_val)}", |
| 228 |
) |
| 229 |
|
| 230 |
def test_tigetstr_input_types(self): |
| 231 |
"""Test tigetstr with different input types.""" |
| 232 |
term = "xterm" |
| 233 |
cap = "cup" |
| 234 |
|
| 235 |
# Test standard curses behavior with string in subprocess |
| 236 |
ncurses_code = dedent( |
| 237 |
f""" |
| 238 |
import _curses |
| 239 |
import json |
| 240 |
_curses.setupterm({repr(term)}, 1) |
| 241 |
|
| 242 |
# Test with string input |
| 243 |
try: |
| 244 |
std_str_result = _curses.tigetstr({repr(cap)}) |
| 245 |
std_accepts_str = True |
| 246 |
if std_str_result is None: |
| 247 |
std_str_val = None |
| 248 |
elif std_str_result == -1: |
| 249 |
std_str_val = -1 |
| 250 |
else: |
| 251 |
std_str_val = list(std_str_result) |
| 252 |
except TypeError: |
| 253 |
std_accepts_str = False |
| 254 |
std_str_val = None |
| 255 |
|
| 256 |
print(json.dumps({{ |
| 257 |
"accepts_str": std_accepts_str, |
| 258 |
"str_result": std_str_val |
| 259 |
}})) |
| 260 |
""" |
| 261 |
) |
| 262 |
|
| 263 |
result = subprocess.run( |
| 264 |
[sys.executable, "-c", ncurses_code], |
| 265 |
capture_output=True, |
| 266 |
text=True, |
| 267 |
) |
| 268 |
ncurses_data = json.loads(result.stdout) |
| 269 |
|
| 270 |
# PyREPL setup |
| 271 |
ti = terminfo.TermInfo(term, fallback=False) |
| 272 |
|
| 273 |
# PyREPL behavior with string |
| 274 |
try: |
| 275 |
pyrepl_str_result = ti.get(cap) |
| 276 |
pyrepl_accepts_str = True |
| 277 |
except TypeError: |
| 278 |
pyrepl_accepts_str = False |
| 279 |
|
| 280 |
# PyREPL should also only accept strings for compatibility |
| 281 |
with self.assertRaises(TypeError): |
| 282 |
ti.get(cap.encode("ascii")) |
| 283 |
|
| 284 |
# Both should accept string input |
| 285 |
self.assertEqual( |
| 286 |
pyrepl_accepts_str, |
| 287 |
ncurses_data["accepts_str"], |
| 288 |
"PyREPL and standard curses should have same string handling", |
| 289 |
) |
| 290 |
self.assertTrue( |
| 291 |
pyrepl_accepts_str, "PyREPL should accept string input" |
| 292 |
) |
| 293 |
|
| 294 |
def test_tparm_basic(self): |
| 295 |
"""Test basic tparm functionality.""" |
| 296 |
term = "xterm" |
| 297 |
ti = terminfo.TermInfo(term, fallback=False) |
| 298 |
|
| 299 |
# Test cursor positioning (cup) |
| 300 |
cup = ti.get("cup") |
| 301 |
if cup and cup not in {ABSENT_STRING, CANCELLED_STRING}: |
| 302 |
# Test various parameter combinations |
| 303 |
test_cases = [ |
| 304 |
(0, 0), # Top-left |
| 305 |
(5, 10), # Arbitrary position |
| 306 |
(23, 79), # Bottom-right of standard terminal |
| 307 |
(999, 999), # Large values |
| 308 |
] |
| 309 |
|
| 310 |
# Get ncurses results in subprocess |
| 311 |
ncurses_code = dedent( |
| 312 |
f""" |
| 313 |
import _curses |
| 314 |
import json |
| 315 |
_curses.setupterm({repr(term)}, 1) |
| 316 |
|
| 317 |
# Get cup capability |
| 318 |
cup = _curses.tigetstr('cup') |
| 319 |
results = {{}} |
| 320 |
|
| 321 |
for row, col in {repr(test_cases)}: |
| 322 |
try: |
| 323 |
result = _curses.tparm(cup, row, col) |
| 324 |
results[f"{{row}},{{col}}"] = list(result) |
| 325 |
except Exception as e: |
| 326 |
results[f"{{row}},{{col}}"] = {{"error": str(e)}} |
| 327 |
|
| 328 |
print(json.dumps(results)) |
| 329 |
""" |
| 330 |
) |
| 331 |
|
| 332 |
result = subprocess.run( |
| 333 |
[sys.executable, "-c", ncurses_code], |
| 334 |
capture_output=True, |
| 335 |
text=True, |
| 336 |
) |
| 337 |
self.assertEqual( |
| 338 |
result.returncode, 0, f"Failed to run ncurses: {result.stderr}" |
| 339 |
) |
| 340 |
ncurses_data = json.loads(result.stdout) |
| 341 |
|
| 342 |
for row, col in test_cases: |
| 343 |
with self.subTest(row=row, col=col): |
| 344 |
# Standard curses tparm from subprocess |
| 345 |
key = f"{row},{col}" |
| 346 |
if ( |
| 347 |
isinstance(ncurses_data[key], dict) |
| 348 |
and "error" in ncurses_data[key] |
| 349 |
): |
| 350 |
self.fail( |
| 351 |
f"ncurses tparm failed: {ncurses_data[key]['error']}" |
| 352 |
) |
| 353 |
std_result = bytes(ncurses_data[key]) |
| 354 |
|
| 355 |
# PyREPL curses tparm |
| 356 |
pyrepl_result = terminfo.tparm(cup, row, col) |
| 357 |
|
| 358 |
# Results should be identical |
| 359 |
self.assertEqual( |
| 360 |
pyrepl_result, |
| 361 |
std_result, |
| 362 |
f"tparm(cup, {row}, {col}): " |
| 363 |
f"std={repr(std_result)}, pyrepl={repr(pyrepl_result)}", |
| 364 |
) |
| 365 |
else: |
| 366 |
raise unittest.SkipTest( |
| 367 |
"test_tparm_basic() requires the `cup` capability" |
| 368 |
) |
| 369 |
|
| 370 |
def test_tparm_multiple_params(self): |
| 371 |
"""Test tparm with capabilities using multiple parameters.""" |
| 372 |
term = "xterm" |
| 373 |
ti = terminfo.TermInfo(term, fallback=False) |
| 374 |
|
| 375 |
# Test capabilities that take parameters |
| 376 |
param_caps = { |
| 377 |
"cub": 1, # cursor_left with count |
| 378 |
"cuf": 1, # cursor_right with count |
| 379 |
"cuu": 1, # cursor_up with count |
| 380 |
"cud": 1, # cursor_down with count |
| 381 |
"dch": 1, # delete_character with count |
| 382 |
"ich": 1, # insert_character with count |
| 383 |
} |
| 384 |
|
| 385 |
# Get all capabilities from PyREPL first |
| 386 |
pyrepl_caps = {} |
| 387 |
for cap in param_caps: |
| 388 |
cap_value = ti.get(cap) |
| 389 |
if cap_value and cap_value not in { |
| 390 |
ABSENT_STRING, |
| 391 |
CANCELLED_STRING, |
| 392 |
}: |
| 393 |
pyrepl_caps[cap] = cap_value |
| 394 |
|
| 395 |
if not pyrepl_caps: |
| 396 |
self.skipTest("No parametrized capabilities found") |
| 397 |
|
| 398 |
# Get ncurses results in subprocess |
| 399 |
ncurses_code = dedent( |
| 400 |
f""" |
| 401 |
import _curses |
| 402 |
import json |
| 403 |
_curses.setupterm({repr(term)}, 1) |
| 404 |
|
| 405 |
param_caps = {repr(param_caps)} |
| 406 |
test_values = [1, 5, 10, 99] |
| 407 |
results = {{}} |
| 408 |
|
| 409 |
for cap in param_caps: |
| 410 |
cap_value = _curses.tigetstr(cap) |
| 411 |
if cap_value and cap_value != -1: |
| 412 |
for value in test_values: |
| 413 |
try: |
| 414 |
result = _curses.tparm(cap_value, value) |
| 415 |
results[f"{{cap}},{{value}}"] = list(result) |
| 416 |
except Exception as e: |
| 417 |
results[f"{{cap}},{{value}}"] = {{"error": str(e)}} |
| 418 |
|
| 419 |
print(json.dumps(results)) |
| 420 |
""" |
| 421 |
) |
| 422 |
|
| 423 |
result = subprocess.run( |
| 424 |
[sys.executable, "-c", ncurses_code], |
| 425 |
capture_output=True, |
| 426 |
text=True, |
| 427 |
) |
| 428 |
self.assertEqual( |
| 429 |
result.returncode, 0, f"Failed to run ncurses: {result.stderr}" |
| 430 |
) |
| 431 |
ncurses_data = json.loads(result.stdout) |
| 432 |
|
| 433 |
for cap, cap_value in pyrepl_caps.items(): |
| 434 |
with self.subTest(capability=cap): |
| 435 |
# Test with different parameter values |
| 436 |
for value in [1, 5, 10, 99]: |
| 437 |
key = f"{cap},{value}" |
| 438 |
if key in ncurses_data: |
| 439 |
if ( |
| 440 |
isinstance(ncurses_data[key], dict) |
| 441 |
and "error" in ncurses_data[key] |
| 442 |
): |
| 443 |
self.fail( |
| 444 |
f"ncurses tparm failed: {ncurses_data[key]['error']}" |
| 445 |
) |
| 446 |
std_result = bytes(ncurses_data[key]) |
| 447 |
|
| 448 |
pyrepl_result = terminfo.tparm(cap_value, value) |
| 449 |
self.assertEqual( |
| 450 |
pyrepl_result, |
| 451 |
std_result, |
| 452 |
f"tparm({cap}, {value}): " |
| 453 |
f"std={repr(std_result)}, pyrepl={repr(pyrepl_result)}", |
| 454 |
) |
| 455 |
|
| 456 |
def test_tparm_null_handling(self): |
| 457 |
"""Test tparm with None/null input.""" |
| 458 |
term = "xterm" |
| 459 |
|
| 460 |
ncurses_code = dedent( |
| 461 |
f""" |
| 462 |
import _curses |
| 463 |
import json |
| 464 |
_curses.setupterm({repr(term)}, 1) |
| 465 |
|
| 466 |
# Test with None |
| 467 |
try: |
| 468 |
_curses.tparm(None) |
| 469 |
raises_typeerror = False |
| 470 |
except TypeError: |
| 471 |
raises_typeerror = True |
| 472 |
except Exception as e: |
| 473 |
raises_typeerror = False |
| 474 |
error_type = type(e).__name__ |
| 475 |
|
| 476 |
print(json.dumps({{"raises_typeerror": raises_typeerror}})) |
| 477 |
""" |
| 478 |
) |
| 479 |
|
| 480 |
result = subprocess.run( |
| 481 |
[sys.executable, "-c", ncurses_code], |
| 482 |
capture_output=True, |
| 483 |
text=True, |
| 484 |
) |
| 485 |
ncurses_data = json.loads(result.stdout) |
| 486 |
|
| 487 |
# PyREPL setup |
| 488 |
ti = terminfo.TermInfo(term, fallback=False) |
| 489 |
|
| 490 |
# Test with None - both should raise TypeError |
| 491 |
if ncurses_data["raises_typeerror"]: |
| 492 |
with self.assertRaises(TypeError): |
| 493 |
terminfo.tparm(None) |
| 494 |
else: |
| 495 |
# If ncurses doesn't raise TypeError, PyREPL shouldn't either |
| 496 |
try: |
| 497 |
terminfo.tparm(None) |
| 498 |
except TypeError: |
| 499 |
self.fail("PyREPL raised TypeError but ncurses did not") |
| 500 |
|
| 501 |
def test_special_terminals(self): |
| 502 |
"""Test with special terminal types.""" |
| 503 |
special_terms = [ |
| 504 |
"dumb", # Minimal terminal |
| 505 |
"unknown", # Should fall back to defaults |
| 506 |
"linux", # Linux console |
| 507 |
"screen", # GNU Screen |
| 508 |
"tmux", # tmux |
| 509 |
] |
| 510 |
|
| 511 |
# Get all string capabilities from ncurses |
| 512 |
for term in special_terms: |
| 513 |
with self.subTest(term=term): |
| 514 |
all_caps = self.infocmp(term) |
| 515 |
ncurses_code = dedent( |
| 516 |
f""" |
| 517 |
import _curses |
| 518 |
import json |
| 519 |
import sys |
| 520 |
|
| 521 |
try: |
| 522 |
_curses.setupterm({repr(term)}, 1) |
| 523 |
results = {{}} |
| 524 |
for cap in {repr(all_caps)}: |
| 525 |
try: |
| 526 |
val = _curses.tigetstr(cap) |
| 527 |
if val is None: |
| 528 |
results[cap] = None |
| 529 |
elif val == -1: |
| 530 |
results[cap] = -1 |
| 531 |
else: |
| 532 |
# Convert bytes to list of ints for JSON |
| 533 |
results[cap] = list(val) |
| 534 |
except BaseException: |
| 535 |
results[cap] = "error" |
| 536 |
print(json.dumps(results)) |
| 537 |
except Exception as e: |
| 538 |
print(json.dumps({{"error": str(e)}})) |
| 539 |
""" |
| 540 |
) |
| 541 |
|
| 542 |
# Get ncurses results |
| 543 |
result = subprocess.run( |
| 544 |
[sys.executable, "-c", ncurses_code], |
| 545 |
capture_output=True, |
| 546 |
text=True, |
| 547 |
) |
| 548 |
if result.returncode != 0: |
| 549 |
self.fail( |
| 550 |
f"Failed to get ncurses data for {term}: {result.stderr}" |
| 551 |
) |
| 552 |
|
| 553 |
try: |
| 554 |
ncurses_data = json.loads(result.stdout) |
| 555 |
except json.JSONDecodeError: |
| 556 |
self.fail( |
| 557 |
f"Failed to parse ncurses output for {term}: {result.stdout}" |
| 558 |
) |
| 559 |
|
| 560 |
if "error" in ncurses_data and len(ncurses_data) == 1: |
| 561 |
# ncurses failed to setup this terminal |
| 562 |
# PyREPL should still work with fallback |
| 563 |
ti = terminfo.TermInfo(term, fallback=True) |
| 564 |
continue |
| 565 |
|
| 566 |
ti = terminfo.TermInfo(term, fallback=False) |
| 567 |
|
| 568 |
# Compare all capabilities |
| 569 |
for cap in all_caps: |
| 570 |
if cap not in ncurses_data: |
| 571 |
continue |
| 572 |
|
| 573 |
with self.subTest(term=term, capability=cap): |
| 574 |
ncurses_val = ncurses_data[cap] |
| 575 |
if isinstance(ncurses_val, list): |
| 576 |
# Convert back to bytes |
| 577 |
ncurses_val = bytes(ncurses_val) |
| 578 |
|
| 579 |
pyrepl_val = ti.get(cap) |
| 580 |
|
| 581 |
# Both should return the same value |
| 582 |
self.assertEqual( |
| 583 |
pyrepl_val, |
| 584 |
ncurses_val, |
| 585 |
f"Capability {cap} for {term}: " |
| 586 |
f"ncurses={repr(ncurses_val)}, " |
| 587 |
f"pyrepl={repr(pyrepl_val)}", |
| 588 |
) |
| 589 |
|
| 590 |
def test_terminfo_fallback(self): |
| 591 |
"""Test that PyREPL falls back gracefully when terminfo is not found.""" |
| 592 |
# Use a non-existent terminal type |
| 593 |
fake_term = "nonexistent-terminal-type-12345" |
| 594 |
|
| 595 |
# Check if standard curses can setup this terminal in subprocess |
| 596 |
ncurses_code = dedent( |
| 597 |
f""" |
| 598 |
import _curses |
| 599 |
import json |
| 600 |
try: |
| 601 |
_curses.setupterm({repr(fake_term)}, 1) |
| 602 |
print(json.dumps({{"success": True}})) |
| 603 |
except _curses.error: |
| 604 |
print(json.dumps({{"success": False, "error": "curses.error"}})) |
| 605 |
except Exception as e: |
| 606 |
print(json.dumps({{"success": False, "error": str(e)}})) |
| 607 |
""" |
| 608 |
) |
| 609 |
|
| 610 |
result = subprocess.run( |
| 611 |
[sys.executable, "-c", ncurses_code], |
| 612 |
capture_output=True, |
| 613 |
text=True, |
| 614 |
) |
| 615 |
ncurses_data = json.loads(result.stdout) |
| 616 |
|
| 617 |
if ncurses_data["success"]: |
| 618 |
# If it succeeded, skip this test as we can't test fallback |
| 619 |
self.skipTest( |
| 620 |
f"System unexpectedly has terminfo for '{fake_term}'" |
| 621 |
) |
| 622 |
|
| 623 |
# PyREPL should succeed with fallback |
| 624 |
try: |
| 625 |
ti = terminfo.TermInfo(fake_term, fallback=True) |
| 626 |
pyrepl_ok = True |
| 627 |
except Exception: |
| 628 |
pyrepl_ok = False |
| 629 |
|
| 630 |
self.assertTrue( |
| 631 |
pyrepl_ok, "PyREPL should fall back for unknown terminals" |
| 632 |
) |
| 633 |
|
| 634 |
# Should still be able to get basic capabilities |
| 635 |
bel = ti.get("bel") |
| 636 |
self.assertIsNotNone( |
| 637 |
bel, "PyREPL should provide basic capabilities after fallback" |
| 638 |
) |
| 639 |
|
| 640 |
def test_invalid_terminal_names(self): |
| 641 |
cases = [ |
| 642 |
(42, TypeError), |
| 643 |
("", ValueError), |
| 644 |
("w\x00t", ValueError), |
| 645 |
(f"..{os.sep}name", ValueError), |
| 646 |
] |
| 647 |
|
| 648 |
for term, exc in cases: |
| 649 |
with self.subTest(term=term): |
| 650 |
with self.assertRaises(exc): |
| 651 |
terminfo._validate_terminal_name_or_raise(term) |