ctkMacroWrapPythonQtModuleInit.cpp.in 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Configured by cmake macro @CMAKE_CURRENT_LIST_FILE@
  2. #include <PythonQt.h>
  3. #include <Python.h>
  4. //-----------------------------------------------------------------------------
  5. static PyMethodDef Py@TARGET_CONFIG@PythonQt_ClassMethods[] = {
  6. {NULL, NULL, 0, NULL}};
  7. //-----------------------------------------------------------------------------
  8. extern "C" { void Q_DECL_EXPORT init@TARGET_CONFIG@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. #if PY_MAJOR_VERSION >= 3
  47. static struct PyModuleDef moduledef = {
  48. PyModuleDef_HEAD_INIT,
  49. "@TARGET_CONFIG@PythonQt", /* m_name */
  50. "The @TARGET_CONFIG@PythonQt module", /* m_doc */
  51. -1, /* m_size */
  52. Py@TARGET_CONFIG@PythonQt_ClassMethods, /* m_methods */
  53. NULL, /* m_reload */
  54. NULL, /* m_traverse */
  55. NULL, /* m_clear */
  56. NULL, /* m_free */
  57. };
  58. #endif
  59. //-----------------------------------------------------------------------------
  60. void init@TARGET_CONFIG@PythonQt()
  61. {
  62. static const char modulename[] = "@TARGET_CONFIG@PythonQt";
  63. PyObject *m;
  64. #if PY_MAJOR_VERSION >= 3
  65. m = PyModule_Create(&moduledef);
  66. #else
  67. m = Py_InitModule((char*)modulename, Py@TARGET_CONFIG@PythonQt_ClassMethods);
  68. #endif
  69. extern void PythonQt_init_@WRAPPING_NAMESPACE_UNDERSCORE@_@TARGET_CONFIG@(PyObject*);
  70. PythonQt_init_@WRAPPING_NAMESPACE_UNDERSCORE@_@TARGET_CONFIG@(m);
  71. #ifdef HAS_DECORATOR
  72. extern void init@TARGET_CONFIG@PythonQtDecorators();
  73. init@TARGET_CONFIG@PythonQtDecorators();
  74. #endif
  75. // Since invoking 'PythonQt_init_@WRAPPING_NAMESPACE_UNDERSCORE@_@TARGET_CONFIG@', will create
  76. // the module "PythonQt.@TARGET_CONFIG@". Let's copy its content into the current module.
  77. PythonQtObjectPtr currentModule = PyImport_ImportModule((char*)"PythonQt.@TARGET_CONFIG@");
  78. if(currentModule.isNull())
  79. {
  80. PyErr_SetString(PyExc_ImportError, (char*)"Failed to import PythonQt.@TARGET_CONFIG@");
  81. return;
  82. }
  83. copyAttributes(currentModule, m);
  84. }