Hello, I recently started using cython in my projects, and faced the problem of casting. There is a function that accepts std :: string as input and returns std :: string, but if you change this line in python code, you get a segolt, I don’t understand how to deal with it, please explain what I am doing wrong?
Cython code:
cdef public string uploadfile(string fileid, string username): return <string>("test_str" + str(username)) C ++ code:
#include <stdio.h> #include <stdlib.h> #include <dlfcn.h> #include <string> #include <iostream> using namespace std; int main(int argc, char **argv) { void *handle; string (*func_0)(string); char *error; string parameter; string parameter_1; handle = dlopen("/home/ks/storage/modules/webui/webui.so", RTLD_LAZY); if (!handle) { fprintf(stderr, "%s\n", dlerror()); exit(EXIT_FAILURE); } dlerror(); string (*func_1)(string, string); *(void **) (&func_1) = dlsym(handle, "uploadfile"); if ((error = dlerror()) != NULL) { fprintf(stderr, "%s\n", error); exit(EXIT_FAILURE); } cout << "Enter parameter \" fileid\": "; cin >> parameter; cout << "Enter parameter \" username\": "; cin >> parameter_1; (*func_1)(parameter, parameter_1); cout << "called func_1" << endl; cout << (*func_1)(parameter, parameter_1) << endl; dlclose(handle); exit(EXIT_SUCCESS); } Again, if I create a variable of type string, I can’t initialize it in any way. This code works:
cdef public string uploadfile(string fileid, string username): cdef string s return s if you do like this, then segolitsya:
cdef public string uploadfile(string fileid, string username): cdef string s = <string>"text" return s The problem is solved, if someone prompts the solution better - I will be glad:
cdef public string uploadfile(string fileid, string username): cdef char *c_str_0 = <char*> "Simple string from python \nfile = " cdef string simple_string = <string> c_str_0 simple_string += fileid return simple_string