Skip to content

Commit ccef823

Browse files
authored
bpo-25588: Fix regrtest when run inside IDLE (python#3962)
When regrtest in run inside IDLE, sys.stdout and sys.stderr are not TextIOWrapper objects and have no file descriptor associated: sys.stderr.fileno() raises io.UnsupportedOperation. Disable faulthandler and don't replace sys.stdout in that case.
1 parent 7f58097 commit ccef823

1 file changed

Lines changed: 31 additions & 12 deletions

File tree

‎Lib/test/libregrtest/setup.py‎

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,26 @@
1414

1515

1616
def setup_tests(ns):
17-
# Display the Python traceback on fatal errors (e.g. segfault)
18-
faulthandler.enable(all_threads=True)
19-
20-
# Display the Python traceback on SIGALRM or SIGUSR1 signal
21-
signals = []
22-
if hasattr(signal, 'SIGALRM'):
23-
signals.append(signal.SIGALRM)
24-
if hasattr(signal, 'SIGUSR1'):
25-
signals.append(signal.SIGUSR1)
26-
for signum in signals:
27-
faulthandler.register(signum, chain=True)
17+
try:
18+
stderr_fd = sys.__stderr__.fileno()
19+
except (ValueError, AttributeError):
20+
# Catch ValueError to catch io.UnsupportedOperation on TextIOBase
21+
# and ValueError on a closed stream.
22+
#
23+
# Catch AttributeError for stderr being None.
24+
stderr_fd = None
25+
else:
26+
# Display the Python traceback on fatal errors (e.g. segfault)
27+
faulthandler.enable(all_threads=True, file=stderr_fd)
28+
29+
# Display the Python traceback on SIGALRM or SIGUSR1 signal
30+
signals = []
31+
if hasattr(signal, 'SIGALRM'):
32+
signals.append(signal.SIGALRM)
33+
if hasattr(signal, 'SIGUSR1'):
34+
signals.append(signal.SIGUSR1)
35+
for signum in signals:
36+
faulthandler.register(signum, chain=True, file=stderr_fd)
2837

2938
replace_stdout()
3039
support.record_original_stdout(sys.stdout)
@@ -109,7 +118,17 @@ def replace_stdout():
109118
"""Set stdout encoder error handler to backslashreplace (as stderr error
110119
handler) to avoid UnicodeEncodeError when printing a traceback"""
111120
stdout = sys.stdout
112-
sys.stdout = open(stdout.fileno(), 'w',
121+
try:
122+
fd = stdout.fileno()
123+
except ValueError:
124+
# On IDLE, sys.stdout has no file descriptor and is not a TextIOWrapper
125+
# object. Leaving sys.stdout unchanged.
126+
#
127+
# Catch ValueError to catch io.UnsupportedOperation on TextIOBase
128+
# and ValueError on a closed stream.
129+
return
130+
131+
sys.stdout = open(fd, 'w',
113132
encoding=stdout.encoding,
114133
errors="backslashreplace",
115134
closefd=False,

0 commit comments

Comments
 (0)