Visitar URL original
bpo-41842: Add a unregister function in _codecs module by shihai1991 · Pull Request #22360 · python/cpython · GitHub
Skip to content
Merged
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 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. */
Comment thread
shihai1991 marked this conversation as resolved.
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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);
Comment thread
shihai1991 marked this conversation as resolved.
Outdated
if (item == search_function) {
Comment thread
shihai1991 marked this conversation as resolved.
PyDict_Clear(interp->codec_search_cache);
Comment thread
shihai1991 marked this conversation as resolved.
Outdated
return PyList_SetSlice(interp->codec_search_path, i, i+1, NULL);
return PyList_SetSlice(codec_search_path, i, i+1, NULL);
}
}
Comment thread
shihai1991 marked this conversation as resolved.
return 0;
Expand Down