After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 311852 - TreeModelRow index out of range error not raising properly
TreeModelRow index out of range error not raising properly
Status: RESOLVED FIXED
Product: pygtk
Classification: Bindings
Component: gtk
unspecified
Other Linux
: Normal normal
: ---
Assigned To: Nobody's working on this now (help wanted and appreciated)
Python bindings maintainers
Depends on:
Blocks:
 
 
Reported: 2005-07-28 11:47 UTC by Baiju M
Modified: 2006-07-20 08:33 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
unit testing for get item (788 bytes, text/plain)
2005-08-09 06:03 UTC, Baiju M
Details

Description Baiju M 2005-07-28 11:47:55 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):
  • File "<stdin>", line 1 in ?
IndexError: column index out of range
>>> a[0][3]
Traceback (most recent call last):
  • File "<stdin>", line 1 in ?
IndexError: column index out of range
>>> a[0][-1]
1
>>> a[0][-2]
0
>>> a[0][-3]
1
>>> a[0][-4]
0
>>> a[0][-5]
Traceback (most recent call last):
  • File "<stdin>", line 1 in ?
IndexError: column index out of range
>>> a[0][-6]
Traceback (most recent call last):
  • File "<stdin>", line 1 in ?
IndexError: column index out of range
>>>
Comment 1 Baiju M 2005-07-28 11:51:18 UTC
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;

Comment 2 Johan (not receiving bugmail) Dahlin 2005-07-28 13:00:08 UTC
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.
Comment 3 Johan (not receiving bugmail) Dahlin 2005-07-28 13:56:27 UTC
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.
Comment 4 Baiju M 2005-07-29 05:34:42 UTC
After applying my patch, I am getting like this, and it looks OK :

>>> a[0][-3]
Traceback (most recent call last):
  • File "<stdin>", line 1 in ?
IndexError: column index out of range
>>> a[0][-4]
Traceback (most recent call last):
  • File "<stdin>", line 1 in ?
IndexError: column index out of range

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,
Comment 5 Baiju M 2005-08-01 12:32:38 UTC
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?
Comment 6 Baiju M 2005-08-09 06:03:58 UTC
Created attachment 50438 [details]
unit testing for get item
Comment 7 John Finlay 2006-07-20 08:33:05 UTC
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