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

2008-11-19 Thread fileoffset

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", 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 <[EMAIL PROTECTED]>

___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig


[C++-sig] Instance access

2008-11-23 Thread fileoffset

Is it possible to access an object instance from within python?

I am trying to automate a single class that is part of a larger, complex
framework.  My idea is that I could load an embedded python script on
construction of the object, which would act on the 'this' or
'shared_from_this()' pointer. This would greatly simplify my automation.

I have tried it myself but it doesn't seem to work:

main_namespace["instance"] = this;

Is that ever going to work like I would want/expect? Or is there an
alternative or work around that I could use?

Please help! Any information is appreciated

-- 
file offset <[EMAIL PROTECTED]>

___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig