diff options
author | Bénédikt Tran <10796600+picnixz@users.noreply.github.com> | 2024-09-29 00:25:23 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-29 00:25:23 (GMT) |
commit | c00964ecd508ba6ae43498017d5a3873844a058a (patch) | |
tree | 5a31d8510a4e248d98bc60f473b6b95116263f21 /Python/codecs.c | |
parent | 04c837d9d8a474777ef9c1412fbba14f0682366c (diff) | |
download | cpython-c00964ecd508ba6ae43498017d5a3873844a058a.zip cpython-c00964ecd508ba6ae43498017d5a3873844a058a.tar.gz cpython-c00964ecd508ba6ae43498017d5a3873844a058a.tar.bz2 |
gh-124665: Add `_PyCodec_UnregisterError` and `_codecs._unregister_error` (#124677)
Diffstat (limited to 'Python/codecs.c')
-rw-r--r-- | Python/codecs.c | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/Python/codecs.c b/Python/codecs.c index 9c0a3fa..68dc232 100644 --- a/Python/codecs.c +++ b/Python/codecs.c @@ -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); |