ctkMacroWrapPythonQtModuleInit.cpp.in 2.0 KB

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