variable.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 "symbolics.h"
  11. #include "types.h"
  12. #include "util.h"
  13. namespace kiwisolver
  14. {
  15. namespace
  16. {
  17. PyObject*
  18. Variable_new( PyTypeObject* type, PyObject* args, PyObject* kwargs )
  19. {
  20. static const char *kwlist[] = { "name", "context", 0 };
  21. PyObject* context = 0;
  22. PyObject* name = 0;
  23. if( !PyArg_ParseTupleAndKeywords(
  24. args, kwargs, "|OO:__new__", const_cast<char**>( kwlist ),
  25. &name, &context ) )
  26. return 0;
  27. cppy::ptr pyvar( PyType_GenericNew( type, args, kwargs ) );
  28. if( !pyvar )
  29. return 0;
  30. Variable* self = reinterpret_cast<Variable*>( pyvar.get() );
  31. self->context = cppy::xincref( context );
  32. if( name != 0 )
  33. {
  34. if( !PyUnicode_Check( name ) )
  35. return cppy::type_error( name, "str" );
  36. std::string c_name;
  37. if( !convert_pystr_to_str(name, c_name) )
  38. return 0; // LCOV_EXCL_LINE
  39. new( &self->variable ) kiwi::Variable( c_name );
  40. }
  41. else
  42. {
  43. new( &self->variable ) kiwi::Variable();
  44. }
  45. return pyvar.release();
  46. }
  47. void
  48. Variable_clear( Variable* self )
  49. {
  50. Py_CLEAR( self->context );
  51. }
  52. int
  53. Variable_traverse( Variable* self, visitproc visit, void* arg )
  54. {
  55. Py_VISIT( self->context );
  56. #if PY_VERSION_HEX >= 0x03090000
  57. // This was not needed before Python 3.9 (Python issue 35810 and 40217)
  58. Py_VISIT(Py_TYPE(self));
  59. #endif
  60. return 0;
  61. }
  62. void
  63. Variable_dealloc( Variable* self )
  64. {
  65. PyObject_GC_UnTrack( self );
  66. Variable_clear( self );
  67. self->variable.~Variable();
  68. Py_TYPE( self )->tp_free( pyobject_cast( self ) );
  69. }
  70. PyObject*
  71. Variable_repr( Variable* self )
  72. {
  73. return PyUnicode_FromString( self->variable.name().c_str() );
  74. }
  75. PyObject*
  76. Variable_name( Variable* self )
  77. {
  78. return PyUnicode_FromString( self->variable.name().c_str() );
  79. }
  80. PyObject*
  81. Variable_setName( Variable* self, PyObject* pystr )
  82. {
  83. if( !PyUnicode_Check( pystr ) )
  84. return cppy::type_error( pystr, "str" );
  85. std::string str;
  86. if( !convert_pystr_to_str( pystr, str ) )
  87. return 0;
  88. self->variable.setName( str );
  89. Py_RETURN_NONE;
  90. }
  91. PyObject*
  92. Variable_context( Variable* self )
  93. {
  94. if( self->context )
  95. return cppy::incref( self->context );
  96. Py_RETURN_NONE;
  97. }
  98. PyObject*
  99. Variable_setContext( Variable* self, PyObject* value )
  100. {
  101. if( value != self->context )
  102. {
  103. PyObject* temp = self->context;
  104. self->context = cppy::incref( value );
  105. Py_XDECREF( temp );
  106. }
  107. Py_RETURN_NONE;
  108. }
  109. PyObject*
  110. Variable_value( Variable* self )
  111. {
  112. return PyFloat_FromDouble( self->variable.value() );
  113. }
  114. PyObject*
  115. Variable_add( PyObject* first, PyObject* second )
  116. {
  117. return BinaryInvoke<BinaryAdd, Variable>()( first, second );
  118. }
  119. PyObject*
  120. Variable_sub( PyObject* first, PyObject* second )
  121. {
  122. return BinaryInvoke<BinarySub, Variable>()( first, second );
  123. }
  124. PyObject*
  125. Variable_mul( PyObject* first, PyObject* second )
  126. {
  127. return BinaryInvoke<BinaryMul, Variable>()( first, second );
  128. }
  129. PyObject*
  130. Variable_div( PyObject* first, PyObject* second )
  131. {
  132. return BinaryInvoke<BinaryDiv, Variable>()( first, second );
  133. }
  134. PyObject*
  135. Variable_neg( PyObject* value )
  136. {
  137. return UnaryInvoke<UnaryNeg, Variable>()( value );
  138. }
  139. PyObject*
  140. Variable_richcmp( PyObject* first, PyObject* second, int op )
  141. {
  142. switch( op )
  143. {
  144. case Py_EQ:
  145. return BinaryInvoke<CmpEQ, Variable>()( first, second );
  146. case Py_LE:
  147. return BinaryInvoke<CmpLE, Variable>()( first, second );
  148. case Py_GE:
  149. return BinaryInvoke<CmpGE, Variable>()( first, second );
  150. default:
  151. break;
  152. }
  153. PyErr_Format(
  154. PyExc_TypeError,
  155. "unsupported operand type(s) for %s: "
  156. "'%.100s' and '%.100s'",
  157. pyop_str( op ),
  158. Py_TYPE( first )->tp_name,
  159. Py_TYPE( second )->tp_name
  160. );
  161. return 0;
  162. }
  163. static PyMethodDef
  164. Variable_methods[] = {
  165. { "name", ( PyCFunction )Variable_name, METH_NOARGS,
  166. "Get the name of the variable." },
  167. { "setName", ( PyCFunction )Variable_setName, METH_O,
  168. "Set the name of the variable." },
  169. { "context", ( PyCFunction )Variable_context, METH_NOARGS,
  170. "Get the context object associated with the variable." },
  171. { "setContext", ( PyCFunction )Variable_setContext, METH_O,
  172. "Set the context object associated with the variable." },
  173. { "value", ( PyCFunction )Variable_value, METH_NOARGS,
  174. "Get the current value of the variable." },
  175. { 0 } // sentinel
  176. };
  177. static PyType_Slot Variable_Type_slots[] = {
  178. { Py_tp_dealloc, void_cast( Variable_dealloc ) }, /* tp_dealloc */
  179. { Py_tp_traverse, void_cast( Variable_traverse ) }, /* tp_traverse */
  180. { Py_tp_clear, void_cast( Variable_clear ) }, /* tp_clear */
  181. { Py_tp_repr, void_cast( Variable_repr ) }, /* tp_repr */
  182. { Py_tp_richcompare, void_cast( Variable_richcmp ) }, /* tp_richcompare */
  183. { Py_tp_methods, void_cast( Variable_methods ) }, /* tp_methods */
  184. { Py_tp_new, void_cast( Variable_new ) }, /* tp_new */
  185. { Py_tp_alloc, void_cast( PyType_GenericAlloc ) }, /* tp_alloc */
  186. { Py_tp_free, void_cast( PyObject_GC_Del ) }, /* tp_free */
  187. { Py_nb_add, void_cast( Variable_add ) }, /* nb_add */
  188. { Py_nb_subtract, void_cast( Variable_sub ) }, /* nb_subtract */
  189. { Py_nb_multiply, void_cast( Variable_mul ) }, /* nb_multiply */
  190. { Py_nb_negative, void_cast( Variable_neg ) }, /* nb_negative */
  191. { Py_nb_true_divide, void_cast( Variable_div ) }, /* nb_true_divide */
  192. { 0, 0 },
  193. };
  194. } // namespace
  195. // Initialize static variables (otherwise the compiler eliminates them)
  196. PyTypeObject* Variable::TypeObject = NULL;
  197. PyType_Spec Variable::TypeObject_Spec = {
  198. "kiwisolver.Variable", /* tp_name */
  199. sizeof( Variable ), /* tp_basicsize */
  200. 0, /* tp_itemsize */
  201. Py_TPFLAGS_DEFAULT|
  202. Py_TPFLAGS_HAVE_GC|
  203. Py_TPFLAGS_BASETYPE, /* tp_flags */
  204. Variable_Type_slots /* slots */
  205. };
  206. bool Variable::Ready()
  207. {
  208. // The reference will be handled by the module to which we will add the type
  209. TypeObject = pytype_cast( PyType_FromSpec( &TypeObject_Spec ) );
  210. if( !TypeObject )
  211. {
  212. return false;
  213. }
  214. return true;
  215. }
  216. } // namespace kiwisolver