PyPy와 cffi를 사용하여 C에서 Python 함수를 임베드하려고합니다. 나는 PyPy 문서에서 this guide을 팔로우하고 있습니다.cffi를 사용하여 C에 문자열을 반환하는 Python 함수를 포함하려면 어떻게해야합니까?
문제는 내가 찾은 모든 예제가 int에서 작동하고, 함수가 문자열을 받아 문자열을 반환한다는 것입니다. C에서이 함수를 포함하는 방법을 알아낼 수 없습니다. C가 실제로 문자열을 갖고있는 것처럼 보이지 않으므로 char 배열을 사용하는 것이 좋습니다. 여기
내가 무엇을 시도했다입니다 : - :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!
I 돈
# 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
을 실행하면이 오류를 얻을 수 C에 대한 많은 경험이 있지만 문자열 인수/리턴 값을 잘못 전달하고있는 것처럼 느껴집니다. ue. 이 작업을 올바르게 수행하려면 어떻게해야합니까?
도움 주셔서 감사합니다.
감사합니다 -이 밖으로 시도 할 것이다! – TheInitializer
나는'initialize_api()'가 필요 없다고 확신합니까? GCC는'generate_cool_page '에 대한 정의되지 않은 참조를 제공합니다. (나는'plugin.h'를 포함 시켰습니다.) – TheInitializer
내가 링크 된 페이지에서 컴파일하는 법을 아주주의 깊게 읽으십시오. 두 가지 유스 케이스에 대해 두 가지 옵션이 있습니다. 이 옵션을 따르지 않는 것 같습니다 ... –