From f2a34521f99ca006a44bed5d67a3bdd4409bf372 Mon Sep 17 00:00:00 2001 From: furkanonder Date: Sun, 20 Nov 2022 23:21:45 +0300 Subject: [PATCH 1/5] gh-51524: fix coverage result fail for the merge input file with non-empty callers --- Lib/test/test_trace.py | 9 +++++++++ Lib/trace.py | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_trace.py b/Lib/test/test_trace.py index 5f712111ca14e0..ee385ede152175 100644 --- a/Lib/test/test_trace.py +++ b/Lib/test/test_trace.py @@ -1,4 +1,5 @@ import os +import pickle import sys from test.support import captured_stdout from test.support.os_helper import (TESTFN, rmtree, unlink) @@ -412,6 +413,14 @@ def test_issue9936(self): self.assertIn(modname, coverage) self.assertEqual(coverage[modname], (5, 100)) + def test_coverageresults_update(self): + outfile = TESTFN + '-outfile' + with open(outfile, 'wb') as f: + pickle.dump(({}, {}, {'caller': 1}), f, protocol=1) + self.addCleanup(unlink, outfile) + results = trace.CoverageResults({}, {}, outfile, {}) + self.assertEqual(results.callers, {'caller': 1}) + ### Tests that don't mess with sys.settrace and can be traced ### themselves TODO: Skip tests that do mess with sys.settrace when ### regrtest is invoked with -T option. diff --git a/Lib/trace.py b/Lib/trace.py index 2cf3643878d4b8..ce61ffc2b978eb 100755 --- a/Lib/trace.py +++ b/Lib/trace.py @@ -167,12 +167,12 @@ def __init__(self, counts=None, calledfuncs=None, infile=None, self.callers = self.callers.copy() self.infile = infile self.outfile = outfile - if self.infile: + if self.infile is not None: # Try to merge existing counts file. try: with open(self.infile, 'rb') as f: counts, calledfuncs, callers = pickle.load(f) - self.update(self.__class__(counts, calledfuncs, callers)) + self.update(CoverageResults(counts, calledfuncs, callers=callers)) except (OSError, EOFError, ValueError) as err: print(("Skipping counts file %r: %s" % (self.infile, err)), file=sys.stderr) From 297912b0501405054809f439c7b742d5cc2b28f0 Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Mon, 21 Nov 2022 13:17:40 +0300 Subject: [PATCH 2/5] update test_coverageresults_update func Co-authored-by: Terry Jan Reedy --- Lib/test/test_trace.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_trace.py b/Lib/test/test_trace.py index ee385ede152175..09e74f7bb6115d 100644 --- a/Lib/test/test_trace.py +++ b/Lib/test/test_trace.py @@ -414,11 +414,12 @@ def test_issue9936(self): self.assertEqual(coverage[modname], (5, 100)) def test_coverageresults_update(self): - outfile = TESTFN + '-outfile' - with open(outfile, 'wb') as f: + # Update empty CoverageResults with a non-empty infile. + infile = TESTFN + '-infile' + with open(infile, 'wb') as f: pickle.dump(({}, {}, {'caller': 1}), f, protocol=1) - self.addCleanup(unlink, outfile) - results = trace.CoverageResults({}, {}, outfile, {}) + self.addCleanup(unlink, infile) + results = trace.CoverageResults({}, {}, infile, {}) self.assertEqual(results.callers, {'caller': 1}) ### Tests that don't mess with sys.settrace and can be traced From 40502d524861144d6f53d2c24c0e25fa2db2b9a3 Mon Sep 17 00:00:00 2001 From: furkanonder Date: Mon, 21 Nov 2022 20:38:58 +0300 Subject: [PATCH 3/5] fix pickle import --- Lib/test/test_trace.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_trace.py b/Lib/test/test_trace.py index 09e74f7bb6115d..fad2b3b8379ffc 100644 --- a/Lib/test/test_trace.py +++ b/Lib/test/test_trace.py @@ -1,5 +1,5 @@ import os -import pickle +from pickle import dump import sys from test.support import captured_stdout from test.support.os_helper import (TESTFN, rmtree, unlink) @@ -417,7 +417,7 @@ def test_coverageresults_update(self): # Update empty CoverageResults with a non-empty infile. infile = TESTFN + '-infile' with open(infile, 'wb') as f: - pickle.dump(({}, {}, {'caller': 1}), f, protocol=1) + dump(({}, {}, {'caller': 1}), f, protocol=1) self.addCleanup(unlink, infile) results = trace.CoverageResults({}, {}, infile, {}) self.assertEqual(results.callers, {'caller': 1}) From 73494dc173469b801c1cf89b088d1565db89a5f1 Mon Sep 17 00:00:00 2001 From: furkanonder Date: Mon, 21 Nov 2022 20:51:58 +0300 Subject: [PATCH 4/5] update if statement and class name --- Lib/trace.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/trace.py b/Lib/trace.py index ce61ffc2b978eb..213e46517d683d 100755 --- a/Lib/trace.py +++ b/Lib/trace.py @@ -167,12 +167,12 @@ def __init__(self, counts=None, calledfuncs=None, infile=None, self.callers = self.callers.copy() self.infile = infile self.outfile = outfile - if self.infile is not None: + if self.infile: # Try to merge existing counts file. try: with open(self.infile, 'rb') as f: counts, calledfuncs, callers = pickle.load(f) - self.update(CoverageResults(counts, calledfuncs, callers=callers)) + self.update(self.__class__(counts, calledfuncs, callers=callers)) except (OSError, EOFError, ValueError) as err: print(("Skipping counts file %r: %s" % (self.infile, err)), file=sys.stderr) From 277022b68144d6d0280c562525ff2830121042e7 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 21 Nov 2022 17:56:21 +0000 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2022-11-21-17-56-18.gh-issue-51524.nTykx8.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-11-21-17-56-18.gh-issue-51524.nTykx8.rst diff --git a/Misc/NEWS.d/next/Library/2022-11-21-17-56-18.gh-issue-51524.nTykx8.rst b/Misc/NEWS.d/next/Library/2022-11-21-17-56-18.gh-issue-51524.nTykx8.rst new file mode 100644 index 00000000000000..63fe7b8a3a3254 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-11-21-17-56-18.gh-issue-51524.nTykx8.rst @@ -0,0 +1 @@ +Fix bug when calling trace.CoverageResults with valid infile.