X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=PythonPublisher.cc;h=56d7bc477d6164b82718f5bcb0aa729c42c80859;hb=c39edbfdec96e88bdafa0b771939924f73799e84;hp=c75fc88520729ab53c7c5c7f7282b95cba587e2a;hpb=24533960cdd5783a6b071da50e45cf45033afe66;p=pykit.git diff --git a/PythonPublisher.cc b/PythonPublisher.cc index c75fc88..56d7bc4 100644 --- a/PythonPublisher.cc +++ b/PythonPublisher.cc @@ -10,6 +10,8 @@ //#include "PythonPublisher.ih" // Custom includes +#include +#include #include "Publisher.hh" //#include "PythonPublisher.mpp" @@ -18,6 +20,15 @@ namespace py = boost::python; +struct pykit::PythonPublisher::Impl +{ + boost::python::dict mainNamespace; + Publisher * pythonPublisher; +}; + +#define PYTHON_PREPARE_IMPORT(module) \ + PyImport_AppendInittab(const_cast(#module), init ## module) + namespace { struct PublisherPyWrapper @@ -34,7 +45,7 @@ namespace { static PyObject * convert(QString const & s) { std::wstring ws (s.toStdWString()); - return PyUnicode_FromWideChar(ws.c_str(), ws.length()); + return PyUnicode_FromWideChar(ws.data(), ws.length()); } }; @@ -88,10 +99,11 @@ namespace { static void construct(PyObject * o, py::converter::rvalue_from_python_stage1_data * data) { + unsigned length (PyString_Size(o)); const char * value (PyString_AsString(o)); void * storage (((py::converter::rvalue_from_python_storage*) data)->storage.bytes); - new (storage) QByteArray(value); + new (storage) QByteArray(value,length); data->convertible = storage; } @@ -420,22 +432,32 @@ BOOST_PYTHON_MODULE(_pykit) } prefix_ pykit::PythonPublisher::PythonPublisher(std::string initPy) + : impl_ (new Impl) { - PYTHON_PREPARE_IMPORT(_pykit); - PYTHON_PREPARE_IMPORT(_qt); - Py_Initialize(); - py::object mainModule_ = py::import("__main__"); - mainNamespace_ = py::extract(mainModule_.attr("__dict__")); - mainNamespace_["__file__"] = py::str(initPy.c_str()); - py::object ignored ( - py::exec_file(initPy.c_str(), mainNamespace_, mainNamespace_)); - pythonPublisher_ = py::extract(mainNamespace_["publisher"]); + try { + PYTHON_PREPARE_IMPORT(_pykit); + PYTHON_PREPARE_IMPORT(_qt); + Py_Initialize(); + py::object mainModule_ = py::import("__main__"); + impl_->mainNamespace = py::extract(mainModule_.attr("__dict__")); + impl_->mainNamespace["__file__"] = py::str(initPy.c_str()); + py::object ignored ( + py::exec_file(initPy.c_str(), impl_->mainNamespace, impl_->mainNamespace)); + impl_->pythonPublisher = py::extract(impl_->mainNamespace["publisher"]); + } + catch (boost::python::error_already_set & ex) { + PyErr_Print(); + throw; + } } +prefix_ pykit::PythonPublisher::~PythonPublisher() +{} + prefix_ void pykit::PythonPublisher::publish(Request & request) { try { - pythonPublisher_->publish(request); + impl_->pythonPublisher->publish(request); } catch (py::error_already_set & ex) { PyErr_Print();