GNOME Bugzilla – Bug 311852
TreeModelRow index out of range error not raising properly
Last modified: 2006-07-20 08:33:05 UTC
When accessing TreeModelRow values through Python's sequence interface, index out of range error not raising for wrong indices. Here error should be raised for a[0][-3] and a[0][-4] >>> import gtk >>> a = gtk.ListStore(int, int) >>> a.append((0, 1)) <GtkTreeIter at 0x824cd10> >>> a[0][0] 0 >>> a[0][1] 1 >>> a[0][2] Traceback (most recent call last):
+ Trace 62044
>>> a[0][3] Traceback (most recent call last):
>>> a[0][-1] 1 >>> a[0][-2] 0 >>> a[0][-3] 1 >>> a[0][-4] 0 >>> a[0][-5] Traceback (most recent call last):
>>> a[0][-6] Traceback (most recent call last):
>>>
column index are pre-adjusted by Python, so no need to check the -ve value condition: --- gtk-types.c.orig 2005-07-28 09:01:20.000000000 +0530 +++ gtk-types.c 2005-07-28 09:03:56.000000000 +0530 @@ -867,8 +867,6 @@ PyObject *ret; n_columns = gtk_tree_model_get_n_columns(self->model); - if (column < 0) - column += n_columns; if (column < 0 || column >= n_columns) { PyErr_SetString(PyExc_IndexError, "column index out of range"); return NULL;
Please test the patch before submitting it. If you look at the if below you realize that it's impossible for negative numbers to be passed in. Please submit patches as attachments.
Actually upon closer inspection it seems like it's not providing values in 0..len(model), as it should. We definitely need unittests for this, so we don't keep on breaking it.
After applying my patch, I am getting like this, and it looks OK : >>> a[0][-3] Traceback (most recent call last):
+ Trace 62055
>>> a[0][-4] Traceback (most recent call last):
Please have a look at Python's list object. http://cvs.sourceforge.net/viewcvs.py/python/python/dist/src/Objects/listobject.c?view=markup or the basic sequence wrapper methods at: Objects/typeobject.c (wrap_sq_item and getindex functions) Python's Modules/arraymodule.c may be also helpfull,
Python actually check the condition, and increment column, so these two lines are not not necessary: if (column < 0) column += n_columns; What about using doctest for some unit testing?
Created attachment 50438 [details] unit testing for get item
Fixed as suggested. * gtk/gtk-types.c (pygtk_tree_model_row_getitem) (pygtk_tree_model_row_setitem): Remove negative index readjustment code. #311852 (Baiju M) Checking in gtk/gtk-types.c; /cvs/gnome/pygtk/gtk/gtk-types.c,v <-- gtk-types.c new revision: 1.57; previous revision: 1.56 done