Need help to grok c extension that needs to use callbacks with data 
Author Message
 Need help to grok c extension that needs to use callbacks with data

Good day all,

  Ok, I'm new to python but not to programming in general.

  I have a task where I need to implement a C extension to interface to
a third-party DLL (yes, windoze).  I also need to do this "by hand" as
opposed to using something like SWIG.

  So far all my methods work like a charm.  Now I'm trying to implement
some functions that use a pointer-to-function callback params that have
data.

  I've gone over the "RECIPE" that uses qsort and am trying to
extrapolate that example into what I'm doing and am not being successful
at it.

  Python -> C Extension -> MethodWithCallback (void (*userFunc)(unsigned
char *))

  ...and I want the callback function to be a Python function.

  Any and all help is appreciated.

  Thank you.

- Jim



Tue, 06 Dec 2005 05:32:33 GMT  
 Need help to grok c extension that needs to use callbacks with data

Quote:

>   Python -> C Extension -> MethodWithCallback (void (*userFunc)(unsigned
> char *))

>   ...and I want the callback function to be a Python function.

You need to write a callback wrapper, and a wrapper object. Roughly,
you need something like that:

struct PyParam{
   PyObject *callable;
   PyObject *param;

Quote:
};

void CallbackWrapper(unsigned char* cdata)
{
   struct PyParam* data = (struct PyParam*)cdata;
   PyObject_CallFunction(data->callable, "O", data->param);
   /* look at exceptions. */

Quote:
}

PyObject* PyMethodWithCallback(PyObject* ignored, PyObject*args)
{
   struct PyParam params;
   PyArg_ParseTuple(args, "OO", &params.callable, &params.param);
   if (!PyCallable_Check(params.callable))
      raise TypeError, "first parameter must be callable"
   MethodWithCallback(CallbackWrapper, &params);

Quote:
}

This, of course, assumes that the unsigned char* does not need
to point to a stringz, but can point to arbitrary memory. It also
assumes that the callback will be called before PyMethodWithCallback
returns; otherwise, params must be allocated dynamically.

HTH,
Martin



Tue, 06 Dec 2005 16:51:20 GMT  
 Need help to grok c extension that needs to use callbacks with data

Quote:

> Good day all,

>   Ok, I'm new to Python but not to programming in general.

>   I have a task where I need to implement a C extension to interface to
> a third-party DLL (yes, windoze).  I also need to do this "by hand" as
> opposed to using something like SWIG.

>   So far all my methods work like a charm.  Now I'm trying to implement
> some functions that use a pointer-to-function callback params that have
> data.

>   I've gone over the "RECIPE" that uses qsort and am trying to
> extrapolate that example into what I'm doing and am not being successful
> at it.

>   Python -> C Extension -> MethodWithCallback (void (*userFunc)(unsigned
> char *))

>   ...and I want the callback function to be a Python function.

Have you looked into ctypes? It is able to do this, even in pure Python.

http://starship.python.net/crew/theller/ctypes/

Thomas



Tue, 06 Dec 2005 16:54:32 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Need help moving string field using data variable at runtime

2. Need help launching external non rexx command using VXREXX (VIO support needed) in OS/2

3. Help with Callback addresses needed

4. I need help with callbacks in Visual Eiffel

5. Need help invoking callback from C

6. C extension using Numeric arrays -- do I really need all this error

7. Need advice: serving data using http/web-server to clients ( not only browsers )

8. help needed with C extension

9. Need help installing sablot extension

10. Help needed with C++ extension

11. C extension help needed

12. Need help with first extension.

 

 
Powered by phpBB® Forum Software