Skip to content

Commit 7435f05

Browse files
authored
Move get_signal_name() to test.support (#121251)
* Move get_signal_name() from test.libregrtest to test.support. * Use get_signal_name() in support.script_helper. * support.script_helper now decodes stdout and stderr from UTF-8, instead of ASCII, if a command failed.
1 parent bfe0e4d commit 7435f05

File tree

6 files changed

+65
-58
lines changed

6 files changed

+65
-58
lines changed

Lib/test/libregrtest/run_workers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from .single import PROGRESS_MIN_TIME
2323
from .utils import (
2424
StrPath, TestName,
25-
format_duration, print_warning, count, plural, get_signal_name)
25+
format_duration, print_warning, count, plural)
2626
from .worker import create_worker_process, USE_PROCESS_GROUP
2727

2828
if MS_WINDOWS:
@@ -366,7 +366,7 @@ def _runtest(self, test_name: TestName) -> MultiprocessResult:
366366
err_msg=None,
367367
state=State.TIMEOUT)
368368
if retcode != 0:
369-
name = get_signal_name(retcode)
369+
name = support.get_signal_name(retcode)
370370
if name:
371371
retcode = f"{retcode} ({name})"
372372
raise WorkerError(self.test_name, f"Exit code {retcode}", stdout,

Lib/test/libregrtest/utils.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -685,35 +685,6 @@ def cleanup_temp_dir(tmp_dir: StrPath):
685685
print("Remove file: %s" % name)
686686
os_helper.unlink(name)
687687

688-
WINDOWS_STATUS = {
689-
0xC0000005: "STATUS_ACCESS_VIOLATION",
690-
0xC00000FD: "STATUS_STACK_OVERFLOW",
691-
0xC000013A: "STATUS_CONTROL_C_EXIT",
692-
}
693-
694-
def get_signal_name(exitcode):
695-
if exitcode < 0:
696-
signum = -exitcode
697-
try:
698-
return signal.Signals(signum).name
699-
except ValueError:
700-
pass
701-
702-
# Shell exit code (ex: WASI build)
703-
if 128 < exitcode < 256:
704-
signum = exitcode - 128
705-
try:
706-
return signal.Signals(signum).name
707-
except ValueError:
708-
pass
709-
710-
try:
711-
return WINDOWS_STATUS[exitcode]
712-
except KeyError:
713-
pass
714-
715-
return None
716-
717688

718689
ILLEGAL_XML_CHARS_RE = re.compile(
719690
'['

Lib/test/support/__init__.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2632,3 +2632,35 @@ def initialized_with_pyrepl():
26322632
"""Detect whether PyREPL was used during Python initialization."""
26332633
# If the main module has a __file__ attribute it's a Python module, which means PyREPL.
26342634
return hasattr(sys.modules["__main__"], "__file__")
2635+
2636+
2637+
WINDOWS_STATUS = {
2638+
0xC0000005: "STATUS_ACCESS_VIOLATION",
2639+
0xC00000FD: "STATUS_STACK_OVERFLOW",
2640+
0xC000013A: "STATUS_CONTROL_C_EXIT",
2641+
}
2642+
2643+
def get_signal_name(exitcode):
2644+
import signal
2645+
2646+
if exitcode < 0:
2647+
signum = -exitcode
2648+
try:
2649+
return signal.Signals(signum).name
2650+
except ValueError:
2651+
pass
2652+
2653+
# Shell exit code (ex: WASI build)
2654+
if 128 < exitcode < 256:
2655+
signum = exitcode - 128
2656+
try:
2657+
return signal.Signals(signum).name
2658+
except ValueError:
2659+
pass
2660+
2661+
try:
2662+
return WINDOWS_STATUS[exitcode]
2663+
except KeyError:
2664+
pass
2665+
2666+
return None

Lib/test/support/script_helper.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,25 @@ def fail(self, cmd_line):
7070
out = b'(... truncated stdout ...)' + out[-maxlen:]
7171
if len(err) > maxlen:
7272
err = b'(... truncated stderr ...)' + err[-maxlen:]
73-
out = out.decode('ascii', 'replace').rstrip()
74-
err = err.decode('ascii', 'replace').rstrip()
75-
raise AssertionError("Process return code is %d\n"
76-
"command line: %r\n"
77-
"\n"
78-
"stdout:\n"
79-
"---\n"
80-
"%s\n"
81-
"---\n"
82-
"\n"
83-
"stderr:\n"
84-
"---\n"
85-
"%s\n"
86-
"---"
87-
% (self.rc, cmd_line,
88-
out,
89-
err))
73+
out = out.decode('utf8', 'replace').rstrip()
74+
err = err.decode('utf8', 'replace').rstrip()
75+
76+
exitcode = self.rc
77+
signame = support.get_signal_name(exitcode)
78+
if signame:
79+
exitcode = f"{exitcode} ({signame})"
80+
raise AssertionError(f"Process return code is {exitcode}\n"
81+
f"command line: {cmd_line!r}\n"
82+
f"\n"
83+
f"stdout:\n"
84+
f"---\n"
85+
f"{out}\n"
86+
f"---\n"
87+
f"\n"
88+
f"stderr:\n"
89+
f"---\n"
90+
f"{err}\n"
91+
f"---")
9092

9193

9294
# Executing the interpreter in a subprocess

Lib/test/test_regrtest.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2329,16 +2329,6 @@ def test_normalize_test_name(self):
23292329
self.assertIsNone(normalize('setUpModule (test.test_x)', is_error=True))
23302330
self.assertIsNone(normalize('tearDownModule (test.test_module)', is_error=True))
23312331

2332-
def test_get_signal_name(self):
2333-
for exitcode, expected in (
2334-
(-int(signal.SIGINT), 'SIGINT'),
2335-
(-int(signal.SIGSEGV), 'SIGSEGV'),
2336-
(128 + int(signal.SIGABRT), 'SIGABRT'),
2337-
(3221225477, "STATUS_ACCESS_VIOLATION"),
2338-
(0xC00000FD, "STATUS_STACK_OVERFLOW"),
2339-
):
2340-
self.assertEqual(utils.get_signal_name(exitcode), expected, exitcode)
2341-
23422332
def test_format_resources(self):
23432333
format_resources = utils.format_resources
23442334
ALL_RESOURCES = utils.ALL_RESOURCES

Lib/test/test_support.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io
44
import os
55
import shutil
6+
import signal
67
import socket
78
import stat
89
import subprocess
@@ -732,6 +733,17 @@ def test_copy_python_src_ignore(self):
732733
self.assertEqual(support.copy_python_src_ignore(path, os.listdir(path)),
733734
ignored)
734735

736+
def test_get_signal_name(self):
737+
for exitcode, expected in (
738+
(-int(signal.SIGINT), 'SIGINT'),
739+
(-int(signal.SIGSEGV), 'SIGSEGV'),
740+
(128 + int(signal.SIGABRT), 'SIGABRT'),
741+
(3221225477, "STATUS_ACCESS_VIOLATION"),
742+
(0xC00000FD, "STATUS_STACK_OVERFLOW"),
743+
):
744+
self.assertEqual(support.get_signal_name(exitcode), expected,
745+
exitcode)
746+
735747
# XXX -follows a list of untested API
736748
# make_legacy_pyc
737749
# is_resource_enabled

0 commit comments

Comments
 (0)