Using example among PyObject_CallObject, PyObject_Call, PyObject_CallFunction

python Object Protocol

From: https://docs.python.org/2/c-api/object.html

Using example among PyObject_CallObject, PyObject_Call, PyObject_CallFunction_第1张图片

Py_BuildValue() Function

From :http://www.fnal.gov/docs/products/python/v1_5_2/ext/buildValue.html 

Using example among PyObject_CallObject, PyObject_Call, PyObject_CallFunction_第2张图片

Example

static PyObject *g_callbackFunc = NULL;
/*
* @Desp : Get the val from JSON data string by key
* @in : char *buffer, char *key
* @out : char* value
* @ret : NULL
* @example for buffer value:
*    {"URI": "ir_command", "VALUE":{"CODESET":0, "CODE":86, "ACTION":"KEYPRESS"}}
*/
static void GetIntVal(char* buffer, char* key, char* value) {
    int ret = 0;
    int i = 0;
    char* tmp = NULL;

    tmp = strstr(buffer, key);
    if (tmp != NULL) {
        // Setting pointer to starting of exact value
        ret = strlen(key);
        while (tmp[ret] == ':' || tmp[ret] == '"' || tmp[ret] == ' ') 
            ret++;
        while (tmp[ret] != '"' && tmp[ret] != ',' && tmp[ret] != '}')
            value[i++] = tmp[ret++];

        value[i] = '\0';
    }
}

//For GetVal()
static char uri[256];
static char Val_1[5];
static char Val_2[5];
static char Val_3[32];

PyObject_CallObject



void restNotifyFunc(int event, void* param, long param_len)
{
  DEBUG_PRINTF("restNotifyFunc, event = %d, param = %p, param_len = %ld\n", event, param, param_len);
  if (g_callbackFunc != NULL) {
    int arg;
    PyObject *arglist;
    PyObject *result;
    arg = event;
    PyGILState_STATE gstate;
    gstate = PyGILState_Ensure();
    do {
      /* Time to call the callback */
      arglist = Py_BuildValue("(i)", arg);
      if (arglist == NULL) {
        DEBUG_PRINTF("restNotifyFunc, Py_BuildValue run OOM.\n");
        break;
      }
      DEBUG_PRINTF("restNotifyFunc, g_callbackFunc = %p.\n", g_callbackFunc);
      result = PyObject_CallObject(g_callbackFunc, arglist);
      Py_DECREF(arglist);
      if (result != NULL)
        Py_DECREF(result);
    } while (0);
    PyGILState_Release(gstate);
  }
}

PyObject_Call

void restNotifyFunc(int event, void* param, long param_len)
{
    DEBUG_PRINTF("restNotifyFunc, event = %d, param = %s, param_len = %ld\n", event, (char*)param, param_len);
    if (g_callbackFunc != NULL) {

    char *arg; 
    arg = param;

    int reason = event;

    PyObject *argList;
    PyObject *argDict;
    PyObject *result;

    memset(uri, 0, sizeof(uri));
    memset(Val_1, 0, sizeof(Val_1));
    memset(Val_2, 0, sizeof(Val_2));
    memset(Val_3, 0, sizeof(Val_3));

    PyGILState_STATE gstate;
    gstate = PyGILState_Ensure();
    switch (reason){
        case 1:   
        case 2:   
         GetIntVal(arg, "URI", uri);
         GetIntVal(arg, "VALUE", Val_1);
         DEBUG_PRINTF(" rest, uri:%s\n", uri);
         DEBUG_PRINTF(" rest, Val_1:%d\n", atoi(Val_1));

         do {
           /* Time to call the callback */
           argList = Py_BuildValue("(i)", reason);
           argDict = Py_BuildValue("{s:s, s:i}", "URI", uri, "VALUE", atoi(Val_1));
           if (argList == NULL || argDict == NULL ) {
             DEBUG_PRINTF("restNotifyFunc, Py_BuildValue run OOM.\n");
             break;
           }

           DEBUG_PRINTF("restNotifyFunc, g_callbackFunc = %p.\n", g_callbackFunc);
           result = PyObject_Call(g_callbackFunc, argList, argDict);
           Py_DECREF(argList);
           Py_DECREF(argDict);
           if (result != NULL)
             Py_DECREF(result);
           else
             DEBUG_PRINTF("PyObject_Call does not work well ! \n");
         } while (0);
        break;

        case 3:   
         GetIntVal(arg, "URI", uri);
         GetIntVal(arg, "CODESET", Val_1);
         GetIntVal(arg, "\"CODE\"", Val_2);
         GetIntVal(arg, "ACTION", Val_3);
         DEBUG_PRINTF(" rest, uri:%s\n", uri);
         DEBUG_PRINTF(" rest, Val_1:%d\n", atoi(Val_1));
         DEBUG_PRINTF(" rest, Val_2:%d\n", atoi(Val_2));
         DEBUG_PRINTF(" rest, Val_3:%s\n", Val_3);

         do {
           /* Time to call the callback */
           argList = Py_BuildValue("(i)", reason);
           argDict = Py_BuildValue("{s:s, s:{s:[{s:i, s:i, s:s}]}}", "URI", uri,
                                                                 "VALUE",
                                                                 "KEYLIST",
                                                                 "CODESET", atoi(Val_1),
                                                                 "CODE", atoi(Val_1),
                                                                 "ACTION", Val_3);
           if (argList == NULL || argDict == NULL ) {
             DEBUG_PRINTF("restNotifyFunc, Py_BuildValue run OOM.\n");
             break;
           }

           DEBUG_PRINTF("restNotifyFunc, g_callbackFunc = %p.\n", g_callbackFunc);
           result = PyObject_Call(g_callbackFunc, argList, argDict);
           Py_DECREF(argList);
           Py_DECREF(argDict);
           if (result != NULL)
             Py_DECREF(result);
           else
             DEBUG_PRINTF("PyObject_Call does not work well ! \n");
         } while (0);

      }

      PyGILState_Release(gstate);
    }
}

PyObject_CallFunction

void restNotifyFunc(int event, void* param, long param_len) {
    DEBUG_PRINTF("restNotifyFunc, event = %d, param = %s, param_len = %ld\n", event, (char*)param, param_len);
    if (g_callback != NULL) {
        char* arg;
        arg = param;

        int reason = event;

        PyObject* result;

        memset(uri, 0, sizeof(uri));
        memset(Val_1, 0, sizeof(Val_1));
        memset(Val_2, 0, sizeof(Val_2));
        memset(Val_3, 0, sizeof(Val_3));

        PyGILState_STATE gstate;
        gstate = PyGILState_Ensure();
        switch (reason) {
            case 1:
            case 2:
                GetIntVal(arg, "URI", uri);
                GetIntVal(arg, "VALUE", Val_1);
                DEBUG_PRINTF("dsl, uri:%s\n", uri);
                DEBUG_PRINTF("dsl, Val_1:%d\n", atoi(Val_1));

                do {
                    /* Time to call the callback */
                    DEBUG_PRINTF("restNotifyFunc, g_callback = %p.\n", g_callback);
                    result = PyObject_CallFunction(g_callback, "{s:s, s:i}", "URI", uri, "VALUE", atoi(Val_1));
                    if (result != NULL)
                        Py_DECREF(result);
                    else
                        DEBUG_PRINTF("PyObject_CallFunction does not work well ! \n");
                } while (0);
                break;

            case 3:
                GetIntVal(arg, "URI", uri);
                GetIntVal(arg, "CODESET", Val_1);
                GetIntVal(arg, "\"CODE\"", Val_2);
                GetIntVal(arg, "ACTION", Val_3);

                DEBUG_PRINTF("dsl, uri:%s\n", uri);
                DEBUG_PRINTF("dsl, Val_1:%d\n", atoi(Val_1));
                DEBUG_PRINTF("dsl, Val_2:%d\n", atoi(Val_2));
                DEBUG_PRINTF("dsl, Val_3:%s\n", Val_3);

                do {
                    /* Time to call the callback */
                    DEBUG_PRINTF("restNotifyFunc, g_callback = %p.\n", g_callback);
                    result = PyObject_CallFunction(g_callback, "{s:s, s:{s:[{s:i, s:i, s:s}]}}", "URI", uri, "VALUE", "KEYLIST", "CODESET",
                                                   atoi(Val_1), "CODE", atoi(Val_2), "ACTION", Val_3);
                    if (result != NULL)
                        Py_DECREF(result);
                    else
                        DEBUG_PRINTF("PyObject_CallFunction does not work well ! \n");
                } while (0);
        }

        PyGILState_Release(gstate);
    }
}

你可能感兴趣的:(Python)