From b1c50b80a6197ba1e41539ec0984a9925956cc1d Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Wed, 19 Jul 2023 04:34:54 -0700 Subject: [PATCH] [3.12] gh-104090: Fix unittest collectedDurations resources leak (GH-106795) (#106888) gh-104090: Fix unittest collectedDurations resources leak (GH-106795) (cherry picked from commit 70b961ed93f67e34d0624e178f6029c886afaeee) Co-authored-by: Yonatan Bitton --- Lib/unittest/result.py | 3 ++- .../next/Tests/2023-07-16-02-57-08.gh-issue-104090.cKtK7g.rst | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Tests/2023-07-16-02-57-08.gh-issue-104090.cKtK7g.rst diff --git a/Lib/unittest/result.py b/Lib/unittest/result.py index 7757dba9670b43..3ace0a5b7bf2ef 100644 --- a/Lib/unittest/result.py +++ b/Lib/unittest/result.py @@ -166,7 +166,8 @@ def addDuration(self, test, elapsed): """ # support for a TextTestRunner using an old TestResult class if hasattr(self, "collectedDurations"): - self.collectedDurations.append((test, elapsed)) + # Pass test repr and not the test object itself to avoid resources leak + self.collectedDurations.append((str(test), elapsed)) def wasSuccessful(self): """Tells whether or not this result was a success.""" diff --git a/Misc/NEWS.d/next/Tests/2023-07-16-02-57-08.gh-issue-104090.cKtK7g.rst b/Misc/NEWS.d/next/Tests/2023-07-16-02-57-08.gh-issue-104090.cKtK7g.rst new file mode 100644 index 00000000000000..5cc6c5bbe15446 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2023-07-16-02-57-08.gh-issue-104090.cKtK7g.rst @@ -0,0 +1 @@ +Avoid creating a reference to the test object in :meth:`~unittest.TestResult.collectedDurations`.