I tried to implement the idea you demonstrated. In the following python script, katana is a staticly linked module and GetInterface returns my Interface instance. The way I call scripts is through a (python) scheduler which calls next() on each loaded script (each script provides a generator called 'main').
When I try to access my instance through python my app crashes.
from __future__ import generators #python 2.3 won't work with boost yetprint "test_interface.py> loaded script"import katanaprint "after import katana"def main(): print "script> Main" print katana.GetInterface() #prints "<katana.Interface object at 0x099868B4<" print "never gets here" yield None
and my boost:
ython ststic module katana definition
#ifndef __PYKATANA_HPP#define __PYKATANA_HPP#include <boost/python.hpp>//#include <boost/python/object.hpp>//#include <boost/python/def.hpp>using namespace boost::Python;#include "interface.hpp"extern Interface systems;object GetInterface() {return object(systems);}BOOST_PYTHON_MODULE(katana){ class_<Interface>( "Interface", init<>() ) .def_readonly( "clock", &Interface::clock ); class_<Clock>( "Clock", init<>() ) .def( "GetTimeSincecLastTick", &Clock::GetTimeSinceLastTick ) .def( "GetTime", &Clock::GetTime ) .def( "GetExactTime", &Clock::GetExactTime ) .def( "GetAvgFps", &Clock::GetAvgFps ) .def( "GetExactFps", &Clock::GetExactFps ) .def( "GetElapsedTimeSec", &Clock::GetElapsedTimeSec ) .def( "GetStartTime", &Clock::GetStartTime ) .def( "GetFramecount", &Clock::GetFramecount ); //def( "GetInterface", GetInterface, return_value_policy<reference_existing_object>() ); //this doesn't compile, but I think this is the key to my problem def( "GetInterface", GetInterface );}#endif //__PYKATANA_HPP
Elsewhere in my program I call 'initkatana()'.
What am I doing wrong?
ctoan: I have no idea how to gow about with your other suggestion
try adding it to themodule's global dictionary, and then in each function doing thewhole 'global systems' dance to get the global variable.
Could you get me started? Ideally, I'd expose only the member variables of systems (so I'd do 'katana.clock' as opposed to 'katana.systems.clock'). And if I don't modify each instance (expose them readonly), it doesn't have to be global in each function, right?
Thanks for your help.
PS: ctoan, I was looking through old forum posts and you posted one of your early embedding attempts for someone else's reference. (it had class CBlah) You had 'static CBlah something("Joe");' in it, but you never exposed something to python - did you do this in a later trial? That is exactly what I'm trying to do.
EDIT: goddamn bbcodes
[edited by - thedustbustr on August 7, 2003 7:46:18 PM] [edited by - thedustbustr on August 7, 2003 7:47:39 PM] [edited by - thedustbustr on August 7, 2003 7:51:19 PM]