ctkMacroWrapPythonQtModuleInit.cpp.in 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Configured by cmake macro @CMAKE_CURRENT_LIST_FILENAME@
  2. #include <PythonQt.h>
  3. #include <Python.h>
  4. //-----------------------------------------------------------------------------
  5. static PyMethodDef Py@TARGET@PythonQt_ClassMethods[] = {
  6. {NULL, NULL, 0, NULL}};
  7. //-----------------------------------------------------------------------------
  8. extern "C" { void Q_DECL_EXPORT init@TARGET@PythonQt(); }
  9. #ifdef __GNUC__
  10. // Disable warnings related to Py_DECREF() macro
  11. // See http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html
  12. // Note: Ideally the incriminated functions and macros should be fixed upstream ...
  13. #pragma GCC diagnostic ignored "-Wold-style-cast"
  14. #endif
  15. #cmakedefine HAS_DECORATOR
  16. namespace {
  17. //-----------------------------------------------------------------------------
  18. void copyAttributes(PyObject* orig_module, PyObject* dest_module)
  19. {
  20. PyObject* keys = PyObject_Dir(orig_module);
  21. if (keys)
  22. {
  23. PyObject* key;
  24. PyObject* value;
  25. int nKeys = PyList_Size(keys);
  26. for (int i = 0; i < nKeys; ++i)
  27. {
  28. key = PyList_GetItem(keys, i);
  29. value = PyObject_GetAttr(orig_module, key);
  30. if (!value)
  31. {
  32. continue;
  33. }
  34. //printf("%s\n", PyString_AsString(key));
  35. if (!PyObject_HasAttr(dest_module, key))
  36. {
  37. PyObject_SetAttr(dest_module, key, value);
  38. }
  39. Py_DECREF((void*)value);
  40. }
  41. Py_DECREF(keys);
  42. }
  43. }
  44. } // end of anonymous namespace
  45. //-----------------------------------------------------------------------------
  46. void init@TARGET@PythonQt()
  47. {
  48. static const char modulename[] = "@TARGET@PythonQt";
  49. PyObject *m;
  50. m = Py_InitModule((char*)modulename, Py@TARGET@PythonQt_ClassMethods);
  51. extern void PythonQt_init_@WRAPPING_NAMESPACE_UNDERSCORE@_@TARGET@(PyObject*);
  52. PythonQt_init_@WRAPPING_NAMESPACE_UNDERSCORE@_@TARGET@(m);
  53. #ifdef HAS_DECORATOR
  54. extern void init@TARGET@PythonQtDecorators();
  55. init@TARGET@PythonQtDecorators();
  56. #endif
  57. // Since invoking 'PythonQt_init_@WRAPPING_NAMESPACE_UNDERSCORE@_@TARGET@', will create
  58. // the module "PythonQt.@TARGET@". Let's copy its content into the current module.
  59. PythonQtObjectPtr currentModule = PyImport_ImportModule((char*)"PythonQt.@TARGET@");
  60. if(currentModule.isNull())
  61. {
  62. PyErr_SetString(PyExc_ImportError, (char*)"Failed to import PythonQt.@TARGET@");
  63. return;
  64. }
  65. copyAttributes(currentModule, m);
  66. }