Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Embedded Python in C++
#11
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)
Reply
#12
another side question, is there a sample link to add a callback function pointer from c++ to python?
Reply
#13
(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
Output:
$ python3.8-config --embed --cflags -I/usr/include/python3.8 -I/usr/include/python3.8 -Wno-unused-result -Wsign-compare -g -fdebug-prefix-map=/build/python3.8-uvizni/python3.8-3.8.10=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector -Wformat -Werror=format-security -DNDEBUG -g -fwrapv -O3 -Wall $ python3.8-config --embed --ldflags -L/usr/lib/python3.8/config-3.8-x86_64-linux-gnu -L/usr/lib -lpython3.8 -lcrypt -lpthread -ldl -lutil -lm -lm
I added -fpie because the linker complained and suggested to add -fpie.
Reply
#14
(Jul-31-2022, 09:33 PM)Xeno Wrote: another side question, is there a sample link to add a callback function pointer from c++ to python?
I don't understand what you want. Can you explain it with more details?
Reply
#15
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"
Reply
#16
I would try
        self.spam_system.argtypes = (ctypes.py_object,)
        self.spam_system.restype = ctypes.py_object
Reply
#17
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")
Reply
#18
(Aug-02-2022, 04:31 AM)Xeno Wrote: but I am getting an Access violation on both PyArg_ParseTuple
You may need to include the 'self' argument as well, try with
self.spam_system.argtypes = (ctypes.py_object, ctypes.py_object,)
Reply
#19
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
Reply
#20
(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()...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to run detectron2, as python embedded code in C++, on GPU? hassaniqbal931 3 1,115 Nov-02-2023, 04:45 PM
Last Post: blabling2
  Adding libraries to embedded Python as a ZIP The_Oman 0 1,246 May-05-2023, 04:05 PM
Last Post: The_Oman
Bug Embedded Python Memory Leaks Alexei 1 1,036 Sep-16-2022, 01:15 PM
Last Post: Alexei
  Embedded python fails to compile on Raspberry Pi tryfon 2 3,494 Dec-22-2020, 02:06 PM
Last Post: tryfon
  Can Embedded Python run any shared library on Android ? sprotz 0 2,333 Nov-08-2020, 12:21 PM
Last Post: sprotz
  memory leak on embedded python in c++ asdf3721 3 3,408 Jul-16-2020, 06:33 AM
Last Post: Gribouillis
  Embedded Python PyObject_CallObject function JRHeisey 1 2,400 Nov-27-2019, 01:50 AM
Last Post: casevh
  Trying to implement Python into embedded OS thesurya7 2 2,417 Apr-02-2019, 06:38 PM
Last Post: ebolisa
  Multiple calls to Python interpreter embedded in C++ application yield segmentation f mmoelle1 0 2,842 Mar-21-2019, 08:54 PM
Last Post: mmoelle1
  python code (embedded in Redshift) to return result of the query Mel 0 2,462 Aug-24-2018, 06:12 PM
Last Post: Mel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020