mirror of
https://github.com/python/cpython.git
synced 2025-08-22 17:55:18 +00:00
gh-124665: Add _PyCodec_UnregisterError
and _codecs._unregister_error
(#124677)
This commit is contained in:
parent
04c837d9d8
commit
c00964ecd5
6 changed files with 136 additions and 2 deletions
|
@ -16,6 +16,12 @@ Copyright (c) Corporation for National Research Initiatives.
|
|||
#include "pycore_pystate.h" // _PyInterpreterState_GET()
|
||||
#include "pycore_ucnhash.h" // _PyUnicode_Name_CAPI
|
||||
|
||||
static const char *codecs_builtin_error_handlers[] = {
|
||||
"strict", "ignore", "replace",
|
||||
"xmlcharrefreplace", "backslashreplace", "namereplace",
|
||||
"surrogatepass", "surrogateescape",
|
||||
};
|
||||
|
||||
const char *Py_hexdigits = "0123456789abcdef";
|
||||
|
||||
/* --- Codec Registry ----------------------------------------------------- */
|
||||
|
@ -618,6 +624,20 @@ int PyCodec_RegisterError(const char *name, PyObject *error)
|
|||
name, error);
|
||||
}
|
||||
|
||||
int _PyCodec_UnregisterError(const char *name)
|
||||
{
|
||||
for (size_t i = 0; i < Py_ARRAY_LENGTH(codecs_builtin_error_handlers); ++i) {
|
||||
if (strcmp(name, codecs_builtin_error_handlers[i]) == 0) {
|
||||
PyErr_Format(PyExc_ValueError,
|
||||
"cannot un-register built-in error handler '%s'", name);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
PyInterpreterState *interp = _PyInterpreterState_GET();
|
||||
assert(interp->codecs.initialized);
|
||||
return PyDict_PopString(interp->codecs.error_registry, name, NULL);
|
||||
}
|
||||
|
||||
/* Lookup the error handling callback function registered under the
|
||||
name error. As a special case NULL can be passed, in which case
|
||||
the error handling callback for strict encoding will be returned. */
|
||||
|
@ -1470,6 +1490,8 @@ _PyCodec_InitRegistry(PyInterpreterState *interp)
|
|||
}
|
||||
}
|
||||
};
|
||||
// ensure that the built-in error handlers' names are kept in sync
|
||||
assert(Py_ARRAY_LENGTH(methods) == Py_ARRAY_LENGTH(codecs_builtin_error_handlers));
|
||||
|
||||
assert(interp->codecs.initialized == 0);
|
||||
interp->codecs.search_path = PyList_New(0);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue