10000 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
Prev Previous commit
Next Next commit
apply Dong-hee Na's comment
  • Loading branch information
shihai1991 committed Sep 26, 2020
commit f18cd4f9f2dc12476069975c4b9ab955cd5c13f9
9 changes: 5 additions & 4 deletions Python/codecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,18 @@ 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. */
if (interp->codec_search_path == NULL) {
if (codec_search_path == NULL) {
return 0;
}

Py_ssize_t n = PyList_Size(interp->codec_search_path);
Py_ssize_t n = PyList_Size(codec_search_path);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the macro PyList_GET_SIZE() instead of PyList_Size(), as codec_search_path is guaranteed to be a list.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it's a list right now, its type can change tomorrow.

@shihai1991: If you modify the code to use fast macros, please add the following assertion before using it:

assert(PyList_CheckExact(interp->codec_search_path));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vstinner Copy that, I have already updated it.
But I have a question. assert(PyList_Check(op) in _PyList_CAST() is not good enough?

for (Py_ssize_t i = 0; i < n; i++) {
PyObject *item = PyList_GetItem(interp->codec_search_path, i);
PyObject *item = PyList_GetItem(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);
return PyList_SetSlice(codec_search_path, i, i+1, NULL);
}
}
return 0;
Expand Down
0