John Ehresman <[EMAIL PROTECTED]> writes:

> The following causes a seg fault because it attempts to reference a CTree 
> node which does not exist:
> 
> >>> import gtk
> >>> tree = gtk.GtkCTree()
> >>> n = tree.node_nth(10)
> >>> print n.parent
> 
> Looking at the source, it appears that the C api function returns NULL in
> this case, but this pointer is passed to PyGtkCTreeNode_New.  The pointer
> is then stored and dereferenced in the get attr function, causing the seg
> fault.  There are a couple ways to fix this that are relatively easy to
> implement:
>   * Return None if the pointer is NULL.
>   * Throw an exception if NULL is returned.
> 
> My question is which is the preferred method in this case and whether all
> the cases where a node is returned should be handled the same way -- are
> there cases where NULL would be an acceptable value rather than a result
> of passing incorrect values to function.  Also, should other boxed types
> act the same way.

The code for these functions is generated by a python program from
some sort of specification. I have appended a patch that checks for
NULL values when returning a (GtkObject *) and in that case returns
None.

A problem with throwing exceptions is that some generated functions
can return NULL as a valid value, and in other cases its clearly a
program error. One way to solve this would be to provide info to the
generator about each function that should raise an exception. Maybe
its a better solution to not change the generator but change some
wrappers in gtk.py to raise an excpetion on a None return.

todo: look for other types that should be handled in the same way

Index: generate.py
===================================================================
RCS file: /cvs/gnome/gnome-python/pygtk/generate/generate.py,v
retrieving revision 1.2
diff -u -r1.2 generate.py
--- generate.py 1999/02/19 03:48:37     1.2
+++ generate.py 1999/12/15 07:41:06
@@ -421,9 +421,12 @@
                        impl.write(funcCall)
                        impl.write(');\n')
                elif retType in objects.keys():
-                       impl.write('    return PyGtk_New((GtkObject *)')
-                       impl.write(funcCall)
-                       impl.write(');\n')
+                       impl.write('    {\nGtkObject *p = (GtkObject *)%s;\n'
+                                  % funcCall)
+                       impl.write('    if (p)\n')
+                       impl.write('        return PyGtk_New(p);\n}\n')
+                       impl.write('    Py_INCREF(Py_None);\n')
+                       impl.write('    return Py_None;\n')
                else:
                        print "%s: unknown return type '%s'" % (name, retType)
                        return 1
To unsubscribe: echo "unsubscribe" | mail [EMAIL PROTECTED]

Reply via email to