diff --git a/src/Base/PyObjectBase.h b/src/Base/PyObjectBase.h index 1b9ae3fc0..1ae0a52d8 100644 --- a/src/Base/PyObjectBase.h +++ b/src/Base/PyObjectBase.h @@ -84,6 +84,21 @@ */ #define PYFUNCIMP_S(CLASS,SFUNC) PyObject* CLASS::SFUNC (PyObject *self,PyObject *args,PyObject *kwd) + +/** Macro for initialization function of Python modules. + */ +#if PY_MAJOR_VERSION >= 3 +# define PyMOD_INIT_FUNC(name) PyMODINIT_FUNC PyInit_##name(void) +#else +# define PyMOD_INIT_FUNC(name) PyMODINIT_FUNC init##name(void) +#endif + +#if PY_MAJOR_VERSION >= 3 +# define PyMOD_Return(name) return name +#else +# define PyMOD_Return(name) return +#endif + /** * Union to convert from PyTypeObject to PyObject pointer. */ diff --git a/src/Mod/Complete/App/AppComplete.cpp b/src/Mod/Complete/App/AppComplete.cpp index 5c202fc90..4d5a45e6e 100644 --- a/src/Mod/Complete/App/AppComplete.cpp +++ b/src/Mod/Complete/App/AppComplete.cpp @@ -30,6 +30,7 @@ #include #include +#include #include #include "CompleteConfiguration.h" @@ -57,7 +58,7 @@ PyObject* initModule() /* Python entry */ -PyMODINIT_FUNC initComplete() +PyMOD_INIT_FUNC(Complete) { // load dependent module try { @@ -86,8 +87,9 @@ PyMODINIT_FUNC initComplete() } catch(const Base::Exception& e) { PyErr_SetString(PyExc_ImportError, e.what()); - return; + PyMOD_Return(0); } - (void)Complete::initModule(); + PyObject* mod = Complete::initModule(); Base::Console().Log("Loading Complete module... done\n"); + PyMOD_Return(mod); } diff --git a/src/Mod/Complete/Gui/AppCompleteGui.cpp b/src/Mod/Complete/Gui/AppCompleteGui.cpp index 6709afeaa..09505603f 100644 --- a/src/Mod/Complete/Gui/AppCompleteGui.cpp +++ b/src/Mod/Complete/Gui/AppCompleteGui.cpp @@ -74,32 +74,32 @@ PyObject* initModule() /* Python entry */ -PyMODINIT_FUNC initCompleteGui() +PyMOD_INIT_FUNC(CompleteGui) { if (!Gui::Application::Instance) { PyErr_SetString(PyExc_ImportError, "Cannot load Gui module in console application."); - return; + PyMOD_Return(0); } - // try to load dependent modules, currently not (AssemblyGui, CamGui) - char *modules[] = {"PartGui", "MeshGui", "MeshPartGui", "PointsGui", "DrawingGui", "RaytracingGui", "SketcherGui", "PartDesignGui", "ImageGui", "TestGui"}; - size_t nModules = sizeof(modules) / sizeof(char*); - for (size_t i = 0; i < nModules; i++) { - try { - Base::Interpreter().loadModule(modules[i]); - } - catch (const Base::Exception& e) { - Base::Console().Error("%s\n", e.what()); - // Prints message to console window if we are in interactive mode - PyErr_Print(); - continue; - } - mods.append(QSTRING(modules[i])); - } - -# ifdef COMPLETE_USE_DRAFTING - mods.append(QSTRING("DraftGui")); - try { + // try to load dependent modules, currently not (AssemblyGui, CamGui) + char *modules[] = {"PartGui", "MeshGui", "MeshPartGui", "PointsGui", "DrawingGui", "RaytracingGui", "SketcherGui", "PartDesignGui", "ImageGui", "TestGui"}; + size_t nModules = sizeof(modules) / sizeof(char*); + for (size_t i = 0; i < nModules; i++) { + try { + Base::Interpreter().loadModule(modules[i]); + } + catch (const Base::Exception& e) { + Base::Console().Error("%s\n", e.what()); + // Prints message to console window if we are in interactive mode + PyErr_Print(); + continue; + } + mods.append(QSTRING(modules[i])); + } + +# ifdef COMPLETE_USE_DRAFTING + mods.append(QSTRING("DraftGui")); + try { Py::Module module(PyImport_ImportModule("FreeCADGui"),true); Py::Callable method(module.getAttr(std::string("getWorkbench"))); @@ -128,15 +128,15 @@ PyMODINIT_FUNC initCompleteGui() // Get the CompleteWorkbench handler args.setItem(0,Py::String("CompleteWorkbench")); } - catch (const Base::Exception& e) { - Base::Console().Error("%s\n", e.what()); - mods.removeAt(mods.size()); - // Prints message to console window if we are in interactive mode - PyErr_Print(); - } -# endif + catch (const Base::Exception& e) { + Base::Console().Error("%s\n", e.what()); + mods.removeAt(mods.size()); + // Prints message to console window if we are in interactive mode + PyErr_Print(); + } +# endif - (void) CompleteGui::initModule(); + PyObject* mod = CompleteGui::initModule(); Base::Console().Log("Loading GUI of Complete module... done\n"); // instantiating the commands @@ -145,4 +145,6 @@ PyMODINIT_FUNC initCompleteGui() // add resources and reloads the translators loadCompleteResource(); + + PyMOD_Return(mod); }