diff options
| author | Hai Shi <shihai1992@gmail.com> | 2020-09-29 05:41:11 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-28 23:41:11 +0200 |
| commit | d332e7b8164c3c9c885b9e631f33d9517b628b75 (patch) | |
| tree | 8e19b9ecde99e301d5211595861898ef13cef69a /Python/codecs.c | |
| parent | 24ba3b0df5e5f2f237d7b23b4017ba12f16320ae (diff) | |
| download | cpython-git-d332e7b8164c3c9c885b9e631f33d9517b628b75.tar.gz | |
bpo-41842: Add codecs.unregister() function (GH-22360)
Add codecs.unregister() and PyCodec_Unregister() functions
to unregister a codec search function.
Diffstat (limited to 'Python/codecs.c')
| -rw-r--r-- | Python/codecs.c | 25 |
1 files changed, 25 insertions, 0 deletions
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 |
