[C++-sig] Passing C++ instance to embedded py

fileoffset fileoffset at gmail.com
Thu Nov 20 06:29:49 CET 2008


To best explain my problem, here is some code:

struct A
{
	A()
	{
		mTest = 1;

		std::cout << "Test: " << mTest;

		Py_Initialize();
		object main_module = import("__main__");
		object main_namespace = main_module.attr("__dict__");

		main_namespace["A_Instance"] = this;

		object result = exec_file(str("test.py"), main_namespace, main_namespace);

		std::cout << "Test: " << mTest;
	}

	int mTest;
}

in main.cpp or similar:

BOOST_PYTHON_MODULE(MyModule)
{
    class_<A>("A", init<>())
        .def_readwrite("Test", &A::mTest)
    ;
}

test.py:

from MyModule import *

A_Instance.Test = 5


Essentially I want to be able to access and modify the properties of an instance of a C++ class
at runtime.

When I try code similar to above, python excepts, as I believe it attempts to copy-construct
the A class instead of just pass the reference (this) so that i can modify it.

Is what I want possible? If not what would be a better way to do it?

-- 
file offset <fileoffset at gmail.com>



More information about the Cplusplus-sig mailing list