views:

79

answers:

2

I have wrapped some C code using SWIG to use it as a python library.

Within this framework, some python code I have written calls a C function, which returns a string. However, for creating the string, the C function requires a ranking, the generation of which I have implemented in Python. How would I go about implementing this using callbacks?

I see this as the following multistep process.

1) Python instantiates a C object: import test_Objects #test_Objects is the C file that has been wrapped C = test_objects.my_class()

2) Call the relevant method on the my_class object, which return a string: txt_1 = "string1" txt_2 - "string2" result = C.sorted_string(txt_1, txt_2)

2.1) I want sorted_string to call the following python function, which returns a sorted list.

   def sorted_string([my_list]):
       .....
       .....
       return your_list

2.2) Sorted_string would make use of the list to generate the result.

How would I implement step 2.1?

A: 

The PyObject_Call*() functions can be used to call a Python function or method from C.

Ignacio Vazquez-Abrams
Thanks. Any ideas on how to implement this through swig?
Matt
Nope. But once you get a function object, it's all over.
Ignacio Vazquez-Abrams
A: 

It is not specific to your question, but here is an example that I asked: swig-passing-argument-to-python-callback-fuction

Hope this is helpful.

joon
joon: I saw your post earlier and thats what prompted me to ask my question. I see callback.c and callback.i. Can you give me a pointer to your .py file (the one you wrote, not the one that is generated by SWIG) that contains the function that callback.c invokes? Also, I noticed that you don't have a callback.h file? Doesn't SWIG require one?
Matt