Python -Why cannot change the value in the C callback function?
Question: I try to using python ctype to call C library (.so) , and this C library have
callback function. C source code:

* * *
    
    
    int showHelloword(int *result)
    {
        *result = 1025;
        return 55;
    }
    
    void BSP_SHOW(int time,int (*callback)(int *result))
    {
        int ret = 44;
        int boo = 4;
        while(time > 0){
            --time;
            if(time == 0){
                if(callback) {
                    printf("boo %d\n",boo);      // I expect boo=4
                    ret = callback(&boo);
                    printf("Outfunc=%d\n",boo);  // I expect Outfunc=1025
                    printf("ret=%d\n",ret);      // I expect ret=55
                }        
                callback = 0;
            }
            sleep(1);
        }
    }
    
    void main(){
        BSP_SHOW( 2, &showHelloword);
    }
    

The print rseult is :

    
    
    boo=4
    Outfunc=1025
    ret=55
    

And it seem okay.

Makefile source :

    
    
    gcc -c -fPIC -Wno-format-security -g main.c
    gcc -shared -o libcall.so main.o
    gcc main.o -o callback -ldl -lpthread -lrt 
    

* * *

THEN, I try to using python ctype to call "BSP_SHOW" this function and get
callback into this function. Python source :

Python version : 2.6.6

* * *
    
    
    import sys
    import os
    import time
    from ctypes import *
    import struct
    from Queue import *
    
    class callback_api(object):
    
        def __init__(self):
            CDLL("/usr/lib64/librt.so",mode=RTLD_GLOBAL)
            path = "/root/workspace/callback/libcall.so"  `C library path`
            self.dll = cdll.LoadLibrary(path)
    
        def BSP_SHOW(self,sec, cb_func):
            self.dll.BSP_SHOW.restype = None
            self.dll.BSP_SHOW.argtypes = [c_int, qa_cb_prototype]
            self.dll.BSP_SHOW(c_int(sec),cb_func)
    
    
        def qa_callback(result):
            result = 1025 #it should change the value. but the result is not....
            return 88
    
    qa_cb_prototype = CFUNCTYPE( c_int, POINTER(c_int))
    qa_cache_cb_func = qa_cb_prototype(qa_callback)
    
    if __name__ == '__main__':
        api = callback_api()
        api.BSP_SHOW(2,qa_cache_cb_func) `got it callback`
    

* * *

The print result is.

    
    
    cb=4
    Outfunc=4 <---I expect it should be 1025,but not! 
    ret=88 <---return value have been change by python callback.
    

Have someone know is this a python ctype limitation or I use a wrong method?
Thanks!


Answer: The = operator reassigns a variable in python. In other words, you change what
the variable means.

You need to change what the variable is pointing to. Since it's a ctypes
pointer, you need to index it with 0 to do this.

    
    
        def qa_callback(result):
            result[0] = 1025
            return 88
    

Note the `[0]` after result.

See also: <https://docs.python.org/2/library/ctypes.html#pointers>

