Skip to content

Commit

Permalink
add a _codecs.unregister in _codecs module
Browse files Browse the repository at this point in the history
  • Loading branch information
shihai1991 committed Sep 22, 2020
1 parent 68526fe commit fefda70
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 4 deletions.
13 changes: 10 additions & 3 deletions Doc/library/codecs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,16 @@ function:

.. note::

Search function registration is not currently reversible,
which may cause problems in some cases, such as unit testing or
module reloading.
Search function registration is reversible since Python 3.10.
You can use `codecs.unregister()` to unregister the search function.

.. function:: unregister(search_function)

Unregister a codec search function from the codecs registry. If
the search function haven't registered, this function dose nothing.

.. versionadded:: 3.10


While the builtin :func:`open` and the associated :mod:`io` module are the
recommended approach for working with encoded text files, this module
Expand Down
4 changes: 4 additions & 0 deletions Include/codecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ PyAPI_FUNC(PyObject *) _PyCodec_Lookup(
PyAPI_FUNC(int) _PyCodec_Forget(
const char *encoding
);

PyAPI_FUNC(PyObject *) _PyCodec_Unregister(
PyObject *search_function
);
#endif

/* Codec registry encoding check API.
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,12 @@ def test_register(self):
self.assertRaises(TypeError, codecs.register)
self.assertRaises(TypeError, codecs.register, 42)

def test_unregister(self):
def test_encoding(encoding):
return None
codecs.register(test_encoding)
self.assertEqual(codecs.unregister(test_encoding), None)

def test_lookup(self):
self.assertRaises(TypeError, codecs.lookup)
self.assertRaises(LookupError, codecs.lookup, "__spam__")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add a :func:`_codecs.unregister` which could unregister a codec search
function from the codec registry.
19 changes: 19 additions & 0 deletions Modules/_codecsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ _codecs_register(PyObject *module, PyObject *search_function)
Py_RETURN_NONE;
}

/*[clinic input]
_codecs.unregister
search_function: object
/
Unregister a codec search function.
Unregister a codec search function from codec registry. If the search
function haven't registered, this function does nothing.
[clinic start generated code]*/

static PyObject *
_codecs_unregister(PyObject *module, PyObject *search_function)
/*[clinic end generated code: output=1f0edee9cf246399 input=ecb26613941e6a47]*/
{
return _PyCodec_Unregister(search_function);
}

/*[clinic input]
_codecs.lookup
encoding: str
Expand Down Expand Up @@ -992,6 +1010,7 @@ _codecs_lookup_error_impl(PyObject *module, const char *name)

static PyMethodDef _codecs_functions[] = {
_CODECS_REGISTER_METHODDEF
_CODECS_UNREGISTER_METHODDEF
_CODECS_LOOKUP_METHODDEF
_CODECS_ENCODE_METHODDEF
_CODECS_DECODE_METHODDEF
Expand Down
14 changes: 13 additions & 1 deletion Modules/clinic/_codecsmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions Python/codecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ int PyCodec_Register(PyObject *search_function)
return -1;
}

PyObject *
_PyCodec_Unregister(PyObject *search_function)
{
PyInterpreterState *interp = PyInterpreterState_Get();
if (interp->codec_search_path == NULL) {
Py_RETURN_NONE;
}

Py_ssize_t n = PyList_Size(interp->codec_search_path);
PyObject *list = PyList_New(0);
for (Py_ssize_t i = 0; i < n; i++) {
PyObject *item = PyList_GetItem(interp->codec_search_path, i);
if (item != search_function) {
PyList_Append(list, item);
}
}
Py_SETREF(interp->codec_search_path, list);
Py_RETURN_NONE;
}

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
Expand Down

0 comments on commit fefda70

Please sign in to comment.