[pygtk] virtual function with the same name

Sébastien Granjoux seb.sfo at free.fr
Thu Dec 13 06:02:37 WST 2007


Hi All,

I'm trying to write in python a object implementing several GInterfaces. 
My problem is that several functions in these interfaces have the same 
name. By example I have something like:

In IDebuggerFrame
	void list(void)		// List all frames
In IDebuggerBreakpoint
	void list(void)		// List all breakpoints

In C, these functions are called with
	void idebugger_frame_list(IDebuggerFrame *obj)
	{
		IDEBUGGER_FRAME(obj)->list();
	}
	void idebugger_breakpoint_list(IDebuggerBreakpoint *obj)
	{
		IDEBUGGER_BREAKPOINT(obj)->list();
	}
There is no problem here, an object implementing both interfaces will 
have 2 different structure with a list member in each one.

In python, the wrapper of both interfaces will look for the same do_list 
method.

I can override both do_list methods to use a different function in both 
case by example do_list_frame and do_list_breakpoint here. But then I 
have a problem in the function initializing the interface 
__IDebuggerBreakpoint__interface_init.

There is a code like the following:
     py_method = pytype? PyObject_GetAttrString((PyObject *) pytype, 
"do_list") : NULL;
     if (py_method && !PyObject_TypeCheck(py_method, &PyCFunction_Type)) {
         iface->list = wrap_IDebuggerBreakpoint__proxy_do_list;
     } else {
         PyErr_Clear();
         if (parent_iface) {
             iface->list = parent_iface->list;
         }
     Py_XDECREF(py_method);

This code is generated in codegen.py in write_virtuals function. The 
"do_list" string is generated by adding "do_" to the function name. And 
this function name is used to name the iface field.

What is the best way to handle this ?

I can think about a few solutions.

1. Add in the .defs file another name, let's call it python name, like 
we already have c name and use it instead of do_* name. If this name is 
not defined generates a name adding "do_" before the function name like 
now. I can write a patch for this.

2. Allow to override the __IDebuggerBreakpoint__interface_init function, 
so I can provide another implementation of this function, using more 
complex name. I have tried a bit but didn't succeed perhaps it's already 
possible. I need to override all functions using a special name too. 
Perhaps I can simply not use codegen for these interfaces and write 
everything by hand.

3. Rename my GInterface function in the C code. It's the easiest 
solution, but the function name is C will be very long and I would 
prefer to handle problem added by the python wrapper in python code.

Regards,

Sébastien


More information about the pygtk mailing list