summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>2024-09-29 00:25:23 (GMT)
committerGitHub <noreply@github.com>2024-09-29 00:25:23 (GMT)
commitc00964ecd508ba6ae43498017d5a3873844a058a (patch)
tree5a31d8510a4e248d98bc60f473b6b95116263f21 /Modules
parent04c837d9d8a474777ef9c1412fbba14f0682366c (diff)
downloadcpython-c00964ecd508ba6ae43498017d5a3873844a058a.zip
cpython-c00964ecd508ba6ae43498017d5a3873844a058a.tar.gz
cpython-c00964ecd508ba6ae43498017d5a3873844a058a.tar.bz2
gh-124665: Add `_PyCodec_UnregisterError` and `_codecs._unregister_error` (#124677)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_codecsmodule.c25
-rw-r--r--Modules/clinic/_codecsmodule.c.h52
2 files changed, 76 insertions, 1 deletions
diff --git a/Modules/_codecsmodule.c b/Modules/_codecsmodule.c
index 32373f0..471b42b 100644
--- a/Modules/_codecsmodule.c
+++ b/Modules/_codecsmodule.c
@@ -980,6 +980,30 @@ _codecs_register_error_impl(PyObject *module, const char *errors,
}
/*[clinic input]
+_codecs._unregister_error -> bool
+ errors: str
+ /
+
+Un-register the specified error handler for the error handling `errors'.
+
+Only custom error handlers can be un-registered. An exception is raised
+if the error handling is a built-in one (e.g., 'strict'), or if an error
+occurs.
+
+Otherwise, this returns True if a custom handler has been successfully
+un-registered, and False if no custom handler for the specified error
+handling exists.
+
+[clinic start generated code]*/
+
+static int
+_codecs__unregister_error_impl(PyObject *module, const char *errors)
+/*[clinic end generated code: output=28c22be667465503 input=a63ab9e9ce1686d4]*/
+{
+ return _PyCodec_UnregisterError(errors);
+}
+
+/*[clinic input]
_codecs.lookup_error
name: str
/
@@ -1044,6 +1068,7 @@ static PyMethodDef _codecs_functions[] = {
_CODECS_CODE_PAGE_ENCODE_METHODDEF
_CODECS_CODE_PAGE_DECODE_METHODDEF
_CODECS_REGISTER_ERROR_METHODDEF
+ _CODECS__UNREGISTER_ERROR_METHODDEF
_CODECS_LOOKUP_ERROR_METHODDEF
{NULL, NULL} /* sentinel */
};
diff --git a/Modules/clinic/_codecsmodule.c.h b/Modules/clinic/_codecsmodule.c.h
index 1c0f374..01855ae 100644
--- a/Modules/clinic/_codecsmodule.c.h
+++ b/Modules/clinic/_codecsmodule.c.h
@@ -2683,6 +2683,56 @@ exit:
return return_value;
}
+PyDoc_STRVAR(_codecs__unregister_error__doc__,
+"_unregister_error($module, errors, /)\n"
+"--\n"
+"\n"
+"Un-register the specified error handler for the error handling `errors\'.\n"
+"\n"
+"Only custom error handlers can be un-registered. An exception is raised\n"
+"if the error handling is a built-in one (e.g., \'strict\'), or if an error\n"
+"occurs.\n"
+"\n"
+"Otherwise, this returns True if a custom handler has been successfully\n"
+"un-registered, and False if no custom handler for the specified error\n"
+"handling exists.");
+
+#define _CODECS__UNREGISTER_ERROR_METHODDEF \
+ {"_unregister_error", (PyCFunction)_codecs__unregister_error, METH_O, _codecs__unregister_error__doc__},
+
+static int
+_codecs__unregister_error_impl(PyObject *module, const char *errors);
+
+static PyObject *
+_codecs__unregister_error(PyObject *module, PyObject *arg)
+{
+ PyObject *return_value = NULL;
+ const char *errors;
+ int _return_value;
+
+ if (!PyUnicode_Check(arg)) {
+ _PyArg_BadArgument("_unregister_error", "argument", "str", arg);
+ goto exit;
+ }
+ Py_ssize_t errors_length;
+ errors = PyUnicode_AsUTF8AndSize(arg, &errors_length);
+ if (errors == NULL) {
+ goto exit;
+ }
+ if (strlen(errors) != (size_t)errors_length) {
+ PyErr_SetString(PyExc_ValueError, "embedded null character");
+ goto exit;
+ }
+ _return_value = _codecs__unregister_error_impl(module, errors);
+ if ((_return_value == -1) && PyErr_Occurred()) {
+ goto exit;
+ }
+ return_value = PyBool_FromLong((long)_return_value);
+
+exit:
+ return return_value;
+}
+
PyDoc_STRVAR(_codecs_lookup_error__doc__,
"lookup_error($module, name, /)\n"
"--\n"
@@ -2746,4 +2796,4 @@ exit:
#ifndef _CODECS_CODE_PAGE_ENCODE_METHODDEF
#define _CODECS_CODE_PAGE_ENCODE_METHODDEF
#endif /* !defined(_CODECS_CODE_PAGE_ENCODE_METHODDEF) */
-/*[clinic end generated code: output=e50d5fdf65bd45fa input=a9049054013a1b77]*/
+/*[clinic end generated code: output=b3013d4709d96ffe input=a9049054013a1b77]*/