ah i see, this works in release mode, but not in debug mode (i do have the python3_d.lib installed, don't know why the return will not work in debug build)
Embedded Python in C++
Embedded Python in C++
|
Jul-31-2022, 09:33 PM
another side question, is there a sample link to add a callback function pointer from c++ to python?
Jul-31-2022, 09:49 PM
(Jul-31-2022, 09:21 PM)Xeno Wrote: is there anything linker wise i need to add in vc++?Sorry I can't tell you: I don't know vs2019 nor the Windows OS. I got my compiler options by running these commands in the Linux terminal I added -fpie because the linker complained and suggested to add -fpie.
Jul-31-2022, 09:51 PM
Jul-31-2022, 10:47 PM
try to make a callback to the c++ from python code
added this c++ function extern "C" { PyObject* CPython::spam_system(PyObject* self, PyObject* args) { const char* command; int sts; if (!PyArg_ParseTuple(args, "s", &command)) return NULL; sts = system(command); return PyLong_FromLong(sts); } }trying to call back to c++ using ctypes, but not sure how to setup the args and returns when defining the c++ function in python import ctypes class TestClass: def __init__(self, count): self.count = count self.lib = ctypes.CDLL( r'C:\Users\WINSIM\PycharmProjects\testProject\cpp\cppTester\x64\Release\cppPythonWrapper.dll') # c++ function self.spam_system = getattr(self.lib, "?spam_system@CPython@@QEAAPEAU_object@@PEAU2@0@Z") # how to setup the c++ function callback? self.spam_system.argtypes = [c_string] self.spam_system.restype = c_long # the callback function def print_cb(self): self.spam_system("ls -s") def print_hi(self, name): if __name__ == '__main__': print(f'PYTHON: function is being run directly {name} {self.count}') self.print_cb() return "Completed" else: print(f'PYTHON: function is being imported {name} {self.count}') return "Skipped"
Aug-01-2022, 07:35 AM
I would try
self.spam_system.argtypes = (ctypes.py_object,) self.spam_system.restype = ctypes.py_object
Aug-02-2022, 04:31 AM
that does seem to fire the callback, and I can see the c++ getting the function calls
but I am getting an Access violation on both PyArg_ParseTuple or PyUnicode_AsEncodedString when trying to decode the PyObject* args (which is a tuple type) PyObject* CPython::spam_system(PyObject* self, PyObject* args) { const char* command; if (!PyArg_ParseTuple(args, "s", &command))PyObject* args don't seem to be populated? tuple(iterable=(), /) -- Built-in immutable sequence. If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable's items. If the argument is a tuple, the return value is the same object.my Python call to the c++ # the callback function def print_cb(self): self.spam_system("ls -s")
Aug-02-2022, 08:08 AM
Aug-03-2022, 01:55 AM
just want to close the issue, and thank you for all the help
I reverted back to 3.8.10, the Access violation just stopped, and i can decode the PyObjects from python now. no code change is need. don't know if this is a bug or not in 3.10
Aug-03-2022, 07:29 AM
(This post was last modified: Aug-03-2022, 07:29 AM by Gribouillis.)
(Aug-03-2022, 01:55 AM)Xeno Wrote: I reverted back to 3.8.10, the Access violation just stopped, and i can decode the PyObjects from python now.I don't use the CPython api very often, so I can't interprete the access violation. In Python 3.10 you could probably use the new PyObject_CallOneArg() function here, and also PyObject_CallMethod() ...
|
|
Users browsing this thread: 2 Guest(s)