ultimatepp/bazaar/BoostPyTest/main.cpp
kohait 3679491ce2 bazaar: PyCon, PyConsoleCtrl: proper exit() python funciton handling, now exit handler can terminate gui app
git-svn-id: svn://ultimatepp.org/upp/trunk@3337 f0d560ea-af0d-0410-9eb7-867de7ffcac7
2011-04-12 21:54:26 +00:00

73 lines
1.8 KiB
C++

#include "BoostPyTest.h"
BoostPyTest::BoostPyTest()
: slpy(sl)
{
CtrlLayout(*this, "Boost Test");
//bool b = Py_IsInitialized();
int ret;
ret = PyImport_AppendInittab( "hello", &inithello );
ret = PyImport_AppendInittab( "UppCtrl", &initUppCtrl );
//Py_Initialize(); //should be done *after* PyImport_AppendInittab, but it still works :), leaving INITBLOCK from Py
//PyCon::Init();
PyCon::AtExit = THISBACK(ExitHandler);
try {
object main_module(handle<>(borrowed(PyImport_AddModule("__main__"))));
object main_namespace = main_module.attr("__dict__");
handle<> ign(PyRun_String( "print \"Hello, World\"",
Py_file_input,
main_namespace.ptr(),
main_namespace.ptr() ));
object hello_module(handle<>(PyImport_ImportModule("hello")));
//MEM LEAK from PyImport_ImportModule ??
//http://mail.python.org/pipermail/python-win32/2008-August/008092.html
//seems to be well known and not correctable
//so better do it in python code
main_namespace["hello"] = hello_module;
w.set("Welcom on earth");
scope(hello_module).attr("earth") = ptr(&w);
slpy.Set(50);
object ctrl_module(handle<>(PyImport_ImportModule("UppCtrl")));
main_namespace["UppCtrl"] = ctrl_module;
scope(ctrl_module).attr("sl") = ptr(&slpy);
//the additional import is needless
String sc =
"import hello\n"
"p = hello.World()\n"
"p.set('Some Greet Text')\n"
"print p.greet()\n"
"print UppCtrl.sl.get()\n"
"UppCtrl.sl.set(23)\n"
;
con.cmd.SetData(sc);
} catch( error_already_set ) {
PyErr_Print();
}
}
BoostPyTest::~BoostPyTest()
{
// Py_Finalize();
}
void BoostPyTest::ExitHandler()
{
SetTimeCallback(1000, Breaker());
}
GUI_APP_MAIN
{
BoostPyTest().Run();
}