Skip to content

Commit c9f696c

Browse files
authored
bpo-41919, test_codecs: Move codecs.register calls to setUp() (GH-22513)
* Move the codecs' (un)register operation to testcases. * Remove _codecs._forget_codec() and _PyCodec_Forget()
1 parent cf693e5 commit c9f696c

File tree

7 files changed

+16
-112
lines changed

7 files changed

+16
-112
lines changed

Lib/test/test_charmapcodec.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ def codec_search_function(encoding):
2020
return tuple(testcodec.getregentry())
2121
return None
2222

23-
codecs.register(codec_search_function)
24-
2523
# test codec's name (see test/testcodec.py)
2624
codecname = 'testcodec'
2725

2826
class CharmapCodecTest(unittest.TestCase):
27+
28+
def setUp(self):
29+
codecs.register(codec_search_function)
30+
self.addCleanup(codecs.unregister, codec_search_function)
31+
2932
def test_constructorx(self):
3033
self.assertEqual(str(b'abc', codecname), 'abc')
3134
self.assertEqual(str(b'xdef', codecname), 'abcdef')

Lib/test/test_codecs.py

+3-22
Original file line numberDiff line numberDiff line change
@@ -2754,29 +2754,14 @@ def test_uu_invalid(self):
27542754

27552755
def _get_test_codec(codec_name):
27562756
return _TEST_CODECS.get(codec_name)
2757-
codecs.register(_get_test_codec) # Returns None, not usable as a decorator
2758-
2759-
try:
2760-
# Issue #22166: Also need to clear the internal cache in CPython
2761-
from _codecs import _forget_codec
2762-
except ImportError:
2763-
def _forget_codec(codec_name):
2764-
pass
27652757

27662758

27672759
class ExceptionChainingTest(unittest.TestCase):
27682760

27692761
def setUp(self):
2770-
# There's no way to unregister a codec search function, so we just
2771-
# ensure we render this one fairly harmless after the test
2772-
# case finishes by using the test case repr as the codec name
2773-
# The codecs module normalizes codec names, although this doesn't
2774-
# appear to be formally documented...
2775-
# We also make sure we use a truly unique id for the custom codec
2776-
# to avoid issues with the codec cache when running these tests
2777-
# multiple times (e.g. when hunting for refleaks)
2778-
unique_id = repr(self) + str(id(self))
2779-
self.codec_name = encodings.normalize_encoding(unique_id).lower()
2762+
self.codec_name = 'exception_chaining_test'
2763+
codecs.register(_get_test_codec)
2764+
self.addCleanup(codecs.unregister, _get_test_codec)
27802765

27812766
# We store the object to raise on the instance because of a bad
27822767
# interaction between the codec caching (which means we can't
@@ -2791,10 +2776,6 @@ def tearDown(self):
27912776
_TEST_CODECS.pop(self.codec_name, None)
27922777
# Issue #22166: Also pop from caches to avoid appearance of ref leaks
27932778
encodings._cache.pop(self.codec_name, None)
2794-
try:
2795-
_forget_codec(self.codec_name)
2796-
except KeyError:
2797-
pass
27982779

27992780
def set_codec(self, encode, decode):
28002781
codec_info = codecs.CodecInfo(encode, decode,

Lib/test/test_io.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -2529,10 +2529,6 @@ def lookupTestDecoder(cls, name):
25292529
streamreader=None, streamwriter=None,
25302530
incrementaldecoder=cls)
25312531

2532-
# Register the previous decoder for testing.
2533-
# Disabled by default, tests will enable it.
2534-
codecs.register(StatefulIncrementalDecoder.lookupTestDecoder)
2535-
25362532

25372533
class StatefulIncrementalDecoderTest(unittest.TestCase):
25382534
"""
@@ -2583,6 +2579,9 @@ def setUp(self):
25832579
self.testdata = b"AAA\r\nBBB\rCCC\r\nDDD\nEEE\r\n"
25842580
self.normalized = b"AAA\nBBB\nCCC\nDDD\nEEE\n".decode("ascii")
25852581
os_helper.unlink(os_helper.TESTFN)
2582+
codecs.register(StatefulIncrementalDecoder.lookupTestDecoder)
2583+
self.addCleanup(codecs.unregister,
2584+
StatefulIncrementalDecoder.lookupTestDecoder)
25862585

25872586
def tearDown(self):
25882587
os_helper.unlink(os_helper.TESTFN)

Lib/test/test_unicode.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ def decode2(input, errors="strict"):
3636
return (encode2, decode2, None, None)
3737
else:
3838
return None
39-
codecs.register(search_function)
4039

4140
def duplicate_string(text):
4241
"""
@@ -58,6 +57,10 @@ class UnicodeTest(string_tests.CommonTest,
5857

5958
type2test = str
6059

60+
def setUp(self):
61+
codecs.register(search_function)
62+
self.addCleanup(codecs.unregister, search_function)
63+
6164
def checkequalnofix(self, result, object, methodname, *args):
6265
method = getattr(object, methodname)
6366
realresult = method(*args)

Modules/_codecsmodule.c

-20
Original file line numberDiff line numberDiff line change
@@ -160,25 +160,6 @@ _codecs_decode_impl(PyObject *module, PyObject *obj, const char *encoding,
160160

161161
/* --- Helpers ------------------------------------------------------------ */
162162

163-
/*[clinic input]
164-
_codecs._forget_codec
165-
166-
encoding: str
167-
/
168-
169-
Purge the named codec from the internal codec lookup cache
170-
[clinic start generated code]*/
171-
172-
static PyObject *
173-
_codecs__forget_codec_impl(PyObject *module, const char *encoding)
174-
/*[clinic end generated code: output=0bde9f0a5b084aa2 input=18d5d92d0e386c38]*/
175-
{
176-
if (_PyCodec_Forget(encoding) < 0) {
177-
return NULL;
178-
};
179-
Py_RETURN_NONE;
180-
}
181-
182163
static
183164
PyObject *codec_tuple(PyObject *decoded,
184165
Py_ssize_t len)
@@ -1057,7 +1038,6 @@ static PyMethodDef _codecs_functions[] = {
10571038
_CODECS_CODE_PAGE_DECODE_METHODDEF
10581039
_CODECS_REGISTER_ERROR_METHODDEF
10591040
_CODECS_LOOKUP_ERROR_METHODDEF
1060-
_CODECS__FORGET_CODEC_METHODDEF
10611041
{NULL, NULL} /* sentinel */
10621042
};
10631043

Modules/clinic/_codecsmodule.c.h

+1-38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/codecs.c

-25
Original file line numberDiff line numberDiff line change
@@ -208,31 +208,6 @@ PyObject *_PyCodec_Lookup(const char *encoding)
208208
return NULL;
209209
}
210210

211-
int _PyCodec_Forget(const char *encoding)
212-
{
213-
PyObject *v;
214-
int result;
215-
216-
PyInterpreterState *interp = _PyInterpreterState_GET();
217-
if (interp->codec_search_path == NULL) {
218-
return -1;
219-
}
220-
221-
/* Convert the encoding to a normalized Python string: all
222-
characters are converted to lower case, spaces and hyphens are
223-
replaced with underscores. */
224-
v = normalizestring(encoding);
225-
if (v == NULL) {
226-
return -1;
227-
}
228-
229-
/* Drop the named codec from the internal cache */
230-
result = PyDict_DelItem(interp->codec_search_cache, v);
231-
Py_DECREF(v);
232-
233-
return result;
234-
}
235-
236211
/* Codec registry encoding check API. */
237212

238213
int PyCodec_KnownEncoding(const char *encoding)

0 commit comments

Comments
 (0)