summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-05-20 22:50:20 (GMT)
committerGitHub <noreply@github.com>2024-05-20 22:50:20 (GMT)
commit42a8d1175240f911727411a61d812b767203c133 (patch)
tree6bace352abe1ba9509f830097a3f7fcbb84a90c3
parent071d996e13d5499b3a0590a4a874e4bbe7fb86bf (diff)
downloadcpython-42a8d1175240f911727411a61d812b767203c133.zip
cpython-42a8d1175240f911727411a61d812b767203c133.tar.gz
cpython-42a8d1175240f911727411a61d812b767203c133.tar.bz2
[3.13] gh-119050: Add XML support to libregrtest refleak checker (GH-119148) (#119270)
gh-119050: Add XML support to libregrtest refleak checker (GH-119148) regrtest test runner: Add XML support to the refleak checker (-R option). * run_unittest() now stores XML elements as string, rather than objects, in support.junit_xml_list. * runtest_refleak() now saves/restores XML strings before/after checking for reference leaks. Save XML into a temporary file. (cherry picked from commit 9257731f5d3e9d4f99e314b23a14506563e167d7) Co-authored-by: Victor Stinner <vstinner@python.org>
-rw-r--r--Lib/test/libregrtest/cmdline.py9
-rw-r--r--Lib/test/libregrtest/refleak.py35
-rw-r--r--Lib/test/libregrtest/single.py9
-rw-r--r--Lib/test/test_regrtest.py9
-rw-r--r--Misc/NEWS.d/next/Tests/2024-05-18-10-59-27.gh-issue-119050.g4qiH7.rst2
5 files changed, 39 insertions, 25 deletions
diff --git a/Lib/test/libregrtest/cmdline.py b/Lib/test/libregrtest/cmdline.py
index 3e7428c..d4dac77 100644
--- a/Lib/test/libregrtest/cmdline.py
+++ b/Lib/test/libregrtest/cmdline.py
@@ -520,15 +520,6 @@ def _parse_args(args, **kwargs):
"--huntrleaks without -jN option",
file=sys.stderr)
- if ns.huntrleaks and ns.xmlpath:
- # The XML data is written into a file outside runtest_refleak(), so
- # it looks like a leak but it's not. Simply disable XML output when
- # hunting for reference leaks (gh-83434).
- ns.xmlpath = None
- print("WARNING: Disable --junit-xml because it's incompatible "
- "with --huntrleaks",
- file=sys.stderr)
-
if ns.forever:
# --forever implies --failfast
ns.failfast = True
diff --git a/Lib/test/libregrtest/refleak.py b/Lib/test/libregrtest/refleak.py
index f582c0d..697f305 100644
--- a/Lib/test/libregrtest/refleak.py
+++ b/Lib/test/libregrtest/refleak.py
@@ -1,3 +1,4 @@
+import os
import sys
import warnings
from inspect import isabstract
@@ -23,6 +24,30 @@ except ImportError:
cls._abc_negative_cache, cls._abc_negative_cache_version)
+def save_support_xml(filename):
+ if support.junit_xml_list is None:
+ return
+
+ import pickle
+ with open(filename, 'xb') as fp:
+ pickle.dump(support.junit_xml_list, fp)
+ support.junit_xml_list = None
+
+
+def restore_support_xml(filename):
+ try:
+ fp = open(filename, 'rb')
+ except FileNotFoundError:
+ return
+
+ import pickle
+ with fp:
+ xml_list = pickle.load(fp)
+ os.unlink(filename)
+
+ support.junit_xml_list = xml_list
+
+
def runtest_refleak(test_name, test_func,
hunt_refleak: HuntRefleak,
quiet: bool):
@@ -95,7 +120,8 @@ def runtest_refleak(test_name, test_func,
numbers = numbers[:warmups] + ':' + numbers[warmups:]
print(numbers, file=sys.stderr, flush=True)
- results = None
+ xml_filename = 'refleak-xml.tmp'
+ result = None
dash_R_cleanup(fs, ps, pic, zdc, abcs)
support.gc_collect()
@@ -103,10 +129,11 @@ def runtest_refleak(test_name, test_func,
current = refleak_helper._hunting_for_refleaks
refleak_helper._hunting_for_refleaks = True
try:
- results = test_func()
+ result = test_func()
finally:
refleak_helper._hunting_for_refleaks = current
+ save_support_xml(xml_filename)
dash_R_cleanup(fs, ps, pic, zdc, abcs)
support.gc_collect()
@@ -145,6 +172,8 @@ def runtest_refleak(test_name, test_func,
fd_before = fd_after
interned_before = interned_after
+ restore_support_xml(xml_filename)
+
if not quiet:
print(file=sys.stderr)
@@ -189,7 +218,7 @@ def runtest_refleak(test_name, test_func,
failed = True
else:
print(' (this is fine)', file=sys.stderr, flush=True)
- return (failed, results)
+ return (failed, result)
def dash_R_cleanup(fs, ps, pic, zdc, abcs):
diff --git a/Lib/test/libregrtest/single.py b/Lib/test/libregrtest/single.py
index fc2f271..adc8f1f 100644
--- a/Lib/test/libregrtest/single.py
+++ b/Lib/test/libregrtest/single.py
@@ -57,7 +57,10 @@ def _run_suite(suite):
result = runner.run(suite)
if support.junit_xml_list is not None:
- support.junit_xml_list.append(result.get_xml_element())
+ import xml.etree.ElementTree as ET
+ xml_elem = result.get_xml_element()
+ xml_str = ET.tostring(xml_elem).decode('ascii')
+ support.junit_xml_list.append(xml_str)
if not result.testsRun and not result.skipped and not result.errors:
raise support.TestDidNotRun
@@ -280,9 +283,7 @@ def _runtest(result: TestResult, runtests: RunTests) -> None:
xml_list = support.junit_xml_list
if xml_list:
- import xml.etree.ElementTree as ET
- result.xml_data = [ET.tostring(x).decode('us-ascii')
- for x in xml_list]
+ result.xml_data = xml_list
finally:
if use_timeout:
faulthandler.cancel_dump_traceback_later()
diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py
index 809abd7..17eff61 100644
--- a/Lib/test/test_regrtest.py
+++ b/Lib/test/test_regrtest.py
@@ -473,15 +473,6 @@ class ParseArgsTestCase(unittest.TestCase):
self.assertEqual(regrtest.hunt_refleak.runs, 10)
self.assertFalse(regrtest.output_on_failure)
- def test_xml_huntrleaks(self):
- args = ['-R', '3:12', '--junit-xml', 'output.xml']
- with support.captured_stderr():
- regrtest = self.create_regrtest(args)
- self.assertIsNotNone(regrtest.hunt_refleak)
- self.assertEqual(regrtest.hunt_refleak.warmups, 3)
- self.assertEqual(regrtest.hunt_refleak.runs, 12)
- self.assertIsNone(regrtest.junit_filename)
-
@dataclasses.dataclass(slots=True)
class Rerun:
diff --git a/Misc/NEWS.d/next/Tests/2024-05-18-10-59-27.gh-issue-119050.g4qiH7.rst b/Misc/NEWS.d/next/Tests/2024-05-18-10-59-27.gh-issue-119050.g4qiH7.rst
new file mode 100644
index 0000000..cfc70c1
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2024-05-18-10-59-27.gh-issue-119050.g4qiH7.rst
@@ -0,0 +1,2 @@
+regrtest test runner: Add XML support to the refleak checker (-R option).
+Patch by Victor Stinner.