On Mon, May 13, 2013 at 5:25 PM, Gregor Thalhammer
<
gregor.t...@gmail.com> wrote:
> do this at
> cython/Demos/callback at master · cython/cython · GitHub
> Unfortunately, this described method requires that the C callback function
> gets another user defined argument that provides information about the
> Python function you want to call. Do you have influence on the signature of
> the C callback? Otherwise you have to store the Python callback function in
> some global variable, which is a bad solution.
I was working on this just a couple days back.
<rant>With all due respect to whoever wrote the example originally, I
found that it is not all that self-explanatory. Sometimes all these
semi-humorous examples are overdone and the technical aspect is lost
sight of. I mean, how am I supposed to immediately understand that the
void* user_data actually means the void* casted form of the Python
function object passed in? The functions and their arguments were not
at all given clear meaningful names.</rant>
I hence wrote up the attached examples, which I hope are more
meaningful to read and understand. The second one is a more condensed
form of the first, and the first is there only to indicate that if the
library you are wrapping doesn't already support (and most probably it
*won't* support) passing-the-ball with a void* py_callback, it is
trivial to write a C wrapper for that. (In the second, the same has
just been integrated into Cython so it's not really all that
different.)
Of course, in my example it would be possible to generate a list of
Fibonacci numbers in C and just pass that to Python to do whatever is
required without needing a feedback. But please remember this is
merely POC, and the same model is to be applied to the real situations
which *do* require a feedback.
The CTypes tutorial gives the libc qsort function as example. That
could also be implemented this way but I don't have the time to do
that.
--
Shriramana Sharma ஶ்ரீரமணஶர்மா श्रीरमणशर्मा