kiwisolver.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) 2013-2019, Nucleic Development Team.
  3. |
  4. | Distributed under the terms of the Modified BSD License.
  5. |
  6. | The full license is in the file LICENSE, distributed with this software.
  7. |----------------------------------------------------------------------------*/
  8. #include <cppy/cppy.h>
  9. #include <kiwi/kiwi.h>
  10. #include "types.h"
  11. #define PY_KIWI_VERSION "1.3.2"
  12. namespace
  13. {
  14. bool ready_types()
  15. {
  16. using namespace kiwisolver;
  17. if( !Variable::Ready() )
  18. {
  19. return false;
  20. }
  21. if( !Term::Ready() )
  22. {
  23. return false;
  24. }
  25. if( !Expression::Ready() )
  26. {
  27. return false;
  28. }
  29. if( !Constraint::Ready() )
  30. {
  31. return false;
  32. }
  33. if( !strength::Ready() )
  34. {
  35. return false;
  36. }
  37. if( !Solver::Ready() )
  38. {
  39. return false;
  40. }
  41. return true;
  42. }
  43. bool add_objects( PyObject* mod )
  44. {
  45. using namespace kiwisolver;
  46. cppy::ptr kiwiversion( PyUnicode_FromString( KIWI_VERSION ) );
  47. if( !kiwiversion )
  48. {
  49. return false;
  50. }
  51. cppy::ptr pyversion( PyUnicode_FromString( PY_KIWI_VERSION ) );
  52. if( !pyversion )
  53. {
  54. return false;
  55. }
  56. cppy::ptr pystrength( PyType_GenericNew( strength::TypeObject, 0, 0 ) );
  57. if( !pystrength )
  58. {
  59. return false;
  60. }
  61. if( PyModule_AddObject( mod, "__version__", pyversion.get() ) < 0 )
  62. {
  63. return false;
  64. }
  65. pyversion.release();
  66. if( PyModule_AddObject( mod, "__kiwi_version__", kiwiversion.get() ) < 0 )
  67. {
  68. return false;
  69. }
  70. kiwiversion.release();
  71. if( PyModule_AddObject( mod, "strength", pystrength.get() ) < 0 )
  72. {
  73. return false;
  74. }
  75. pystrength.release();
  76. // Variable
  77. cppy::ptr var( pyobject_cast( Variable::TypeObject ) );
  78. if( PyModule_AddObject( mod, "Variable", var.get() ) < 0 )
  79. {
  80. return false;
  81. }
  82. var.release();
  83. // Term
  84. cppy::ptr term( pyobject_cast( Term::TypeObject ) );
  85. if( PyModule_AddObject( mod, "Term", term.get() ) < 0 )
  86. {
  87. return false;
  88. }
  89. term.release();
  90. // Expression
  91. cppy::ptr expr( pyobject_cast( Expression::TypeObject ) );
  92. if( PyModule_AddObject( mod, "Expression", expr.get() ) < 0 )
  93. {
  94. return false;
  95. }
  96. expr.release();
  97. // Constraint
  98. cppy::ptr cons( pyobject_cast( Constraint::TypeObject ) );
  99. if( PyModule_AddObject( mod, "Constraint", cons.get() ) < 0 )
  100. {
  101. return false;
  102. }
  103. cons.release();
  104. cppy::ptr solver( pyobject_cast( Solver::TypeObject ) );
  105. if( PyModule_AddObject( mod, "Solver", solver.get() ) < 0 )
  106. {
  107. return false;
  108. }
  109. solver.release();
  110. PyModule_AddObject( mod, "DuplicateConstraint", DuplicateConstraint );
  111. PyModule_AddObject( mod, "UnsatisfiableConstraint", UnsatisfiableConstraint );
  112. PyModule_AddObject( mod, "UnknownConstraint", UnknownConstraint );
  113. PyModule_AddObject( mod, "DuplicateEditVariable", DuplicateEditVariable );
  114. PyModule_AddObject( mod, "UnknownEditVariable", UnknownEditVariable );
  115. PyModule_AddObject( mod, "BadRequiredStrength", BadRequiredStrength );
  116. return true;
  117. }
  118. int
  119. catom_modexec( PyObject *mod )
  120. {
  121. if( !ready_types() )
  122. {
  123. return -1;
  124. }
  125. if( !kiwisolver::init_exceptions() )
  126. {
  127. return -1;
  128. }
  129. if( !add_objects( mod ) )
  130. {
  131. return -1;
  132. }
  133. return 0;
  134. }
  135. static PyMethodDef
  136. kiwisolver_methods[] = {
  137. { 0 } // Sentinel
  138. };
  139. PyModuleDef_Slot kiwisolver_slots[] = {
  140. {Py_mod_exec, reinterpret_cast<void*>( catom_modexec ) },
  141. {0, NULL}
  142. };
  143. struct PyModuleDef moduledef = {
  144. PyModuleDef_HEAD_INIT,
  145. "kiwisolver",
  146. "kiwisolver extension module",
  147. 0,
  148. kiwisolver_methods,
  149. kiwisolver_slots,
  150. NULL,
  151. NULL,
  152. NULL
  153. };
  154. } // namespace
  155. PyMODINIT_FUNC PyInit_kiwisolver( void )
  156. {
  157. return PyModuleDef_Init( &moduledef );
  158. }