Using example among PyObject_CallObject, PyObject_Call, PyObject_CallFunction

python Object Protocol

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

Doc

Py_BuildValue() Function

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

这里写图片描述

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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值