Skip to content
Merged
Prev Previous commit
Next Next commit
Improve readability in the error handling branch
  • Loading branch information
pablogsal committed Oct 26, 2018
commit e7f8c464258acf2e82278909fb5e1a792aa65dbf
6 changes: 2 additions & 4 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2679,12 +2679,10 @@ list___init___impl(PyListObject *self, PyObject *iterable)
if (_PyObject_HasLen(iterable)) {
Py_ssize_t iter_len = PyObject_Size(iterable);
if (iter_len == -1) {
if (PyErr_ExceptionMatches(PyExc_Exception)) {
PyErr_Clear();
}
else {
if (!PyErr_ExceptionMatches(PyExc_Exception)) {
return -1;
}
PyErr_Clear();
Comment thread
vstinner marked this conversation as resolved.
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.

@pablogsal, @serhiy-storchaka: In a previous comment, I proposed to add an helper function to "probe" an object size: so move this code into a private helper function. Since the same code is used by PyObject_LengthHint(), it would now make sense, no?

See also iter_len() ... which is different.

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.

This code will be used just in two places and is not too complex. Adding yet one intermediate function will add a performance penalty for calling a function and checking its result, and will complicate the code. If this code will be used in more places, it can be refactored.

}
if (iter_len > 0 && self->ob_item == NULL
&& list_preallocate_exact(self, iter_len)) {
Expand Down