8000 bpo-41842: Add a unregister function in _codecs module by shihai1991 · Pull Request #22360 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-41842: Add a unregister function in _codecs module #22360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
apply victor's comment
  • Loading branch information
shihai1991 committed Sep 23, 2020
commit 71b624e3b6d09884b80670b98896d6638826db42
3 changes: 1 addition & 2 deletions Doc/library/codecs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,8 @@ function:

.. function:: unregister(search_function)

Unregister a codec search function from the codecs registry.
Unregister a codec search function and clear the registry's cache.
If the search function is not registered, do nothing.
Clear the registry's cache if the codec search function is removed.

.. versionadded:: 3.10

Expand Down
2 changes: 1 addition & 1 deletion Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Base32 Encoding with Extended Hex Alphabet.
codecs
------

Add :func:`_codecs.unregister` to unregister a codec search function.
Add :func:`codecs.unregister` to unregister a codec search function.

curses
------
Expand Down
14 changes: 10 additions & 4 deletions I 10000 nclude/codecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ PyAPI_FUNC(int) PyCodec_Register(
PyObject *search_function
);

/* Unregister a codec search function.

Unregister a codec search function and clear the registry's cache.
If the search function is not registered, do nothing.
*/

PyAPI_FUNC(int) PyCodec_Unregister(
PyObject *search_function
);

/* Codec registry lookup API.

Looks up the given encoding and returns a CodecInfo object with
Expand All @@ -53,10 +63,6 @@ PyAPI_FUNC(PyObject *) _PyCodec_Lookup(
PyAPI_FUNC(int) _PyCodec_Forget(
const char *encoding
);

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

/* Codec registry encoding check API.
Expand Down
9 changes: 5 additions & 4 deletions Lib/test/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1642,10 +1642,11 @@ def test_register(self):
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)
search_function = mock.Mock(return_value=(1, 2, 3, 4))
codecs.register(search_function)
codecs.unregister(search_function)
self.assertRaises(LookupError, codecs.lookup, "test")
search_function.assert_not_called()

def test_lookup(self):
self.assertRaises(TypeError, codecs.lookup)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Add a :func:`_codecs.unregister` to unregister a codec search function.
Add a :func:`codecs.unregister` to unregister a codec search function.
6 changes: 3 additions & 3 deletions Modules/_codecsmodule.c
341A
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ _codecs.unregister
search_function: object
/

Unregister a codec search function.
Unregister a codec search function and clear the registry's cache.

If the search function is not registered, do nothing.
[clinic start generated code]*/

static PyObject *
_codecs_unregister(PyObject *module, PyObject *search_function)
/*[clinic end generated code: output=1f0edee9cf246399 input=8bfb99685e00d653]*/
/*[clinic end generated code: output=1f0edee9cf246399 input=dd7c004c652d345e]*/
{
if (_PyCodec_Unregister(search_function)) {
if (PyCodec_Unregister(search_function) < 0) {
return NULL;
}

Expand Down
4 changes: 2 additions & 2 deletions Modules/clinic/_codecsmodule.c.h

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

4 changes: 2 additions & 2 deletions Python/codecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ int PyCodec_Register(PyObject *search_function)
}

int
_PyCodec_Unregister(PyObject *search_function)
PyCodec_Unregister(PyObject *search_function)
{
PyInterpreterState *interp = PyInterpreterState_Get();
/* Do nothing if codec_search_path is not created yet or was created. */
Expand All @@ -63,10 +63,10 @@ _PyCodec_Unregister(PyObject *search_function)
for (Py_ssize_t i = 0; i < n; i++) {
PyObject *item = PyList_GetItem(interp->codec_search_path, i);
if (item == search_function) {
PyDict_Clear(interp->codec_search_cache);
return PyList_SetSlice(interp->codec_search_path, i, i+1, NULL);
}
}
PyDict_Clear(interp->codec_search_cache);
return 0;
}

Expand Down
0