Passing a C++ pointer between C and Python -


i have python extension module written in c++, contains multiple functions. 1 of these generates instance of custom structure, want use other functions of module in python follows

import mymodule var = mymodule.genfunc() mymodule.readfunc(var) 

to this, i've tried using pycapsule objects pass pointer these objects between python , c, produces errors when attempting read them in second c function ("pycapsule_getpointer called invalid pycapsule object"). python, however, if asked print pycapsule object (var) correctly identifies "'capsule object "testcapsule"'. c code appears follows:

struct mystruct {     int value; };  static pyobject* genfunc(pyobject* self, pyobject *args) {     mystruct var;     pyobject *capsuletest;      var.value = 1;     capsuletest = pycapsule_new(&var, "testcapsule", null);     return capsuletest; }  static pyobject* readfunc(pyobject* self, pyobject *args) {     pycapsule_getpointer(args, "testcapsule");     return 0; } 

thank help.

like stated in comment question, you'll run issue when reading data local variable mystruct var. can use third destructor pycapsule_new.

but that's not reason problem now. you're using pycapsule_getpointer(args, "testcapsule") on args parameter. , since it's not capsule, though var one, might have defined signature of function meth_varargs. instead need unpack tuple or use meth_o.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -