Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
fefda70f7775c071b624ed186726f8e912b40666fc3257b6ca493a35e29ec44db4631e5efff22f18cd4f8f6dc4d4308ea8c9a0c99File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -27,6 +27,14 @@ PyAPI_FUNC(int) PyCodec_Register( | ||
| PyObject *search_function | ||
| ); | ||
| /* Unregister a codec search function and clear the registry's cache. | ||
| If the search function is not registered, do nothing. | ||
| Return 0 on success. Raise an exception and return -1 on error. */ | ||
| PyAPI_FUNC(int) PyCodec_Unregister( | ||
| PyObject *search_function | ||
| ); | ||
shihai1991 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| /* Codec registry lookup API. | ||
| Looks up the given encoding and returns a CodecInfo object with | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1641,6 +1641,18 @@ def test_register(self): | ||
| self.assertRaises(TypeError, codecs.register) | ||
| self.assertRaises(TypeError, codecs.register, 42) | ||
| def test_unregister(self): | ||
| name = "nonexistent_codec_name" | ||
| search_function = mock.Mock() | ||
| codecs.register(search_function) | ||
shihai1991 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| self.assertRaises(TypeError, codecs.lookup, name) | ||
| search_function.assert_called_with(name) | ||
| search_function.reset_mock() | ||
shihai1991 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| codecs.unregister(search_function) | ||
| self.assertRaises(LookupError, codecs.lookup, name) | ||
| search_function.assert_not_called() | ||
| def test_lookup(self): | ||
| self.assertRaises(TypeError, codecs.lookup) | ||
| self.assertRaises(LookupError, codecs.lookup, "__spam__") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Add :c:func:`PyCodec_Unregister` function to unregister a codec search | ||
| function. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add :func:`codecs.unregister` function to unregister a codec search function. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -50,6 +50,31 @@ int PyCodec_Register(PyObject *search_function) | ||
| return -1; | ||
| } | ||
| int | ||
| PyCodec_Unregister(PyObject *search_function) | ||
| { | ||
| PyInterpreterState *interp = PyInterpreterState_Get(); | ||
| PyObject *codec_search_path = interp->codec_search_path; | ||
| /* Do nothing if codec_search_path is not created yet or was cleared. */ | ||
shihai1991 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| if (codec_search_path == NULL){ | ||
| return 0; | ||
| } | ||
| assert(PyList_CheckExact(codec_search_path)); | ||
| Py_ssize_t n = PyList_GET_SIZE(codec_search_path); | ||
| for (Py_ssize_t i = 0; i < n; i++){ | ||
| PyObject *item = PyList_GET_ITEM(codec_search_path, i); | ||
| if (item == search_function){ | ||
shihai1991 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| if (interp->codec_search_cache != NULL){ | ||
| assert(PyDict_CheckExact(interp->codec_search_cache)); | ||
| PyDict_Clear(interp->codec_search_cache); | ||
| } | ||
| return PyList_SetSlice(codec_search_path, i, i+1, NULL); | ||
| } | ||
| } | ||
shihai1991 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| return 0; | ||
| } | ||
| 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 | ||
Uh oh!
There was an error while loading. Please reload this page.