Python Function pointer in C

D

David Gilbert

I'm trying to maintain the interface to KQueue for FreeBSD. At one
point, the C module stores an opaque object in a C structure such that
the opaque pointer can be used later by the application.

In Python, I want to say something like:

kev = [KEvent(1000, filter=EVFILT_TIMER, flags=EV_ADD|EV_ONESHOT,
udata=lambda x: getJob(x, con))]
kq.event(kev, 0, 0)

and later, this is used by

kev = kq.event(None, 1, 0)
print "Kevent Triggered! %s" % str(kev)

# Call event's udata
kev.udata(kev)

.... so this doesn't work. Specifically:

[3:4:304]dgilbert@canoe:~/devel/SoftGrabber> python
Python 2.3.4 (#2, Nov 2 2004, 16:03:35)
[GCC 3.4.2 [FreeBSD] 20040728] on freebsd5
Type "help", "copyright", "credits" or "license" for more information.Segmentation fault (core dumped)

Now the code in the C module (kqsyscall above) defines the kevent()
return as the following object:

static struct memberlist KQEvent_memberlist[] = {
{"ident", T_UINT, OFF(e.ident)},
{"filter", T_SHORT, OFF(e.filter)},
{"flags", T_USHORT, OFF(e.flags)},
{"fflags", T_UINT, OFF(e.fflags)},
{"data", T_INT, OFF(e.data)},
{"udata", T_OBJECT, OFF(e.udata)},
{NULL} /* Sentinel */
};

And it also defines various support functions.

How do I define "udata" such that I can stuff the lambda reference in
there?

Now... I've wondered if this is a reference counting issue --- but
this still happens when I use the name of a module global function as
udata.

Dave.

--
============================================================================
|David Gilbert, Independent Contractor. | Two things can only be |
|Mail: (e-mail address removed) | equal if and only if they |
|http://daveg.ca | are precisely opposite. |
=========================================================GLO================
 
N

Nick Coghlan

David said:
I'm trying to maintain the interface to KQueue for FreeBSD. At one
point, the C module stores an opaque object in a C structure such that
the opaque pointer can be used later by the application.

In this case, a Py_IncRef/DecRef pair is definitely required - the 'borrowed'
reference from the Python function call only lasts until the function returns.
You need your own reference if you're going to hang on to the pointer.
How do I define "udata" such that I can stuff the lambda reference in
there?

PyObject* should be fine so long as the references are handled correctly.
Now... I've wondered if this is a reference counting issue --- but
this still happens when I use the name of a module global function as
udata.

I'd suggest adding the IncRef/DecRef pair and trying it out again. I can't see
anything else obviously wrong in the fragments you've posted.

If that was the sole problem though, I would have expected the same as you - for
the module global function to work correctly.

Sorry I can't be more help. . .

Cheers,
Nick.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top