From e9ecac068485efd7947340cbfaf819b9da1289ca Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Thu, 19 Mar 2020 18:32:46 +0800 Subject: [PATCH 01/15] add a test case to against nomralizeing() of codecs --- Lib/test/test_codecs.py | 16 ++++++++++++++++ Modules/_testcapimodule.c | 1 - Modules/_testinternalcapi.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 54a3520802a4f3..81d456f34d721d 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -8,6 +8,7 @@ from unittest import mock from test import support +import _testinternalcapi try: import _testcapi @@ -48,6 +49,7 @@ class CPINFOEXW(ctypes.Structure): info = CPINFOEXW() return GetCPInfoEx(cp, 0, info) + class Queue(object): """ queue: write bytes at one end, read bytes from the other end @@ -3401,5 +3403,19 @@ def test_rot13_func(self): 'To be, or not to be, that is the question') +class ZNormalizedTest(unittest.TestCase): + """Test the normalizestring function via codecs module""" + def test_normalized_encoding(self): + def search_function(encoding): + if encoding == "aaa_8": + return (1, 2, 3, 4) + else: + return (None, None, None, None) + _testinternalcapi.codecs_unregister() + codecs.register(search_function) + self.assertEqual((1, 2, 3, 4), codecs.lookup('AAA-8')) + self.assertEqual((None, None, None, None), codecs.lookup('BBB-8')) + + if __name__ == "__main__": unittest.main() diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 3cc558689b6c18..bcd9fc32ed2541 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -2155,7 +2155,6 @@ codec_incrementaldecoder(PyObject *self, PyObject *args) return PyCodec_IncrementalDecoder(encoding, errors); } - /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ static PyObject * test_long_numbits(PyObject *self, PyObject *Py_UNUSED(ignored)) diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index 394b870e907800..d0ddbcba9ff544 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -10,6 +10,7 @@ #include "Python.h" #include "pycore_initconfig.h" // _Py_GetConfigsAsDict() +#include "pycore_pystate.h" static PyObject * @@ -29,9 +30,37 @@ get_recursion_depth(PyObject *self, PyObject *args) } +static PyObject * +codecs_unregister(PyObject *self, PyObject *Py_UNUSED(args)) +{ + PyListObject *codec_search_path; + PyObject **item; + Py_ssize_t i; + + PyThreadState *tstate = PyThreadState_Get(); + PyInterpreterState *interp = PyThreadState_GetInterpreter(tstate); + codec_search_path = (PyListObject *)interp->codec_search_path; + item = codec_search_path->ob_item; + + if (item != NULL) { + i = Py_SIZE(codec_search_path); + Py_SET_SIZE(codec_search_path, 0); + codec_search_path->ob_item = NULL; + codec_search_path->allocated = 0; + while (--i >= 0) { + Py_XDECREF(item[i]); + } + } + PyMem_FREE(item); + + Py_RETURN_NONE; +} + + static PyMethodDef TestMethods[] = { {"get_configs", get_configs, METH_NOARGS}, {"get_recursion_depth", get_recursion_depth, METH_NOARGS}, + {"codecs_unregister", codecs_unregister, METH_NOARGS}, {NULL, NULL} /* sentinel */ }; From cf46290d67ef8fed5b1938dc9d59ab99c64d4bec Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Thu, 19 Mar 2020 18:36:37 +0800 Subject: [PATCH 02/15] remove redundant operation --- Lib/test/test_codecs.py | 1 - Modules/_testcapimodule.c | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 81d456f34d721d..b67955c85bdc76 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -49,7 +49,6 @@ class CPINFOEXW(ctypes.Structure): info = CPINFOEXW() return GetCPInfoEx(cp, 0, info) - class Queue(object): """ queue: write bytes at one end, read bytes from the other end diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index bcd9fc32ed2541..3cc558689b6c18 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -2155,6 +2155,7 @@ codec_incrementaldecoder(PyObject *self, PyObject *args) return PyCodec_IncrementalDecoder(encoding, errors); } + /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ static PyObject * test_long_numbits(PyObject *self, PyObject *Py_UNUSED(ignored)) From 59911e1e4e1fc3f4f47eb3c2656dacb1896aad42 Mon Sep 17 00:00:00 2001 From: hai shi Date: Sat, 21 Mar 2020 01:10:56 +0800 Subject: [PATCH 03/15] not clear all codec_search_paths --- Modules/_testinternalcapi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index d0ddbcba9ff544..ab97d740b8f287 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -47,7 +47,7 @@ codecs_unregister(PyObject *self, PyObject *Py_UNUSED(args)) Py_SET_SIZE(codec_search_path, 0); codec_search_path->ob_item = NULL; codec_search_path->allocated = 0; - while (--i >= 0) { + while (--i > 0) { Py_XDECREF(item[i]); } } From aa3c4d0a2ad917ab638fe2ab7db2a13212a5dfe9 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Sat, 21 Mar 2020 16:58:02 +0800 Subject: [PATCH 04/15] remove codecs_unregister() --- Lib/test/test_codecs.py | 3 +-- Modules/_testinternalcapi.c | 28 ---------------------------- 2 files changed, 1 insertion(+), 30 deletions(-) diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index b67955c85bdc76..e6f5f93f6c199a 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -3402,7 +3402,7 @@ def test_rot13_func(self): 'To be, or not to be, that is the question') -class ZNormalizedTest(unittest.TestCase): +class NormalizedTest(unittest.TestCase): """Test the normalizestring function via codecs module""" def test_normalized_encoding(self): def search_function(encoding): @@ -3410,7 +3410,6 @@ def search_function(encoding): return (1, 2, 3, 4) else: return (None, None, None, None) - _testinternalcapi.codecs_unregister() codecs.register(search_function) self.assertEqual((1, 2, 3, 4), codecs.lookup('AAA-8')) self.assertEqual((None, None, None, None), codecs.lookup('BBB-8')) diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index ab97d740b8f287..02d6c8237772c2 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -30,37 +30,9 @@ get_recursion_depth(PyObject *self, PyObject *args) } -static PyObject * -codecs_unregister(PyObject *self, PyObject *Py_UNUSED(args)) -{ - PyListObject *codec_search_path; - PyObject **item; - Py_ssize_t i; - - PyThreadState *tstate = PyThreadState_Get(); - PyInterpreterState *interp = PyThreadState_GetInterpreter(tstate); - codec_search_path = (PyListObject *)interp->codec_search_path; - item = codec_search_path->ob_item; - - if (item != NULL) { - i = Py_SIZE(codec_search_path); - Py_SET_SIZE(codec_search_path, 0); - codec_search_path->ob_item = NULL; - codec_search_path->allocated = 0; - while (--i > 0) { - Py_XDECREF(item[i]); - } - } - PyMem_FREE(item); - - Py_RETURN_NONE; -} - - static PyMethodDef TestMethods[] = { {"get_configs", get_configs, METH_NOARGS}, {"get_recursion_depth", get_recursion_depth, METH_NOARGS}, - {"codecs_unregister", codecs_unregister, METH_NOARGS}, {NULL, NULL} /* sentinel */ }; From aa4bf51728f2f26260e5fb40112c537e8a68d222 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Sat, 21 Mar 2020 16:59:19 +0800 Subject: [PATCH 05/15] remove useless head file --- Modules/_testinternalcapi.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index 02d6c8237772c2..394b870e907800 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -10,7 +10,6 @@ #include "Python.h" #include "pycore_initconfig.h" // _Py_GetConfigsAsDict() -#include "pycore_pystate.h" static PyObject * From 8ce5956a87a23a6ba9ec8f0fafa8100694b0373d Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Sat, 21 Mar 2020 17:30:37 +0800 Subject: [PATCH 06/15] remove useless import --- Lib/test/test_codecs.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index e6f5f93f6c199a..65f154ae9a0aab 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -8,7 +8,6 @@ from unittest import mock from test import support -import _testinternalcapi try: import _testcapi From 41a1672934bdd8784f19129742deaa7a3287762f Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Fri, 3 Apr 2020 01:56:05 +0800 Subject: [PATCH 07/15] retrigger checks From f48e5d9465da9139ae20970b8ded795f4a61ecc3 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Thu, 10 Sep 2020 12:40:57 +0800 Subject: [PATCH 08/15] add codecs_unregister() in testinternalcapi --- Lib/test/test_codecs.py | 2 ++ Modules/_testinternalcapi.c | 41 +++++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 6493891ca19c01..5a4ef3e5a35f3b 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -10,6 +10,7 @@ from test import support from test.support import os_helper from test.support import warnings_helper +import _testinternalcapi try: import _testcapi @@ -3414,6 +3415,7 @@ def search_function(encoding): codecs.register(search_function) self.assertEqual((1, 2, 3, 4), codecs.lookup('AAA-8')) self.assertEqual((None, None, None, None), codecs.lookup('BBB-8')) + _testinternalcapi.codecs_unregister(search_function) if __name__ == "__main__": diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index ad74af8363ef45..e52aa75d62339d 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -12,10 +12,11 @@ #define PY_SSIZE_T_CLEAN #include "Python.h" -#include "pycore_bitutils.h" // _Py_bswap32() -#include "pycore_initconfig.h" // _Py_GetConfigsAsDict() -#include "pycore_hashtable.h" // _Py_hashtable_new() -#include "pycore_gc.h" // PyGC_Head +#include "pycore_bitutils.h" // _Py_bswap32() +#include "pycore_initconfig.h" // _Py_GetConfigsAsDict() +#include "pycore_hashtable.h" // _Py_hashtable_new() +#include "pycore_gc.h" // PyGC_Head +#include "internal/pycore_interp.h" // PyInterpreterState_Get() static PyObject * @@ -35,6 +36,37 @@ get_recursion_depth(PyObject *self, PyObject *Py_UNUSED(args)) } +static PyObject* +codecs_unregister(PyObject *self, PyObject *args) +{ + PyObject *search_function; + + if (!PyArg_ParseTuple(args, "O:codecs_unregister", + &search_function)) { + } + if (search_function == NULL) { + return NULL; + } + + PyInterpreterState *interp = PyInterpreterState_Get(); + if (interp->codec_search_path == NULL) { + return NULL; + } + + Py_ssize_t n = PyList_Size(interp->codec_search_path); + PyObject *list = PyList_New(0); + for (Py_ssize_t i = 0; i < n; i++) { + PyObject *item = PyList_GetItem(interp->codec_search_path, i); + if (item != search_function) { + PyList_Append(list, item); + } + } + Py_SETREF(interp->codec_search_path, list); + + Py_RETURN_NONE; +} + + static PyObject* test_bswap(PyObject *self, PyObject *Py_UNUSED(args)) { @@ -234,6 +266,7 @@ test_hashtable(PyObject *self, PyObject *Py_UNUSED(args)) static PyMethodDef TestMethods[] = { {"get_configs", get_configs, METH_NOARGS}, {"get_recursion_depth", get_recursion_depth, METH_NOARGS}, + {"codecs_unregister", codecs_unregister, METH_VARARGS}, {"test_bswap", test_bswap, METH_NOARGS}, {"test_popcount", test_popcount, METH_NOARGS}, {"test_bit_length", test_bit_length, METH_NOARGS}, From 379542001a2fd93acdf06d98b304aaf85b6d3e35 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Thu, 10 Sep 2020 20:24:47 +0800 Subject: [PATCH 09/15] update code --- Modules/_testinternalcapi.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index e52aa75d62339d..b71b7b0cf20892 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -43,6 +43,7 @@ codecs_unregister(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "O:codecs_unregister", &search_function)) { + return NULL; } if (search_function == NULL) { return NULL; From a2887f3fb8252186e708705af5addd4987591050 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Tue, 29 Sep 2020 12:17:02 +0800 Subject: [PATCH 10/15] Using codecs.unregister() in test case --- Lib/test/test_codecs.py | 3 +-- Modules/_testinternalcapi.c | 42 ++++--------------------------------- 2 files changed, 5 insertions(+), 40 deletions(-) diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index c2bbc0ea490f0b..c33c62917167dd 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -10,7 +10,6 @@ from test import support from test.support import os_helper from test.support import warnings_helper -import _testinternalcapi try: import _testcapi @@ -3427,7 +3426,7 @@ def search_function(encoding): codecs.register(search_function) self.assertEqual((1, 2, 3, 4), codecs.lookup('AAA-8')) self.assertEqual((None, None, None, None), codecs.lookup('BBB-8')) - _testinternalcapi.codecs_unregister(search_function) + codecs.unregister(search_function) if __name__ == "__main__": diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index b71b7b0cf20892..ad74af8363ef45 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -12,11 +12,10 @@ #define PY_SSIZE_T_CLEAN #include "Python.h" -#include "pycore_bitutils.h" // _Py_bswap32() -#include "pycore_initconfig.h" // _Py_GetConfigsAsDict() -#include "pycore_hashtable.h" // _Py_hashtable_new() -#include "pycore_gc.h" // PyGC_Head -#include "internal/pycore_interp.h" // PyInterpreterState_Get() +#include "pycore_bitutils.h" // _Py_bswap32() +#include "pycore_initconfig.h" // _Py_GetConfigsAsDict() +#include "pycore_hashtable.h" // _Py_hashtable_new() +#include "pycore_gc.h" // PyGC_Head static PyObject * @@ -36,38 +35,6 @@ get_recursion_depth(PyObject *self, PyObject *Py_UNUSED(args)) } -static PyObject* -codecs_unregister(PyObject *self, PyObject *args) -{ - PyObject *search_function; - - if (!PyArg_ParseTuple(args, "O:codecs_unregister", - &search_function)) { - return NULL; - } - if (search_function == NULL) { - return NULL; - } - - PyInterpreterState *interp = PyInterpreterState_Get(); - if (interp->codec_search_path == NULL) { - return NULL; - } - - Py_ssize_t n = PyList_Size(interp->codec_search_path); - PyObject *list = PyList_New(0); - for (Py_ssize_t i = 0; i < n; i++) { - PyObject *item = PyList_GetItem(interp->codec_search_path, i); - if (item != search_function) { - PyList_Append(list, item); - } - } - Py_SETREF(interp->codec_search_path, list); - - Py_RETURN_NONE; -} - - static PyObject* test_bswap(PyObject *self, PyObject *Py_UNUSED(args)) { @@ -267,7 +234,6 @@ test_hashtable(PyObject *self, PyObject *Py_UNUSED(args)) static PyMethodDef TestMethods[] = { {"get_configs", get_configs, METH_NOARGS}, {"get_recursion_depth", get_recursion_depth, METH_NOARGS}, - {"codecs_unregister", codecs_unregister, METH_VARARGS}, {"test_bswap", test_bswap, METH_NOARGS}, {"test_popcount", test_popcount, METH_NOARGS}, {"test_bit_length", test_bit_length, METH_NOARGS}, From 632a4480f506cd757981d1263e498f8b51d1de73 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Tue, 29 Sep 2020 19:04:54 +0800 Subject: [PATCH 11/15] apply Tal's comment --- Lib/test/test_codecs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index c33c62917167dd..6acc0355e15135 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -3415,7 +3415,7 @@ def test_rot13_func(self): 'To be, or not to be, that is the question') -class NormalizedTest(unittest.TestCase): +class CodecNameNormalizationTest(unittest.TestCase): """Test the normalizestring function via codecs module""" def test_normalized_encoding(self): def search_function(encoding): From bb6e78c8794b34edd467f89a6b6854955219b482 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Sat, 3 Oct 2020 13:03:08 +0800 Subject: [PATCH 12/15] apply victor's comment --- Lib/test/test_codecs.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 6acc0355e15135..7364e0b254c93a 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -3418,14 +3418,23 @@ def test_rot13_func(self): class CodecNameNormalizationTest(unittest.TestCase): """Test the normalizestring function via codecs module""" def test_normalized_encoding(self): + FOUND = (1, 2, 3, 4) + NOT_FOUND = (None, None, None, None) def search_function(encoding): if encoding == "aaa_8": - return (1, 2, 3, 4) + return FOUND else: - return (None, None, None, None) + return NOT_FOUND + codecs.register(search_function) - self.assertEqual((1, 2, 3, 4), codecs.lookup('AAA-8')) - self.assertEqual((None, None, None, None), codecs.lookup('BBB-8')) + self.assertEqual(FOUND, codecs.lookup('AAA-8')) + self.assertEqual(FOUND, codecs.lookup('AAA---8')) + self.assertEqual(FOUND, codecs.lookup('AAA 8')) + self.assertEqual(NOT_FOUND, codecs.lookup('AAA.8')) + self.assertEqual(NOT_FOUND, codecs.lookup('AAA...8')) + self.assertEqual(NOT_FOUND, codecs.lookup('BBB-8')) + self.assertEqual(NOT_FOUND, codecs.lookup('BBB.8')) + self.assertEqual(NOT_FOUND, codecs.lookup('a\xe9\u20ac-8')) codecs.unregister(search_function) From 72e243557cbe7f9df7ed52dcaf1f63fc6f5a326b Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Sat, 3 Oct 2020 16:25:02 +0800 Subject: [PATCH 13/15] apply tal's comment --- Lib/test/test_codecs.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 7364e0b254c93a..92af75210ad8ad 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -3416,7 +3416,7 @@ def test_rot13_func(self): class CodecNameNormalizationTest(unittest.TestCase): - """Test the normalizestring function via codecs module""" + """Test codec name normalization""" def test_normalized_encoding(self): FOUND = (1, 2, 3, 4) NOT_FOUND = (None, None, None, None) @@ -3427,6 +3427,7 @@ def search_function(encoding): return NOT_FOUND codecs.register(search_function) + self.assertEqual(FOUND, codecs.lookup('aaa_8')) self.assertEqual(FOUND, codecs.lookup('AAA-8')) self.assertEqual(FOUND, codecs.lookup('AAA---8')) self.assertEqual(FOUND, codecs.lookup('AAA 8')) From 26fcbe4e6e08c932a491b6f0ab12427c92568505 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Mon, 5 Oct 2020 23:31:21 +0800 Subject: [PATCH 14/15] apply victor's comment --- Lib/test/test_codecs.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 92af75210ad8ad..76730e0ce72401 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -3431,12 +3431,13 @@ def search_function(encoding): self.assertEqual(FOUND, codecs.lookup('AAA-8')) self.assertEqual(FOUND, codecs.lookup('AAA---8')) self.assertEqual(FOUND, codecs.lookup('AAA 8')) + self.assertEqual(FOUND, codecs.lookup('aaa\xe9\u20ac-8')) self.assertEqual(NOT_FOUND, codecs.lookup('AAA.8')) self.assertEqual(NOT_FOUND, codecs.lookup('AAA...8')) self.assertEqual(NOT_FOUND, codecs.lookup('BBB-8')) self.assertEqual(NOT_FOUND, codecs.lookup('BBB.8')) self.assertEqual(NOT_FOUND, codecs.lookup('a\xe9\u20ac-8')) - codecs.unregister(search_function) + self.addCleanup(codecs.unregister, search_function) if __name__ == "__main__": From 37b80e072979ee0b254a02b146d239071b793615 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Thu, 8 Oct 2020 22:00:40 +0800 Subject: [PATCH 15/15] apply victor's comment --- Lib/test/test_codecs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 76730e0ce72401..ddf4e08af6247a 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -3427,6 +3427,7 @@ def search_function(encoding): return NOT_FOUND codecs.register(search_function) + self.addCleanup(codecs.unregister, search_function) self.assertEqual(FOUND, codecs.lookup('aaa_8')) self.assertEqual(FOUND, codecs.lookup('AAA-8')) self.assertEqual(FOUND, codecs.lookup('AAA---8')) @@ -3437,7 +3438,6 @@ def search_function(encoding): self.assertEqual(NOT_FOUND, codecs.lookup('BBB-8')) self.assertEqual(NOT_FOUND, codecs.lookup('BBB.8')) self.assertEqual(NOT_FOUND, codecs.lookup('a\xe9\u20ac-8')) - self.addCleanup(codecs.unregister, search_function) if __name__ == "__main__":