From 85d8d70411979cda6e409d82db8c9e78c95db279 Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 21 Jan 2016 12:27:12 +0100 Subject: [PATCH] + simplify porting of Image module to Python3 --- src/Mod/Image/App/AppImage.cpp | 33 +++++++++---- src/Mod/Image/Gui/AppImageGui.cpp | 12 ++--- src/Mod/Image/Gui/AppImageGuiPy.cpp | 72 ++++++++++++++++------------- 3 files changed, 69 insertions(+), 48 deletions(-) diff --git a/src/Mod/Image/App/AppImage.cpp b/src/Mod/Image/App/AppImage.cpp index 8859b84e9..d77b5de56 100644 --- a/src/Mod/Image/App/AppImage.cpp +++ b/src/Mod/Image/App/AppImage.cpp @@ -14,24 +14,41 @@ # include #endif +#include +#include + #include #include "ImagePlane.h" -/* registration table */ -static struct PyMethodDef Image_methods[] = { - {NULL, NULL} /* end of table marker */ +namespace Image { +class Module : public Py::ExtensionModule +{ +public: + Module() : Py::ExtensionModule("Image") + { + initialize("This module is the Image module."); // register with Python + } + + virtual ~Module() {} + +private: }; +PyObject* initModule() +{ + return (new Module)->module().ptr(); +} + +} // namespace Image + /* Python entry */ -extern "C" { -void ImageExport initImage() { - (void) Py_InitModule("Image", Image_methods); /* mod name, table ptr */ +PyMODINIT_FUNC initImage() +{ + (void) Image::initModule(); Base::Console().Log("Loading Image module... done\n"); Image::ImagePlane::init(); return; } - -} // extern "C" diff --git a/src/Mod/Image/Gui/AppImageGui.cpp b/src/Mod/Image/Gui/AppImageGui.cpp index 6ca8c54ff..182ada2c4 100644 --- a/src/Mod/Image/Gui/AppImageGui.cpp +++ b/src/Mod/Image/Gui/AppImageGui.cpp @@ -31,20 +31,20 @@ void loadImageResource() Gui::Translator::instance()->refresh(); } -/* registration table */ -extern struct PyMethodDef ImageGui_Import_methods[]; +namespace ImageGui { +extern PyObject* initModule(); +} /* Python entry */ -extern "C" { -void ImageGuiExport initImageGui() +PyMODINIT_FUNC initImageGui() { if (!Gui::Application::Instance) { PyErr_SetString(PyExc_ImportError, "Cannot load Gui module in console application."); return; } - (void) Py_InitModule("ImageGui", ImageGui_Import_methods); /* mod name, table ptr */ + (void) ImageGui::initModule(); Base::Console().Log("Loading GUI of Image module... done\n"); // instantiating the commands @@ -56,5 +56,3 @@ void ImageGuiExport initImageGui() // add resources and reloads the translators loadImageResource(); } - -} // extern "C" { diff --git a/src/Mod/Image/Gui/AppImageGuiPy.cpp b/src/Mod/Image/Gui/AppImageGuiPy.cpp index f9fef2a3b..261ae29e6 100644 --- a/src/Mod/Image/Gui/AppImageGuiPy.cpp +++ b/src/Mod/Image/Gui/AppImageGuiPy.cpp @@ -27,6 +27,9 @@ # include #endif +#include +#include + #include "ImageView.h" #include @@ -36,21 +39,32 @@ #include #include -using namespace ImageGui; - - -/* module functions */ -static PyObject * -open(PyObject *self, PyObject *args) +namespace ImageGui { +class Module : public Py::ExtensionModule { - char* Name; - const char* DocName=0; - if (!PyArg_ParseTuple(args, "et|s","utf-8",&Name,&DocName)) - return NULL; - std::string EncodedName = std::string(Name); - PyMem_Free(Name); +public: + Module() : Py::ExtensionModule("ImageGui") + { + add_varargs_method("open",&Module::open + ); + add_varargs_method("insert",&Module::open + ); + initialize("This module is the ImageGui module."); // register with Python + } + + virtual ~Module() {} + +private: + Py::Object open(const Py::Tuple& args) + { + char* Name; + const char* DocName=0; + if (!PyArg_ParseTuple(args.ptr(), "et|s","utf-8",&Name,&DocName)) + throw Py::Exception(); + + std::string EncodedName = std::string(Name); + PyMem_Free(Name); - PY_TRY { QString fileName = QString::fromUtf8(EncodedName.c_str()); QFileInfo file(fileName); @@ -73,8 +87,9 @@ open(PyObject *self, PyObject *args) } } } - else - Py_Error(PyExc_IOError, "Could not load image file"); + else { + throw Py::Exception(PyExc_IOError, "Could not load image file"); + } // Displaying the image in a view. // This ImageView object takes ownership of the pixel data (in 'pointImageTo') so we don't need to delete it here @@ -85,22 +100,13 @@ open(PyObject *self, PyObject *args) Gui::getMainWindow()->addWindow( iView ); iView->pointImageTo((void *)pPixelData, (unsigned long)imageq.width(), (unsigned long)imageq.height(), format, 0, true); - } PY_CATCH; - - Py_Return; -} - - -/* module functions */ -static PyObject * -insert(PyObject *self, PyObject *args) -{ - return open(self, args); -} - -/* registration table */ -struct PyMethodDef ImageGui_Import_methods[] = { - {"open" ,open , 1}, /* method name, C func ptr, always-tuple */ - {"insert" ,insert, 1}, - {NULL, NULL} /* end of table marker */ + return Py::None(); + } }; + +PyObject* initModule() +{ + return (new Module)->module().ptr(); +} + +} // namespace ImageGui