Repository navigation
test.test_pdb.PdbTestColorize.test_gen_colors fails when pyrepl is unavailable at runtime #150544
Description
Bug report
Bug description:
Description
test.test_pdb.PdbTestColorize.test_gen_colors can fail in the standard test suite (for example via make test) with:
Re-running test_pdb in verbose mode (matching: test_gen_colors)
test_gen_colors (test.test_pdb.PdbTestColorize.test_gen_colors) ... ERROR
======================================================================
ERROR: test_gen_colors (test.test_pdb.PdbTestColorize.test_gen_colors)
----------------------------------------------------------------------
Traceback (most recent call last):
File �[35m"/home/yansendao/git/cpython-ysd/Lib/test/test_pdb.py"�[0m, line �[35m5008�[0m, in �[35mtest_gen_colors�[0m
gen_colors = �[1;31mp.pyrepl_input.gen_colors�[0m
�[1;31m^^^^^^^^^^^^^^^^^^^^^^^^^�[0m
�[1;35mAttributeError�[0m: �[35m'NoneType' object has no attribute 'gen_colors'�[0m
Root cause
PdbTestColorize.test_gen_colors creates pdb.Pdb() with the default streams,
so it uses sys.stdin/sys.stdout at the moment the test runs. With
--junit-xml, regrtest creates its test runner with capture_output=True
(_run_suite() in Lib/test/libregrtest/single.py), i.e.
TextTestRunner(buffer=True). For every test, unittest.result.TestResult. _setupStdout() replaces sys.stdout and sys.stderr with io.StringIO().
PdbPyReplInput.__init__() then calls stdout.fileno() (Lib/pdb.py), which
raises io.UnsupportedOperation on a StringIO; Pdb.__init__ swallows the
exception (except Exception: pass) and leaves self.pyrepl_input = None,
which the test then dereferences.
The test was skipped at import time instead:
@unittest.skipIf(not pdb._pyrepl_available()) is evaluated when the test
class is defined, not when the test runs. _pyrepl_available() only consults
_pyrepl.main.CAN_USE_PYREPL, which is computed at import time from
sys.stdin alone. In this scenario sys.stdin is still a real TTY, so the
condition stays True and the test is not skipped, even though the pdb.Pdb()
instance created inside the test cannot use pyrepl.
Additionally, _pyrepl_available() always inspected sys.stdin, while Pdb
uses self.stdin and self.stdout, so it could disagree with whether
PdbPyReplInput can actually be created.
Steps to reproduce
Run from a terminal, so that sys.stdin is a TTY and pyrepl is considered
available. The --junit-xml flag is what enables unittest output buffering
and triggers the error:
./python -m test test_pdb -m test_gen_colors # passes
./python -m test test_pdb -m test_gen_colors --junit-xml /tmp/x.xml # errorsOr without regrtest:
./python -c 'import io, sys, pdb; sys.stdout = io.StringIO(); print(pdb.Pdb().pyrepl_input, file=sys.__stdout__)'
# NoneCPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Activity
Add NEWS for pythongh-150544
Merge branch 'main' into pythongh-150544
sendaoYan commentedon Jul 25, 2026
/touch
Merge branch 'main' into pythongh-150544
Merge branch 'main' into pythongh-150544
pythongh-150544: Correct NEWS entries for test_gen_colors