From d332e7b8164c3c9c885b9e631f33d9517b628b75 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Tue, 29 Sep 2020 05:41:11 +0800 Subject: bpo-41842: Add codecs.unregister() function (GH-22360) Add codecs.unregister() and PyCodec_Unregister() functions to unregister a codec search function. --- Python/codecs.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'Python/codecs.c') diff --git a/Python/codecs.c b/Python/codecs.c index 0f18c27e5f..a8233a73c4 100644 --- a/Python/codecs.c +++ b/Python/codecs.c @@ -50,6 +50,31 @@ int PyCodec_Register(PyObject *search_function) return -1; } +int +PyCodec_Unregister(PyObject *search_function) +{ + PyInterpreterState *interp = PyInterpreterState_Get(); + PyObject *codec_search_path = interp->codec_search_path; + /* Do nothing if codec_search_path is not created yet or was cleared. */ + if (codec_search_path == NULL) { + return 0; + } + + assert(PyList_CheckExact(codec_search_path)); + Py_ssize_t n = PyList_GET_SIZE(codec_search_path); + for (Py_ssize_t i = 0; i < n; i++) { + PyObject *item = PyList_GET_ITEM(codec_search_path, i); + if (item == search_function) { + if (interp->codec_search_cache != NULL) { + assert(PyDict_CheckExact(interp->codec_search_cache)); + PyDict_Clear(interp->codec_search_cache); + } + return PyList_SetSlice(codec_search_path, i, i+1, NULL); + } + } + return 0; +} + extern int _Py_normalize_encoding(const char *, char *, size_t); /* Convert a string to a normalized Python string(decoded from UTF-8): all characters are -- cgit v1.2.1 From c9f696cb96d1c362d5cad871f61da520572d9b08 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Fri, 16 Oct 2020 16:34:15 +0800 Subject: 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() --- Python/codecs.c | 25 ------------------------- 1 file changed, 25 deletions(-) (limited to 'Python/codecs.c') diff --git a/Python/codecs.c b/Python/codecs.c index a8233a73c4..ade1418720 100644 --- a/Python/codecs.c +++ b/Python/codecs.c @@ -208,31 +208,6 @@ PyObject *_PyCodec_Lookup(const char *encoding) return NULL; } -int _PyCodec_Forget(const char *encoding) -{ - PyObject *v; - int result; - - PyInterpreterState *interp = _PyInterpreterState_GET(); - if (interp->codec_search_path == NULL) { - return -1; - } - - /* Convert the encoding to a normalized Python string: all - characters are converted to lower case, spaces and hyphens are - replaced with underscores. */ - v = normalizestring(encoding); - if (v == NULL) { - return -1; - } - - /* Drop the named codec from the internal cache */ - result = PyDict_DelItem(interp->codec_search_cache, v); - Py_DECREF(v); - - return result; -} - /* Codec registry encoding check API. */ int PyCodec_KnownEncoding(const char *encoding) -- cgit v1.2.1