From 509636ac5d539ff115616b24892707e25ca678b5 Mon Sep 17 00:00:00 2001
From: Yonatan Bitton <yonatan.bitton@perception-point.io>
Date: Sun, 16 Jul 2023 14:58:20 +0300
Subject: [PATCH 1/3] gh-104090: Fix unittest collectedDurations resources leak

---
 Lib/unittest/result.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

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."""

From ea4e7665392627f6fa075f3d8eb0df361de60d19 Mon Sep 17 00:00:00 2001
From: Yonatan Bitton <yonatan.bitton@perception-point.io>
Date: Sun, 16 Jul 2023 14:59:07 +0300
Subject: [PATCH 2/3] Added NEWS file

---
 .../next/Tests/2023-07-16-02-57-08.gh-issue-104090.cKtK7g.rst    | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 Misc/NEWS.d/next/Tests/2023-07-16-02-57-08.gh-issue-104090.cKtK7g.rst

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..49d0275e1e3906
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2023-07-16-02-57-08.gh-issue-104090.cKtK7g.rst
@@ -0,0 +1 @@
+Fix unittest collectedDurations resources leak

From 800434e81ed8785179a3ae26937443ecccff7517 Mon Sep 17 00:00:00 2001
From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Date: Wed, 19 Jul 2023 10:53:40 +0100
Subject: [PATCH 3/3] tweak wording in news

---
 .../next/Tests/2023-07-16-02-57-08.gh-issue-104090.cKtK7g.rst   | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

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
index 49d0275e1e3906..5cc6c5bbe15446 100644
--- 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
@@ -1 +1 @@
-Fix unittest collectedDurations resources leak
+Avoid creating a reference to the test object in :meth:`~unittest.TestResult.collectedDurations`.