2017-10-15 30 views
0

我想在C中使用PyPy和cffi嵌入Python函数。我正在关注PyPy文档中的this guide如何嵌入一个使用cffi在C中返回字符串的Python函数?

问题是,我找到的所有例子都是在int上进行操作的,我的函数接受一个字符串并返回一个字符串。我似乎无法弄清楚如何在C中嵌入这个函数,因为C似乎并没有真正的字符串,而是使用了字符数组。

这是我已经试过:

# interface.py 

import cffi 

ffi = cffi.FFI() 
ffi.cdef(''' 
struct API { 
    char (*generate_cool_page)(char url[]); 
}; 
''') 

... 


@ffi.callback("char[] (char[])") 
def generate_cool_page(url): 
    # do some processing with BS4 
    return str(soup) 

def fill_api(ptr): 
    global api 
    api = ffi.cast("struct API*", ptr) 
    api.generate_cool_page = generate_cool_page 

-

// c_tests.c 

#include "PyPy.h" 
#include <stdio.h> 
#include <stdlib.h> 

struct API { 
    char (*generate_cool_page)(char url[]); 
}; 

struct API api; /* global var */ 

int initialize_api(void) 
{ 
    static char source[] = 
     "import sys; sys.path.insert(0, '.'); " 
     "import interface; interface.fill_api(c_argument)"; 
    int res; 

    rpython_startup_code(); 
    res = pypy_setup_home(NULL, 1); 
    if (res) { 
     fprintf(stderr, "Error setting pypy home!\n"); 
     return -1; 
    } 
    res = pypy_execute_source_ptr(source, &api); 
    if (res) { 
     fprintf(stderr, "Error calling pypy_execute_source_ptr!\n"); 
     return -1; 
    } 
    return 0; 
} 

int main(void) 
{ 
    if (initialize_api() < 0) 
     return 1; 

    printf(api.generate_cool_page("https://example.com")); 

    return 0; 
} 

当我运行gcc -I/opt/pypy3/include -Wno-write-strings c_tests.c -L/opt/pypy3/bin -lpypy3-c -g -o c_tests然后运行./c_tests,我得到这个错误:

debug: OperationError: 
debug: operror-type: CDefError 
debug: operror-value: cannot render the type <char()(char *)>: it is a function type, not a pointer-to-function type 
Error calling pypy_execute_source_ptr! 

我不在C语言方面没有太多的经验,我觉得我误表了字符串参数/返回值UE。我如何正确地做到这一点?

感谢您的帮助!

回答

2

请注意,您不应该使用pypy的弃用接口来嵌入;相反,请参阅http://cffi.readthedocs.io/en/latest/embedding.html

C语言没有“字符串”,但只有字符数组。在C中,想要返回“字符串”的函数通常以不同的方式写入 :它接受指向预先存在的缓冲区(类型为char[])的指针作为第一个参数,并将该缓冲区的长度作为第二个参数;并在被调用时填充缓冲区。这可能很麻烦,因为你理想上需要处理调用者中缓冲区太小的情况,例如,分配一个更大的数组并再次调用该函数。

或者,某些功能放弃并返回新的malloc() -ed char *。那么来电者必须记得free()它,否则会发生泄漏。在这种情况下,我会推荐这种方法,因为在调用之前猜测字符串的最大长度可能会很困难。

所以,类似的东西。假设你开始 http://cffi.readthedocs.io/en/latest/embedding.html,改变 plugin.h含有::

// return type is "char *" 
extern char *generate_cool_page(char url[]); 

,并更改该位的plugin_build.py ::

ffibuilder.embedding_init_code(""" 
    from my_plugin import ffi, lib 

    @ffi.def_extern() 
    def generate_cool_page(url): 
     url = ffi.string(url) 
     # do some processing 
     return lib.strdup(str(soup)) # calls malloc() 
""") 
ffibuilder.cdef(""" 
    #include <string.h> 
    char *strdup(const char *); 
""") 

从C代码,你不需要在initialize_api()在所有 新的嵌入模式;相反,你刚才说#include "plugin.h" 并直接调用该函数::

char *data = generate_cool_page("https://example.com"); 
if (data == NULL) { handle_errors... } 
printf("Got this: '%s'\n", data); 
free(data); // important! 
+0

感谢您指出我正确的文档 - 我会尝试了这一点! – TheInitializer

+0

你确定我不需要'initialize_api()'吗? GCC给我'未定义的引用'generate_cool_page'。 (我已经包含'plugin.h') – TheInitializer

+0

仔细阅读如何编译,在我链接的页面上。对于两种不同的用例有两种选择。看起来你并没有遵循这些选项...... –

相关问题