[Pyrex] Re: callback

Bryan Weingarten bryan.weingarten at pobox.com
Mon Aug 18 16:06:34 CEST 2003


> On Mon, 18 Aug 2003, Pedro Rodriguez wrote:
>
> > On Mon, 18 Aug 2003 03:04:40 +0000, Bryan Weingarten wrote:
> [...]
> > > my problem is that the i need user data to be passed into
status_callback
> > > which gets passed back to the python callback.  the "cheese" way of
doing
> [...]
> > Instead of just passing the actual python callback, you can pass a
python
> > object that will hold all the data you need.
> [...]
> > But there may be still an issue here. I don't know if the 'data' tuple
> > provided to the C code (set_status_callback) may be garbage collected.
> > In that case, it may be required to prevent this by memorizing the last
> > passed data at global level.
> [...]
>
> Or Py_INCREF instead, I suppose?
>
> cdef extern from "Python.h":
>     void Py_INCREF(object)
>     void Py_DECREF(object)
>     void Py_XINCREF(object)
>     void Py_XDECREF(object)
>
>
> John
>
>

john, pedro,

thanks for your quick responses... i'm still having trouble with both of
your suggestions.


class Play
    def status_callback(self, callback, user_data):
        data = (callback, user_data)
        set_status_callback(self.ctx, _callback_wrapper, <void*>data)

cdef int _callback_wrapper(void *data, long sec, long msec):
    callback, user_data = <object>data # this is line 56
    return callback(data, sec, msec)

when i compile this i get the following error
C:\src>mpgedit.pyx:56:20: Cannot convert 'void (*)' to Python object

when i use the global variable technique as pedro suggested i get an
additional error
C:\src>mpgedit.pyx:49:8: '%s' redeclared
C:\src>mpgedit.pyx:55:20: Cannot convert 'void (*)' to Python object

line 49 is the "global data" line


john,

i would prefer to use your Py_INCREF and Py_DECREF since i'm used to using
that when i write extesions manually.  but i don't see how use it in my
case.  in my example, i don't see where to Py_INCREF or Py_DECREF.


bryan





More information about the Pyrex mailing list