diff options
Diffstat (limited to 'Lib/test')
| -rw-r--r-- | Lib/test/test_charmapcodec.py | 7 | ||||
| -rw-r--r-- | Lib/test/test_codecs.py | 25 | ||||
| -rw-r--r-- | Lib/test/test_io.py | 7 | ||||
| -rw-r--r-- | Lib/test/test_unicode.py | 5 |
4 files changed, 15 insertions, 29 deletions
diff --git a/Lib/test/test_charmapcodec.py b/Lib/test/test_charmapcodec.py index 0d4594d8c0..3f628902a1 100644 --- a/Lib/test/test_charmapcodec.py +++ b/Lib/test/test_charmapcodec.py @@ -20,12 +20,15 @@ def codec_search_function(encoding): return tuple(testcodec.getregentry()) return None -codecs.register(codec_search_function) - # test codec's name (see test/testcodec.py) codecname = 'testcodec' class CharmapCodecTest(unittest.TestCase): + + def setUp(self): + codecs.register(codec_search_function) + self.addCleanup(codecs.unregister, codec_search_function) + def test_constructorx(self): self.assertEqual(str(b'abc', codecname), 'abc') self.assertEqual(str(b'xdef', codecname), 'abcdef') diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 09ceef76eb..9be8281ce5 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -2754,29 +2754,14 @@ _TEST_CODECS = {} def _get_test_codec(codec_name): return _TEST_CODECS.get(codec_name) -codecs.register(_get_test_codec) # Returns None, not usable as a decorator - -try: - # Issue #22166: Also need to clear the internal cache in CPython - from _codecs import _forget_codec -except ImportError: - def _forget_codec(codec_name): - pass class ExceptionChainingTest(unittest.TestCase): def setUp(self): - # There's no way to unregister a codec search function, so we just - # ensure we render this one fairly harmless after the test - # case finishes by using the test case repr as the codec name - # The codecs module normalizes codec names, although this doesn't - # appear to be formally documented... - # We also make sure we use a truly unique id for the custom codec - # to avoid issues with the codec cache when running these tests - # multiple times (e.g. when hunting for refleaks) - unique_id = repr(self) + str(id(self)) - self.codec_name = encodings.normalize_encoding(unique_id).lower() + self.codec_name = 'exception_chaining_test' + codecs.register(_get_test_codec) + self.addCleanup(codecs.unregister, _get_test_codec) # We store the object to raise on the instance because of a bad # interaction between the codec caching (which means we can't @@ -2791,10 +2776,6 @@ class ExceptionChainingTest(unittest.TestCase): _TEST_CODECS.pop(self.codec_name, None) # Issue #22166: Also pop from caches to avoid appearance of ref leaks encodings._cache.pop(self.codec_name, None) - try: - _forget_codec(self.codec_name) - except KeyError: - pass def set_codec(self, encode, decode): codec_info = codecs.CodecInfo(encode, decode, diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 85fac30e30..fbaea3aaec 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -2529,10 +2529,6 @@ class StatefulIncrementalDecoder(codecs.IncrementalDecoder): streamreader=None, streamwriter=None, incrementaldecoder=cls) -# Register the previous decoder for testing. -# Disabled by default, tests will enable it. -codecs.register(StatefulIncrementalDecoder.lookupTestDecoder) - class StatefulIncrementalDecoderTest(unittest.TestCase): """ @@ -2583,6 +2579,9 @@ class TextIOWrapperTest(unittest.TestCase): self.testdata = b"AAA\r\nBBB\rCCC\r\nDDD\nEEE\r\n" self.normalized = b"AAA\nBBB\nCCC\nDDD\nEEE\n".decode("ascii") os_helper.unlink(os_helper.TESTFN) + codecs.register(StatefulIncrementalDecoder.lookupTestDecoder) + self.addCleanup(codecs.unregister, + StatefulIncrementalDecoder.lookupTestDecoder) def tearDown(self): os_helper.unlink(os_helper.TESTFN) diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index d485bc7ede..90b0965582 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -36,7 +36,6 @@ def search_function(encoding): return (encode2, decode2, None, None) else: return None -codecs.register(search_function) def duplicate_string(text): """ @@ -58,6 +57,10 @@ class UnicodeTest(string_tests.CommonTest, type2test = str + def setUp(self): + codecs.register(search_function) + self.addCleanup(codecs.unregister, search_function) + def checkequalnofix(self, result, object, methodname, *args): method = getattr(object, methodname) realresult = method(*args) |
