https://github.com/python/cpython/commit/9f5152441d32166134c3c64f56f974b9476f9478
commit: 9f5152441d32166134c3c64f56f974b9476f9478
branch: main
author: Petr Viktorin <[email protected]>
committer: encukou <[email protected]>
date: 2025-11-10T14:42:18+01:00
summary:

gh-136702: Clear codec caches for refleak tests; use test.support helpers 
(GH-141345)

This should fix refleak buildbots.

files:
M Lib/test/libregrtest/utils.py
M Lib/test/test_codecs.py
M Lib/test/test_email/test_email.py
M Lib/test/test_email/test_headerregistry.py

diff --git a/Lib/test/libregrtest/utils.py b/Lib/test/libregrtest/utils.py
index d94fb84a743828..cfb009c203ee80 100644
--- a/Lib/test/libregrtest/utils.py
+++ b/Lib/test/libregrtest/utils.py
@@ -294,6 +294,25 @@ def clear_caches():
     else:
         importlib_metadata.FastPath.__new__.cache_clear()
 
+    try:
+        encodings = sys.modules['encodings']
+    except KeyError:
+        pass
+    else:
+        encodings._cache.clear()
+
+    try:
+        codecs = sys.modules['codecs']
+    except KeyError:
+        pass
+    else:
+        # There's no direct API to clear the codecs search cache, but
+        # `unregister` clears it implicitly.
+        def noop_search_function(name):
+            return None
+        codecs.register(noop_search_function)
+        codecs.unregister(noop_search_function)
+
 
 def get_build_info():
     # Get most important configure and build options as a list of strings.
diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py
index f1f0ac5ad36fd2..c31faec9ee5214 100644
--- a/Lib/test/test_codecs.py
+++ b/Lib/test/test_codecs.py
@@ -13,6 +13,7 @@
 
 from test import support
 from test.support import os_helper
+from test.support import warnings_helper
 
 try:
     import _testlimitedcapi
@@ -3902,8 +3903,8 @@ def test_encodings_normalize_encoding(self):
         self.assertEqual(normalize('utf...8'), 'utf...8')
 
         # Non-ASCII *encoding* is deprecated.
-        with self.assertWarnsRegex(DeprecationWarning,
-                "Support for non-ascii encoding names will be removed in 
3.17"):
+        msg = "Support for non-ascii encoding names will be removed in 3.17"
+        with warnings_helper.check_warnings((msg, DeprecationWarning)):
             self.assertEqual(normalize('utf\xE9\u20AC\U0010ffff-8'), 'utf_8')
 
 
diff --git a/Lib/test/test_email/test_email.py 
b/Lib/test/test_email/test_email.py
index 1900adf463befc..4020f1041c4304 100644
--- a/Lib/test/test_email/test_email.py
+++ b/Lib/test/test_email/test_email.py
@@ -41,6 +41,7 @@
 
 from test import support
 from test.support import threading_helper
+from test.support import warnings_helper
 from test.support.os_helper import unlink
 from test.test_email import openfile, TestEmailBase
 
@@ -5738,7 +5739,7 @@ def test_rfc2231_bad_character_in_encoding(self):
 
 """
         msg = email.message_from_string(m)
-        with self.assertWarns(DeprecationWarning):
+        with warnings_helper.check_warnings(('', DeprecationWarning)):
             self.assertEqual(msg.get_filename(), 'myfile.txt')
 
     def test_rfc2231_single_tick_in_filename_extended(self):
diff --git a/Lib/test/test_email/test_headerregistry.py 
b/Lib/test/test_email/test_headerregistry.py
index 1d0d0a49a82917..7138aa4c556d1f 100644
--- a/Lib/test/test_email/test_headerregistry.py
+++ b/Lib/test/test_email/test_headerregistry.py
@@ -8,6 +8,7 @@
 from email import headerregistry
 from email.headerregistry import Address, Group
 from test.support import ALWAYS_EQ
+from test.support import warnings_helper
 
 
 DITTO = object()
@@ -252,7 +253,7 @@ def content_type_as_value(self,
         if 'utf-8%E2%80%9D' in source and 'ascii' not in source:
             import encodings
             encodings._cache.clear()
-            with self.assertWarns(DeprecationWarning):
+            with warnings_helper.check_warnings(('', DeprecationWarning)):
                 h = self.make_header('Content-Type', source)
         else:
             h = self.make_header('Content-Type', source)

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to