包装用于python的C代码

时间:2015-11-19 10:47:45

标签: python c

我已经多次看过使用C / C ++扩展Python的例子但是对于我的生活来说无法弄清楚如何处理已经存在的代码。如果假设在呼叫名称之后是PyObject *self, PyObject *args,那么那些已经存在的所有其他部分呢?

我的意思的一小部分。

void onNewChannelEvent(uint64 serverConnectionHandlerID, uint64 channelID, uint64 channelParentID) {
    /* Query channel name from channel ID */
    char* name;
    unsigned int error;

    printf("onNewChannelEvent: %llu %llu %llu\n", (unsigned long long)serverConnectionHandlerID, (unsigned long long)channelID, (unsigned long long)channelParentID);
    if((error = ts3client_getChannelVariableAsString(serverConnectionHandlerID, channelID, CHANNEL_NAME, &name)) == ERROR_ok) {
        printf("New channel: %llu %s \n", (unsigned long long)channelID, name);
        ts3client_freeMemory(name);  /* Release dynamically allocated memory only if function succeeded */
    } else {
        char* errormsg;
        if(ts3client_getErrorMessage(error, &errormsg) == ERROR_ok) {
            printf("Error getting channel name in onNewChannelEvent: %s\n", errormsg);
            ts3client_freeMemory(errormsg);
        }
    }
}

完整代码在https://bpaste.net/show/f6fd244728de

看一下这些例子,我看到PyObject *self, PyObject *args在命名调用时参数的使用,但是如果已经有了什么,你会做什么?

看着它时我觉得它会像这样。

static PyObject *
onNewChannelEvent(PyObject *self, PyObject *args, uint64 serverConnectionHandlerID, uint64 channelID, uint64 channelParentID) {
    /* Query channel name from channel ID */
    char* name;
    unsigned int error;

    if (!PyArg_ParseTuple(args, "s", &name))
        return NULL;

    printf("onNewChannelEvent: %llu %llu %llu\n", (unsigned long long)serverConnectionHandlerID, (unsigned long long)channelID, (unsigned long long)channelParentID);
    if((error = ts3client_getChannelVariableAsString(serverConnectionHandlerID, channelID, CHANNEL_NAME, &name)) == ERROR_ok) {
        printf("New channel: %llu %s \n", (unsigned long long)channelID, name);
        ts3client_freeMemory(name);  /* Release dynamically allocated memory only if function succeeded */
    } else {
        char* errormsg;
        if(ts3client_getErrorMessage(error, &errormsg) == ERROR_ok) {
            printf("Error getting channel name in onNewChannelEvent: %s\n", errormsg);
            ts3client_freeMemory(errormsg);
        }
    }
}

我确信我只是以一种非常奇怪的方式执行此操作,因为有一种更有效的方法可以尝试使用此处的代码创建模块。我该怎么做才能获得提供给我的代码模块?

0 个答案:

没有答案