diff --git a/bazaar/BoostPyTest/BoostPyTest.h b/bazaar/BoostPyTest/BoostPyTest.h index 280b35994..968edbded 100644 --- a/bazaar/BoostPyTest/BoostPyTest.h +++ b/bazaar/BoostPyTest/BoostPyTest.h @@ -1,32 +1,12 @@ #ifndef _BoostPyTest_BoostPyTest_h_ #define _BoostPyTest_BoostPyTest_h_ -#include -using namespace boost::python; +//py stuff needs go first +#include "PyEx.h" -#include "world.h" -#include "PyConsoleCtrl.h" -#include +#include using namespace Upp; -NAMESPACE_UPP - -decl_export_Callback1(int); -decl_export_Gate1(int); -decl_export_Callback1(Value); -decl_export_Gate1(Value); - -decl_export_Callback2(int, double); -decl_export_Gate2(int, double); - -decl_export_Callback3(int, double, bool); -decl_export_Gate3(int, double, bool); - -decl_export_Callback4(int, double, bool, int64); -decl_export_Gate4(int, double, bool, int64); - -END_UPP_NAMESPACE - #define LAYOUTFILE #include @@ -36,12 +16,13 @@ public: BoostPyTest(); ~BoostPyTest(); + void InitPyEnv(); void ExitHandler(); void CBi(int i); void EvalCB(); void EvalPyCv(); - object main_namespace; + boost::python::object main_namespace; #if PUREVIRTEST #else diff --git a/bazaar/BoostPyTest/BoostPyTest.upp b/bazaar/BoostPyTest/BoostPyTest.upp index 29bfd2d7c..c6b020d2b 100644 --- a/bazaar/BoostPyTest/BoostPyTest.upp +++ b/bazaar/BoostPyTest/BoostPyTest.upp @@ -9,12 +9,15 @@ file PyConsoleCtrl.cpp, world.h, world.cpp, + PyEx.h, + PyEx.icpp, app readonly separator, - compile.txt, - modules.cppi, BoostPyTest.h, main.cpp, - BoostPyTest.lay; + BoostPyTest.lay, + modules.cppi, + pyenv.cpp, + compile.txt; mainconfig "" = "GUI MT SSE2 BOOSTPY"; diff --git a/bazaar/BoostPyTest/PyConsoleCtrl.cpp b/bazaar/BoostPyTest/PyConsoleCtrl.cpp index 0d08d5d92..c0387f3f5 100644 --- a/bazaar/BoostPyTest/PyConsoleCtrl.cpp +++ b/bazaar/BoostPyTest/PyConsoleCtrl.cpp @@ -1,9 +1,6 @@ #include "PyConsoleCtrl.h" using namespace boost::python; -#include -using namespace Upp; - NAMESPACE_UPP void export_PyConsoleCtrl() diff --git a/bazaar/BoostPyTest/PyConsoleCtrl.h b/bazaar/BoostPyTest/PyConsoleCtrl.h index 27d5a5475..5c9cb7105 100644 --- a/bazaar/BoostPyTest/PyConsoleCtrl.h +++ b/bazaar/BoostPyTest/PyConsoleCtrl.h @@ -1,6 +1,7 @@ #ifndef _BoostPyTest_PyConsoleCtrl_h_ #define _BoostPyTest_PyConsoleCtrl_h_ +#include #include NAMESPACE_UPP @@ -9,5 +10,4 @@ void export_PyConsoleCtrl(); END_UPP_NAMESPACE - #endif diff --git a/bazaar/BoostPyTest/PyEx.h b/bazaar/BoostPyTest/PyEx.h new file mode 100644 index 000000000..e38b80997 --- /dev/null +++ b/bazaar/BoostPyTest/PyEx.h @@ -0,0 +1,28 @@ +#ifndef _BoostPyTest_PyEx_h_ +#define _BoostPyTest_PyEx_h_ + +//py stuff needs go first +#include +#include +#include "PyConsoleCtrl.h" +#include "world.h" + +NAMESPACE_UPP + +decl_export_Callback1(int); +decl_export_Gate1(int); +decl_export_Callback1(Value); +decl_export_Gate1(Value); + +decl_export_Callback2(int, double); +decl_export_Gate2(int, double); + +decl_export_Callback3(int, double, bool); +decl_export_Gate3(int, double, bool); + +decl_export_Callback4(int, double, bool, int64); +decl_export_Gate4(int, double, bool, int64); + +END_UPP_NAMESPACE + +#endif diff --git a/bazaar/BoostPyTest/PyEx.icpp b/bazaar/BoostPyTest/PyEx.icpp new file mode 100644 index 000000000..18d937f36 --- /dev/null +++ b/bazaar/BoostPyTest/PyEx.icpp @@ -0,0 +1,20 @@ +#include "PyEx.h" +using namespace boost::python; + +NAMESPACE_UPP + +def_export_Callback1(int) +def_export_Gate1(int) +def_export_Callback1(Value) +def_export_Gate1(Value) + +def_export_Callback2(int, double) +def_export_Gate2(int, double) + +def_export_Callback3(int, double, bool) +def_export_Gate3(int, double, bool) + +def_export_Callback4(int, double, bool, int64) +def_export_Gate4(int, double, bool, int64) + +END_UPP_NAMESPACE diff --git a/bazaar/BoostPyTest/init b/bazaar/BoostPyTest/init index 9aa5ffb6e..c619da297 100644 --- a/bazaar/BoostPyTest/init +++ b/bazaar/BoostPyTest/init @@ -4,4 +4,7 @@ #include "PyConsoleCtrl/init" #include "ValueCtrl/init" #include "CtrlLibBoostPy/init" +#define BLITZ_INDEX__ F77A55E940E891CC1429BF25681A67728 +#include "PyEx.icpp" +#undef BLITZ_INDEX__ #endif diff --git a/bazaar/BoostPyTest/main.cpp b/bazaar/BoostPyTest/main.cpp index 670af2383..079f630b4 100644 --- a/bazaar/BoostPyTest/main.cpp +++ b/bazaar/BoostPyTest/main.cpp @@ -1,268 +1,11 @@ #include "BoostPyTest.h" -// CallbackX / GateX definitions need to go before the module include -// it's kinda template instantiation - -NAMESPACE_UPP - -def_export_Callback1(int) -def_export_Gate1(int) -def_export_Callback1(Value) -def_export_Gate1(Value) - -def_export_Callback2(int, double) -def_export_Gate2(int, double) - -def_export_Callback3(int, double, bool) -def_export_Gate3(int, double, bool) - -def_export_Callback4(int, double, bool, int64) -def_export_Gate4(int, double, bool, int64) - -END_UPP_NAMESPACE - -//CAUTION special module definitions to reduce compile time -#include "modules.cppi" - -String GetDataDir() -{ - String s = GetEnv("UPP_MAIN__"); - return s.GetCount() ? s : GetFileFolder(GetExeFilePath()); -} - -void BoostPyTest::CBi(int i) -{ - RLOG(i); -} - -dict swap_object_dict(object target, dict d) -{ - dict result = extract(target.attr("__dict__")); - target.attr("__dict__") = d; - return result; -} - -void BoostPyTest::EvalCB() -{ - String s = ev.GetData(); - - Value v; - try - { - object o = eval(s.Begin(), main_namespace, main_namespace); - v = extract(o); - } - catch(boost::python::error_already_set const &) - { - // Parse and output the exception - std::string perror_str = parse_py_exception(); - String es = perror_str; - v = ErrorValue(es); - } - - evr.SetData(v); -} - -void BoostPyTest::EvalPyCv() -{ - Value v = invc.GetData(); - pycv.expr = evcv.GetData(); - Value w = pycv.Format(v); - outvc.SetData(w); -} - BoostPyTest::BoostPyTest() { CtrlLayout(*this, "Boost Test"); Sizeable().Zoomable(); - //bool b = Py_IsInitialized(); - - int ret; - ret = PyImport_AppendInittab( "hello", &inithello ); - ret = PyImport_AppendInittab( "upp", &initupp ); - - //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__")))); - main_namespace = main_module.attr("__dict__"); - - //add path - String s = GetDataDir(); - object sys = import("sys"); - object path = sys.attr("path"); - path.attr("append")(s.Begin()); - - 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 - //is only caused on boost python module declarations - main_namespace["hello"] = hello_module; - -#if PUREVIRTEST -#else - w.set("Welcom on earth"); - scope(hello_module).attr("earth") = ptr(&w); -#endif - - object upp_module = import("upp"); - main_namespace["upp"] = upp_module; - - Value v = "Teststring"; - //object tstr(v); - main_namespace["teststr"] = object(v); - -if(0) { - object locals; - locals = dict(); - locals["arg"] = object(v); - main_namespace["loc"] = locals; -} - scope(upp_module).attr("app") = ptr((TopWindow*)this); - - scope(upp_module).attr("con") = ptr(&con); - - sl.SetData(50); - scope(upp_module).attr("sl") = ptr(&sl); - - vc.SetData(123); - scope(upp_module).attr("vc") = ptr(&vc); - - es.SetData("Hello from Upp"); - scope(upp_module).attr("es") = ptr(&es); - - scope(upp_module).attr("pr") = ptr(&pr); - - cbi = THISBACK(CBi); - scope(upp_module).attr("cbi") = ptr(&cbi); - - object o = eval("100 + upp.sl.data", main_namespace, main_namespace); - int data = extract(o); - - ev.WhenEnter = THISBACK(EvalCB); - ex <<= THISBACK(EvalCB); - evr.SetReadOnly(); - ev.SetData("100 + upp.sl.getdata()"); - EvalCB(); - - pycv.globals = main_namespace; - pycv.locals = dict(); - excv <<= THISBACK(EvalPyCv); - evcv.SetData("arg + ' ' + upp.app.title"); - invc.SetData("Please try"); - outvc.SetReadOnly(); - - String sc = - - "upp.app.title = 'My Big BoostPy Test Environment'\n" - -#if PUREVIRTEST -#else - "p = hello.World()\n" - "p.set('Some Greet Text')\n" - "print p.get()\n" -#endif - - "upp.sl.data = 75\n" - "print upp.sl.data\n" - - "upp.vc.data = range(10)\n" - "print upp.vc.data\n" - - "print upp.es.data\n" - "upp.es.data = 'Another Text from Upp'\n" - "print upp.es.data\n" - - "c = upp.Color(123,124,125)\n" - "print c\n" - -#if PUREVIRTEST -#else - "pp = p\n" - "p.set('Hi')\n" - "print p\n" - "print pp\n" -#endif - - //"upp.vc.data = c\n" - - "import time\n" - "upp.pr.reset()\n" - "upp.pr.create()\n" - "upp.pr.canceled()\n" - "i = 0\n" - "while i < 3:\n" - " print i\n" - " upp.pr.text = str(i)\n" - " i = i+1\n" - " upp.pr.step(1)\n" - " time.sleep(1)\n" - " if upp.pr.canceled():\n" - " break\n" - "upp.pr.close()\n" - - "def ff():\n" - " print 'MyCallback, would could work with p.greet or lambda: as well'\n" - "upp.sl.whenaction = ff\n" - - "sl2 = upp.SliderCtrl()\n" - "sl2.rightpos(0,200)\n" - "sl2.bottompos(0,40)\n" - "sl2.whenaction = ff\n" - "sl2.data = 12\n" - "upp.app.add(sl2)\n" - - "print 'USE DEBUG TO SEE THE INVOKATIONS in WrapWorld'\n" -#if PUREVIRTEST - "class MyPureWorld(hello.World):\n" - " def vir(self, a):\n" - " return 'MyPurePyWorld'\n" - "mw = MyPureWorld()\n" - "print mw.vir(8)\n" - "print hello.invworld(mw,7)\n" -#else - "w = hello.World()\n" - "print hello.invworld(w,7)\n" - "print w.vir(7)\n" - - "class MyWorld(hello.World):\n" - " def vir(self, a):\n" - " return 'MyPyWorld'\n" - "mw = MyWorld()\n" - "print mw.vir(8)\n" - "print hello.invworld(mw,7)\n" -#endif - - "class MyUWorld(hello.Universe):\n" - " def vir(self, a):\n" - " return 'MyPyUWorld'\n" - "mu = MyUWorld()\n" - "print mu.vir(8)\n" - "print hello.invworld(mu,7)\n" - - "import fibo\n" - "print fibo.fib(200)\n" - - "import myupp\n" - "myupp.chtitle()\n" - - ; - con.cmd.SetData(sc); - - } catch( error_already_set ) { - PyErr_Print(); - } + InitPyEnv(); } BoostPyTest::~BoostPyTest() diff --git a/bazaar/BoostPyTest/pyenv.cpp b/bazaar/BoostPyTest/pyenv.cpp new file mode 100644 index 000000000..e8a07b468 --- /dev/null +++ b/bazaar/BoostPyTest/pyenv.cpp @@ -0,0 +1,243 @@ +#include "BoostPyTest.h" +using namespace boost::python; + +//CAUTION special module definitions to reduce compile time +#include "modules.cppi" + +String GetDataDir() +{ + String s = GetEnv("UPP_MAIN__"); + return s.GetCount() ? s : GetFileFolder(GetExeFilePath()); +} + +void BoostPyTest::CBi(int i) +{ + RLOG(i); +} + +dict swap_object_dict(object target, dict d) +{ + dict result = extract(target.attr("__dict__")); + target.attr("__dict__") = d; + return result; +} + +void BoostPyTest::EvalCB() +{ + String s = ev.GetData(); + + Value v; + try + { + object o = eval(s.Begin(), main_namespace, main_namespace); + v = extract(o); + } + catch(boost::python::error_already_set const &) + { + // Parse and output the exception + std::string perror_str = parse_py_exception(); + String es = perror_str; + v = ErrorValue(es); + } + + evr.SetData(v); +} + +void BoostPyTest::EvalPyCv() +{ + Value v = invc.GetData(); + pycv.expr = evcv.GetData(); + Value w = pycv.Format(v); + outvc.SetData(w); +} + +void BoostPyTest::InitPyEnv() +{ + //bool b = Py_IsInitialized(); + + int ret; + ret = PyImport_AppendInittab( "hello", &inithello ); + ret = PyImport_AppendInittab( "upp", &initupp ); + + //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__")))); + main_namespace = main_module.attr("__dict__"); + + //add path + String s = GetDataDir(); + object sys = import("sys"); + object path = sys.attr("path"); + path.attr("append")(s.Begin()); + + 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 + //is only caused on boost python module declarations + main_namespace["hello"] = hello_module; + +#if PUREVIRTEST +#else + w.set("Welcom on earth"); + scope(hello_module).attr("earth") = ptr(&w); +#endif + + object upp_module = import("upp"); + main_namespace["upp"] = upp_module; + + Value v = "Teststring"; + //object tstr(v); + main_namespace["teststr"] = object(v); + +if(0) { + object locals; + locals = dict(); + locals["arg"] = object(v); + main_namespace["loc"] = locals; +} + scope(upp_module).attr("app") = ptr((TopWindow*)this); + + scope(upp_module).attr("con") = ptr(&con); + + sl.SetData(50); + scope(upp_module).attr("sl") = ptr(&sl); + + vc.SetData(123); + scope(upp_module).attr("vc") = ptr(&vc); + + es.SetData("Hello from Upp"); + scope(upp_module).attr("es") = ptr(&es); + + scope(upp_module).attr("pr") = ptr(&pr); + + cbi = THISBACK(CBi); + scope(upp_module).attr("cbi") = ptr(&cbi); + + object o = eval("100 + upp.sl.data", main_namespace, main_namespace); + int data = extract(o); + + ev.WhenEnter = THISBACK(EvalCB); + ex <<= THISBACK(EvalCB); + evr.SetReadOnly(); + ev.SetData("100 + upp.sl.getdata()"); + EvalCB(); + + pycv.globals = main_namespace; + pycv.locals = dict(); + excv <<= THISBACK(EvalPyCv); + evcv.SetData("arg + ' ' + upp.app.title"); + invc.SetData("Please try"); + outvc.SetReadOnly(); + + String sc = + + "upp.app.title = 'My Big BoostPy Test Environment'\n" + +#if PUREVIRTEST +#else + "p = hello.World()\n" + "p.set('Some Greet Text')\n" + "print p.get()\n" +#endif + + "upp.sl.data = 75\n" + "print upp.sl.data\n" + + "upp.vc.data = range(10)\n" + "print upp.vc.data\n" + + "print upp.es.data\n" + "upp.es.data = 'Another Text from Upp'\n" + "print upp.es.data\n" + + "c = upp.Color(123,124,125)\n" + "print c\n" + +#if PUREVIRTEST +#else + "pp = p\n" + "p.set('Hi')\n" + "print p\n" + "print pp\n" +#endif + + //"upp.vc.data = c\n" + + "import time\n" + "upp.pr.reset()\n" + "upp.pr.create()\n" + "upp.pr.canceled()\n" + "i = 0\n" + "while i < 3:\n" + " print i\n" + " upp.pr.text = str(i)\n" + " i = i+1\n" + " upp.pr.step(1)\n" + " time.sleep(1)\n" + " if upp.pr.canceled():\n" + " break\n" + "upp.pr.close()\n" + + "def ff():\n" + " print 'MyCallback, would could work with p.greet or lambda: as well'\n" + "upp.sl.whenaction = ff\n" + + "sl2 = upp.SliderCtrl()\n" + "sl2.rightpos(0,200)\n" + "sl2.bottompos(0,40)\n" + "sl2.whenaction = ff\n" + "sl2.data = 12\n" + "upp.app.add(sl2)\n" + + "print 'USE DEBUG TO SEE THE INVOKATIONS in WrapWorld'\n" +#if PUREVIRTEST + "class MyPureWorld(hello.World):\n" + " def vir(self, a):\n" + " return 'MyPurePyWorld'\n" + "mw = MyPureWorld()\n" + "print mw.vir(8)\n" + "print hello.invworld(mw,7)\n" +#else + "w = hello.World()\n" + "print hello.invworld(w,7)\n" + "print w.vir(7)\n" + + "class MyWorld(hello.World):\n" + " def vir(self, a):\n" + " return 'MyPyWorld'\n" + "mw = MyWorld()\n" + "print mw.vir(8)\n" + "print hello.invworld(mw,7)\n" +#endif + + "class MyUWorld(hello.Universe):\n" + " def vir(self, a):\n" + " return 'MyPyUWorld'\n" + "mu = MyUWorld()\n" + "print mu.vir(8)\n" + "print hello.invworld(mu,7)\n" + + "import fibo\n" + "print fibo.fib(200)\n" + + "import myupp\n" + "myupp.chtitle()\n" + + ; + con.cmd.SetData(sc); + + } catch( error_already_set ) { + PyErr_Print(); + } +} diff --git a/bazaar/CoreBoostPy/CoreBoostPy.cpp b/bazaar/CoreBoostPy/CoreBoostPy.cpp index 8e933bf2e..95c287956 100644 --- a/bazaar/CoreBoostPy/CoreBoostPy.cpp +++ b/bazaar/CoreBoostPy/CoreBoostPy.cpp @@ -1,4 +1,5 @@ #include "CoreBoostPy.h" +using namespace boost::python; NAMESPACE_UPP diff --git a/bazaar/CtrlLibBoostPy/CtrlLibBoostPy.cpp b/bazaar/CtrlLibBoostPy/CtrlLibBoostPy.cpp index cfd0ae32d..1f46bca4c 100644 --- a/bazaar/CtrlLibBoostPy/CtrlLibBoostPy.cpp +++ b/bazaar/CtrlLibBoostPy/CtrlLibBoostPy.cpp @@ -1,4 +1,5 @@ #include "CtrlLibBoostPy.h" +using namespace boost::python; NAMESPACE_UPP diff --git a/bazaar/Tree/Tree.cpp b/bazaar/Tree/Tree.cpp new file mode 100644 index 000000000..1372092b4 --- /dev/null +++ b/bazaar/Tree/Tree.cpp @@ -0,0 +1,2 @@ +#include "Tree.h" + diff --git a/bazaar/Tree/Tree.upp b/bazaar/Tree/Tree.upp index 435eb4eda..612fd6188 100644 --- a/bazaar/Tree/Tree.upp +++ b/bazaar/Tree/Tree.upp @@ -4,6 +4,7 @@ uses Core; file + Tree.cpp, Tree.h, src.tpp;