mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
bpo-41842: Add codecs.unregister() function (GH-22360)
Add codecs.unregister() and PyCodec_Unregister() functions to unregister a codec search function.
This commit is contained in:
parent
24ba3b0df5
commit
d332e7b816
11 changed files with 108 additions and 5 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue