unhappy_mage
[H]ard|DCer of the Month - October 2005
- Joined
- Jun 29, 2004
- Messages
- 11,455
I'm writing a Python/Swig wrapper for some C code, and one of the things it does it take a callback function and use it to iterate over items. I want to properly wrap this: pass a Python callback function, and have some C code that translates the items that are iterated over into Python objects and then calls the Python function on these objects. I'm so close to being where I need to be, but I need one more layer of abstraction. I've already wrapped the struct itself, and I can create a new instance:
All fine and good. This object has all the methods I've exposed. However, when I try the callback function thing, I have code like this:
My callback function just prints its arguments for now:
and when I run it I get output like this:
and the methods I've defined are missing.
Looking at the output of swig, it looks like I'm constructing the C object properly, but not wrapping it in the Python needed to make everything work right. So the question is, how can I make the C code properly instantiate a Python object? What needs to go in my .i file to make this work?
Code:
>>> myobj = mymodule.new(args)
>>> print myobj
<mymodule; proxy of <Swig Object of type 'my_c_class *' at 0x80a3938> >
Code:
my_c_class the_child = my_c_class(m_parent, m_handle, child);
resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(&the_child), SWIGTYPE_p_mymodule, SWIG_POINTER_NEW | 0 );
PyEval_CallObject(func, Py_BuildValue("(OO)", resultobj, realdata));
Code:
def fn(x, data):
print " Python arg is %s, and data is %s" % (x, data)
print repr(x)
Code:
The Python says _18640408_p_mymodule, and data is None
<Swig Object of type 'my_c_class *' at 0x8046418>
Looking at the output of swig, it looks like I'm constructing the C object properly, but not wrapping it in the Python needed to make everything work right. So the question is, how can I make the C code properly instantiate a Python object? What needs to go in my .i file to make this work?