diff --git a/bazaar/BoostPyTest/BoostPyTest.h b/bazaar/BoostPyTest/BoostPyTest.h index fb6321f8a..3dd7ae7df 100644 --- a/bazaar/BoostPyTest/BoostPyTest.h +++ b/bazaar/BoostPyTest/BoostPyTest.h @@ -11,10 +11,39 @@ using namespace Upp; struct World { void set(std::string msg) { this->msg = msg; } - std::string greet() { return msg; } + std::string greet() const { return msg; } std::string msg; }; +BOOST_PYTHON_MODULE(hello) +{ + scope().attr("__doc__") = "Hello module's docstring"; + class_("World", "A simple world") + .def("greet", &World::greet) + .def("set", &World::set) + ; +} + +struct SliderCtrlPy +{ + SliderCtrlPy(SliderCtrl& o) : o(o) {} + + //void Set(const int& d) { o.SetData(d); } + void Set(int d) { o.SetData(d); } + int Get() const { return o.GetData(); } + + SliderCtrl& o; +}; + +BOOST_PYTHON_MODULE(UppCtrl) +{ + scope().attr("__doc__") = "ctrl module's docstring"; + class_("SliderCtrlPy", "A SliderCtrl wrapper", no_init) + .def("get", &SliderCtrlPy::Get) + .def("set", &SliderCtrlPy::Set) + ; +} + #define LAYOUTFILE #include @@ -23,6 +52,9 @@ public: typedef BoostPyTest CLASSNAME; BoostPyTest(); ~BoostPyTest(); + + World w; + SliderCtrlPy slpy; }; #endif diff --git a/bazaar/BoostPyTest/BoostPyTest.lay b/bazaar/BoostPyTest/BoostPyTest.lay index 2554698a1..5c6ff7c02 100644 --- a/bazaar/BoostPyTest/BoostPyTest.lay +++ b/bazaar/BoostPyTest/BoostPyTest.lay @@ -1,4 +1,5 @@ LAYOUT(BoostPyTestLayout, 672, 380) - ITEM(PyConsoleCtrl, con, HSizePosZ(0, 0).VSizePosZ(0, 0)) + ITEM(PyConsoleCtrl, con, HSizePosZ(0, 248).VSizePosZ(0, 0)) + ITEM(SliderCtrl, sl, RightPosZ(8, 232).TopPosZ(12, 48)) END_LAYOUT diff --git a/bazaar/BoostPyTest/main.cpp b/bazaar/BoostPyTest/main.cpp index dbec70209..367a3be2d 100644 --- a/bazaar/BoostPyTest/main.cpp +++ b/bazaar/BoostPyTest/main.cpp @@ -1,18 +1,15 @@ #include "BoostPyTest.h" -BOOST_PYTHON_MODULE(hello) -{ - class_("World") - .def("greet", &World::greet) - .def("set", &World::set) - ; -} - BoostPyTest::BoostPyTest() + : slpy(sl) { CtrlLayout(*this, "Boost Test"); - int ret = PyImport_AppendInittab( "hello", &inithello ); + //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(); @@ -33,11 +30,23 @@ BoostPyTest::BoostPyTest() //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 ) { diff --git a/bazaar/PyCon/PyCon.cpp b/bazaar/PyCon/PyCon.cpp index 5933f02d7..177c9365a 100644 --- a/bazaar/PyCon/PyCon.cpp +++ b/bazaar/PyCon/PyCon.cpp @@ -50,7 +50,7 @@ ONCELOCK "\t\tlog.ToStdOut(str)\n" "class StdErrIncept:\n" "\tdef write(self, str):\n" - "\t\tlog.ToStderr(str)\n" + "\t\tlog.ToStdErr(str)\n" ); } Enable(); @@ -61,11 +61,13 @@ void PyCon::Enable(bool b) if(enabled = b) PyRun_SimpleString ( "sys.stdout = StdOutIncept()\n" - "sys.stderr = StdErrIncept()\n" ); + "sys.stderr = StdErrIncept()\n" + ); else PyRun_SimpleString ( "sys.stdout = sys.__stdout__\n" - "sys.stderr = sys.__stderr__\n" ); + "sys.stderr = sys.__stderr__\n" + ); } //for having an echo print of return PyObject, is simply a copy of PyRun_SimpleStringFlags, with Py_single_input