Skip to content

Commit 9f51524

Browse files
authored
pythongh-136702: Clear codec caches for refleak tests; use test.support helpers (pythonGH-141345)
This should fix refleak buildbots.
1 parent 13fa313 commit 9f51524

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

Lib/test/libregrtest/utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,25 @@ def clear_caches():
294294
else:
295295
importlib_metadata.FastPath.__new__.cache_clear()
296296

297+
try:
298+
encodings = sys.modules['encodings']
299+
except KeyError:
300+
pass
301+
else:
302+
encodings._cache.clear()
303+
304+
try:
305+
codecs = sys.modules['codecs']
306+
except KeyError:
307+
pass
308+
else:
309+
# There's no direct API to clear the codecs search cache, but
310+
# `unregister` clears it implicitly.
311+
def noop_search_function(name):
312+
return None
313+
codecs.register(noop_search_function)
314+
codecs.unregister(noop_search_function)
315+
297316

298317
def get_build_info():
299318
# Get most important configure and build options as a list of strings.

Lib/test/test_codecs.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from test import support
1515
from test.support import os_helper
16+
from test.support import warnings_helper
1617

1718
try:
1819
import _testlimitedcapi
@@ -3902,8 +3903,8 @@ def test_encodings_normalize_encoding(self):
39023903
self.assertEqual(normalize('utf...8'), 'utf...8')
39033904

39043905
# Non-ASCII *encoding* is deprecated.
3905-
with self.assertWarnsRegex(DeprecationWarning,
3906-
"Support for non-ascii encoding names will be removed in 3.17"):
3906+
msg = "Support for non-ascii encoding names will be removed in 3.17"
3907+
with warnings_helper.check_warnings((msg, DeprecationWarning)):
39073908
self.assertEqual(normalize('utf\xE9\u20AC\U0010ffff-8'), 'utf_8')
39083909

39093910

Lib/test/test_email/test_email.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
from test import support
4343
from test.support import threading_helper
44+
from test.support import warnings_helper
4445
from test.support.os_helper import unlink
4546
from test.test_email import openfile, TestEmailBase
4647

@@ -5738,7 +5739,7 @@ def test_rfc2231_bad_character_in_encoding(self):
57385739
57395740
"""
57405741
msg = email.message_from_string(m)
5741-
with self.assertWarns(DeprecationWarning):
5742+
with warnings_helper.check_warnings(('', DeprecationWarning)):
57425743
self.assertEqual(msg.get_filename(), 'myfile.txt')
57435744

57445745
def test_rfc2231_single_tick_in_filename_extended(self):

Lib/test/test_email/test_headerregistry.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from email import headerregistry
99
from email.headerregistry import Address, Group
1010
from test.support import ALWAYS_EQ
11+
from test.support import warnings_helper
1112

1213

1314
DITTO = object()
@@ -252,7 +253,7 @@ def content_type_as_value(self,
252253
if 'utf-8%E2%80%9D' in source and 'ascii' not in source:
253254
import encodings
254255
encodings._cache.clear()
255-
with self.assertWarns(DeprecationWarning):
256+
with warnings_helper.check_warnings(('', DeprecationWarning)):
256257
h = self.make_header('Content-Type', source)
257258
else:
258259
h = self.make_header('Content-Type', source)

0 commit comments

Comments
 (0)