aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/kiwisolver/py3/py/term.cpp
blob: 35b0a0e0bc1fc55da63f2d845a01c2c5260fca53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*-----------------------------------------------------------------------------
| Copyright (c) 2013-2019, Nucleic Development Team.
|
| Distributed under the terms of the Modified BSD License.
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
#include <sstream>
#include <cppy/cppy.h>
#include "symbolics.h"
#include "types.h"
#include "util.h"


namespace kiwisolver
{


namespace
{


PyObject*
Term_new( PyTypeObject* type, PyObject* args, PyObject* kwargs )
{
	static const char *kwlist[] = { "variable", "coefficient", 0 };
	PyObject* pyvar;
	PyObject* pycoeff = 0;
	if( !PyArg_ParseTupleAndKeywords(
		args, kwargs, "O|O:__new__", const_cast<char**>( kwlist ),
		&pyvar, &pycoeff ) )
		return 0;
	if( !Variable::TypeCheck( pyvar ) )
		return cppy::type_error( pyvar, "Variable" );
	double coefficient = 1.0;
	if( pycoeff && !convert_to_double( pycoeff, coefficient ) )
		return 0;
	PyObject* pyterm = PyType_GenericNew( type, args, kwargs );
	if( !pyterm )
		return 0;
	Term* self = reinterpret_cast<Term*>( pyterm );
	self->variable = cppy::incref( pyvar );
	self->coefficient = coefficient;
	return pyterm;
}


void
Term_clear( Term* self )
{
	Py_CLEAR( self->variable );
}


int
Term_traverse( Term* self, visitproc visit, void* arg )
{
	Py_VISIT( self->variable );
#if PY_VERSION_HEX >= 0x03090000
    // This was not needed before Python 3.9 (Python issue 35810 and 40217)
    Py_VISIT(Py_TYPE(self));
#endif
	return 0;
}


void
Term_dealloc( Term* self )
{
	PyObject_GC_UnTrack( self );
	Term_clear( self );
	Py_TYPE( self )->tp_free( pyobject_cast( self ) );
}


PyObject*
Term_repr( Term* self )
{
	std::stringstream stream;
	stream << self->coefficient << " * ";
	stream << reinterpret_cast<Variable*>( self->variable )->variable.name();
	return PyUnicode_FromString( stream.str().c_str() );
}


PyObject*
Term_variable( Term* self )
{
	return cppy::incref( self->variable );
}


PyObject*
Term_coefficient( Term* self )
{
	return PyFloat_FromDouble( self->coefficient );
}


PyObject*
Term_value( Term* self )
{
	Variable* pyvar = reinterpret_cast<Variable*>( self->variable );
	return PyFloat_FromDouble( self->coefficient * pyvar->variable.value() );
}


PyObject*
Term_add( PyObject* first, PyObject* second )
{
	return BinaryInvoke<BinaryAdd, Term>()( first, second );
}


PyObject*
Term_sub( PyObject* first, PyObject* second )
{
	return BinaryInvoke<BinarySub, Term>()( first, second );
}


PyObject*
Term_mul( PyObject* first, PyObject* second )
{
	return BinaryInvoke<BinaryMul, Term>()( first, second );
}


PyObject*
Term_div( PyObject* first, PyObject* second )
{
	return BinaryInvoke<BinaryDiv, Term>()( first, second );
}


PyObject*
Term_neg( PyObject* value )
{
	return UnaryInvoke<UnaryNeg, Term>()( value );
}


PyObject*
Term_richcmp( PyObject* first, PyObject* second, int op )
{
	switch( op )
	{
		case Py_EQ:
			return BinaryInvoke<CmpEQ, Term>()( first, second );
		case Py_LE:
			return BinaryInvoke<CmpLE, Term>()( first, second );
		case Py_GE:
			return BinaryInvoke<CmpGE, Term>()( first, second );
		default:
			break;
	}
	PyErr_Format(
		PyExc_TypeError,
		"unsupported operand type(s) for %s: "
		"'%.100s' and '%.100s'",
		pyop_str( op ),
		Py_TYPE( first )->tp_name,
		Py_TYPE( second )->tp_name
	);
	return 0;
}


static PyMethodDef
Term_methods[] = {
	{ "variable", ( PyCFunction )Term_variable, METH_NOARGS,
	  "Get the variable for the term." },
	{ "coefficient", ( PyCFunction )Term_coefficient, METH_NOARGS,
	  "Get the coefficient for the term." },
	{ "value", ( PyCFunction )Term_value, METH_NOARGS,
	  "Get the value for the term." },
	{ 0 } // sentinel
};


static PyType_Slot Term_Type_slots[] = {
    { Py_tp_dealloc, void_cast( Term_dealloc ) },      /* tp_dealloc */
    { Py_tp_traverse, void_cast( Term_traverse ) },    /* tp_traverse */
    { Py_tp_clear, void_cast( Term_clear ) },          /* tp_clear */
    { Py_tp_repr, void_cast( Term_repr ) },            /* tp_repr */
    { Py_tp_richcompare, void_cast( Term_richcmp ) },  /* tp_richcompare */
    { Py_tp_methods, void_cast( Term_methods ) },      /* tp_methods */
    { Py_tp_new, void_cast( Term_new ) },              /* tp_new */
    { Py_tp_alloc, void_cast( PyType_GenericAlloc ) }, /* tp_alloc */
    { Py_tp_free, void_cast( PyObject_GC_Del ) },      /* tp_free */
    { Py_nb_add, void_cast( Term_add ) },              /* nb_add */
    { Py_nb_subtract, void_cast( Term_sub ) },         /* nb_subatract */
    { Py_nb_multiply, void_cast( Term_mul ) },         /* nb_multiply */
    { Py_nb_negative, void_cast( Term_neg ) },         /* nb_negative */
    { Py_nb_true_divide, void_cast( Term_div ) },      /* nb_true_divide */
    { 0, 0 },
};


} // namespace


// Initialize static variables (otherwise the compiler eliminates them)
PyTypeObject* Term::TypeObject = NULL;


PyType_Spec Term::TypeObject_Spec = {
	"kiwisolver.Term",             /* tp_name */
	sizeof( Term ),                /* tp_basicsize */
	0,                                   /* tp_itemsize */
	Py_TPFLAGS_DEFAULT|
    Py_TPFLAGS_HAVE_GC|
    Py_TPFLAGS_BASETYPE,                 /* tp_flags */
    Term_Type_slots                /* slots */
};


bool Term::Ready()
{
    // The reference will be handled by the module to which we will add the type
	TypeObject = pytype_cast( PyType_FromSpec( &TypeObject_Spec ) );
    if( !TypeObject )
    {
        return false;
    }
    return true;
}

}  // namespace kiwisolver