| 1 |
/*----------------------------------------------------------------------------- |
| 2 |
| Copyright (c) 2013-2026, 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 |
|
| 14 |
|
| 15 |
namespace kiwisolver |
| 16 |
{ |
| 17 |
|
| 18 |
|
| 19 |
namespace |
| 20 |
{ |
| 21 |
|
| 22 |
|
| 23 |
PyObject* |
| 24 |
Variable_new( PyTypeObject* type, PyObject* args, PyObject* kwargs ) |
| 25 |
{ |
| 26 |
static const char *kwlist[] = { "name", "context", 0 }; |
| 27 |
PyObject* context = 0; |
| 28 |
PyObject* name = 0; |
| 29 |
|
| 30 |
if( !PyArg_ParseTupleAndKeywords( |
| 31 |
args, kwargs, "|OO:__new__", const_cast<char**>( kwlist ), |
| 32 |
&name, &context ) ) |
| 33 |
return 0; |
| 34 |
|
| 35 |
cppy::ptr pyvar( PyType_GenericNew( type, args, kwargs ) ); |
| 36 |
if( !pyvar ) |
| 37 |
return 0; |
| 38 |
|
| 39 |
Variable* self = reinterpret_cast<Variable*>( pyvar.get() ); |
| 40 |
self->context = cppy::xincref( context ); |
| 41 |
|
| 42 |
if( name != 0 ) |
| 43 |
{ |
| 44 |
if( !PyUnicode_Check( name ) ) |
| 45 |
return cppy::type_error( name, "str" ); |
| 46 |
std::string c_name; |
| 47 |
if( !convert_pystr_to_str(name, c_name) ) |
| 48 |
return 0; // LCOV_EXCL_LINE |
| 49 |
ACQUIRE_GLOBAL_LOCK(); |
| 50 |
new( &self->variable ) kiwi::Variable( c_name ); |
| 51 |
RELEASE_GLOBAL_LOCK(); |
| 52 |
} |
| 53 |
else |
| 54 |
{ |
| 55 |
ACQUIRE_GLOBAL_LOCK(); |
| 56 |
new( &self->variable ) kiwi::Variable(); |
| 57 |
RELEASE_GLOBAL_LOCK(); |
| 58 |
} |
| 59 |
|
| 60 |
return pyvar.release(); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
void |
| 65 |
Variable_clear( Variable* self ) |
| 66 |
{ |
| 67 |
Py_CLEAR( self->context ); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
int |
| 72 |
Variable_traverse( Variable* self, visitproc visit, void* arg ) |
| 73 |
{ |
| 74 |
Py_VISIT( self->context ); |
| 75 |
#if PY_VERSION_HEX >= 0x03090000 |
| 76 |
// This was not needed before Python 3.9 (Python issue 35810 and 40217) |
| 77 |
Py_VISIT(Py_TYPE(self)); |
| 78 |
#endif |
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
void |
| 84 |
Variable_dealloc( Variable* self ) |
| 85 |
{ |
| 86 |
PyObject_GC_UnTrack( self ); |
| 87 |
Variable_clear( self ); |
| 88 |
ACQUIRE_GLOBAL_LOCK(); |
| 89 |
self->variable.~Variable(); |
| 90 |
RELEASE_GLOBAL_LOCK(); |
| 91 |
Py_TYPE( self )->tp_free( pyobject_cast( self ) ); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
PyObject* |
| 96 |
Variable_repr( Variable* self ) |
| 97 |
{ |
| 98 |
ACQUIRE_GLOBAL_LOCK(); |
| 99 |
std::string name = self->variable.name(); |
| 100 |
RELEASE_GLOBAL_LOCK(); |
| 101 |
return PyUnicode_FromString( name.c_str() ); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
PyObject* |
| 106 |
Variable_name( Variable* self ) |
| 107 |
{ |
| 108 |
ACQUIRE_GLOBAL_LOCK(); |
| 109 |
std::string name = self->variable.name(); |
| 110 |
RELEASE_GLOBAL_LOCK(); |
| 111 |
return PyUnicode_FromString( name.c_str() ); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
PyObject* |
| 116 |
Variable_setName( Variable* self, PyObject* pystr ) |
| 117 |
{ |
| 118 |
if( !PyUnicode_Check( pystr ) ) |
| 119 |
return cppy::type_error( pystr, "str" ); |
| 120 |
std::string str; |
| 121 |
if( !convert_pystr_to_str( pystr, str ) ) |
| 122 |
return 0; |
| 123 |
|
| 124 |
ACQUIRE_GLOBAL_LOCK(); |
| 125 |
self->variable.setName( str ); |
| 126 |
RELEASE_GLOBAL_LOCK(); |
| 127 |
|
| 128 |
Py_RETURN_NONE; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
PyObject* |
| 133 |
Variable_context_locked( Variable* self ) |
| 134 |
{ |
| 135 |
if( self->context ) |
| 136 |
return cppy::incref( self->context ); |
| 137 |
Py_RETURN_NONE; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
PyObject* |
| 142 |
Variable_context( Variable* self ) |
| 143 |
{ |
| 144 |
PyObject* context; |
| 145 |
Py_BEGIN_CRITICAL_SECTION(self); |
| 146 |
context = Variable_context_locked(self); |
| 147 |
Py_END_CRITICAL_SECTION(); |
| 148 |
return context; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
void |
| 153 |
Variable_setContext_locked( Variable* self, PyObject* value ) |
| 154 |
{ |
| 155 |
if( value != self->context ) |
| 156 |
{ |
| 157 |
Py_XSETREF(self->context, cppy::incref( value )); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
PyObject* |
| 163 |
Variable_setContext( Variable* self, PyObject* value ) |
| 164 |
{ |
| 165 |
Py_BEGIN_CRITICAL_SECTION(self); |
| 166 |
Variable_setContext_locked(self, value); |
| 167 |
Py_END_CRITICAL_SECTION(); |
| 168 |
Py_RETURN_NONE; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
PyObject* |
| 173 |
Variable_value( Variable* self ) |
| 174 |
{ |
| 175 |
ACQUIRE_GLOBAL_LOCK(); |
| 176 |
double value = self->variable.value(); |
| 177 |
RELEASE_GLOBAL_LOCK(); |
| 178 |
return PyFloat_FromDouble( value ); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
PyObject* |
| 183 |
Variable_add( PyObject* first, PyObject* second ) |
| 184 |
{ |
| 185 |
return BinaryInvoke<BinaryAdd, Variable>()( first, second ); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
PyObject* |
| 190 |
Variable_sub( PyObject* first, PyObject* second ) |
| 191 |
{ |
| 192 |
return BinaryInvoke<BinarySub, Variable>()( first, second ); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
PyObject* |
| 197 |
Variable_mul( PyObject* first, PyObject* second ) |
| 198 |
{ |
| 199 |
return BinaryInvoke<BinaryMul, Variable>()( first, second ); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
PyObject* |
| 204 |
Variable_div( PyObject* first, PyObject* second ) |
| 205 |
{ |
| 206 |
return BinaryInvoke<BinaryDiv, Variable>()( first, second ); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
PyObject* |
| 211 |
Variable_neg( PyObject* value ) |
| 212 |
{ |
| 213 |
return UnaryInvoke<UnaryNeg, Variable>()( value ); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
PyObject* |
| 218 |
Variable_richcmp( PyObject* first, PyObject* second, int op ) |
| 219 |
{ |
| 220 |
switch( op ) |
| 221 |
{ |
| 222 |
case Py_EQ: |
| 223 |
return BinaryInvoke<CmpEQ, Variable>()( first, second ); |
| 224 |
case Py_LE: |
| 225 |
return BinaryInvoke<CmpLE, Variable>()( first, second ); |
| 226 |
case Py_GE: |
| 227 |
return BinaryInvoke<CmpGE, Variable>()( first, second ); |
| 228 |
default: |
| 229 |
break; |
| 230 |
} |
| 231 |
PyErr_Format( |
| 232 |
PyExc_TypeError, |
| 233 |
"unsupported operand type(s) for %s: " |
| 234 |
"'%.100s' and '%.100s'", |
| 235 |
pyop_str( op ), |
| 236 |
Py_TYPE( first )->tp_name, |
| 237 |
Py_TYPE( second )->tp_name |
| 238 |
); |
| 239 |
return 0; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
static PyMethodDef |
| 244 |
Variable_methods[] = { |
| 245 |
{ "name", ( PyCFunction )Variable_name, METH_NOARGS, |
| 246 |
"Get the name of the variable." }, |
| 247 |
{ "setName", ( PyCFunction )Variable_setName, METH_O, |
| 248 |
"Set the name of the variable." }, |
| 249 |
{ "context", ( PyCFunction )Variable_context, METH_NOARGS, |
| 250 |
"Get the context object associated with the variable." }, |
| 251 |
{ "setContext", ( PyCFunction )Variable_setContext, METH_O, |
| 252 |
"Set the context object associated with the variable." }, |
| 253 |
{ "value", ( PyCFunction )Variable_value, METH_NOARGS, |
| 254 |
"Get the current value of the variable." }, |
| 255 |
{ 0 } // sentinel |
| 256 |
}; |
| 257 |
|
| 258 |
|
| 259 |
static PyType_Slot Variable_Type_slots[] = { |
| 260 |
{ Py_tp_dealloc, void_cast( Variable_dealloc ) }, /* tp_dealloc */ |
| 261 |
{ Py_tp_traverse, void_cast( Variable_traverse ) }, /* tp_traverse */ |
| 262 |
{ Py_tp_clear, void_cast( Variable_clear ) }, /* tp_clear */ |
| 263 |
{ Py_tp_repr, void_cast( Variable_repr ) }, /* tp_repr */ |
| 264 |
{ Py_tp_richcompare, void_cast( Variable_richcmp ) }, /* tp_richcompare */ |
| 265 |
{ Py_tp_methods, void_cast( Variable_methods ) }, /* tp_methods */ |
| 266 |
{ Py_tp_new, void_cast( Variable_new ) }, /* tp_new */ |
| 267 |
{ Py_tp_alloc, void_cast( PyType_GenericAlloc ) }, /* tp_alloc */ |
| 268 |
{ Py_tp_free, void_cast( PyObject_GC_Del ) }, /* tp_free */ |
| 269 |
{ Py_nb_add, void_cast( Variable_add ) }, /* nb_add */ |
| 270 |
{ Py_nb_subtract, void_cast( Variable_sub ) }, /* nb_subtract */ |
| 271 |
{ Py_nb_multiply, void_cast( Variable_mul ) }, /* nb_multiply */ |
| 272 |
{ Py_nb_negative, void_cast( Variable_neg ) }, /* nb_negative */ |
| 273 |
{ Py_nb_true_divide, void_cast( Variable_div ) }, /* nb_true_divide */ |
| 274 |
{ 0, 0 }, |
| 275 |
}; |
| 276 |
|
| 277 |
|
| 278 |
} // namespace |
| 279 |
|
| 280 |
|
| 281 |
// Initialize static variables (otherwise the compiler eliminates them) |
| 282 |
PyTypeObject* Variable::TypeObject = NULL; |
| 283 |
|
| 284 |
|
| 285 |
PyType_Spec Variable::TypeObject_Spec = { |
| 286 |
"kiwisolver.Variable", /* tp_name */ |
| 287 |
sizeof( Variable ), /* tp_basicsize */ |
| 288 |
0, /* tp_itemsize */ |
| 289 |
Py_TPFLAGS_DEFAULT| |
| 290 |
Py_TPFLAGS_HAVE_GC| |
| 291 |
Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 292 |
Variable_Type_slots /* slots */ |
| 293 |
}; |
| 294 |
|
| 295 |
|
| 296 |
bool Variable::Ready() |
| 297 |
{ |
| 298 |
// The reference will be handled by the module to which we will add the type |
| 299 |
TypeObject = pytype_cast( PyType_FromSpec( &TypeObject_Spec ) ); |
| 300 |
if( !TypeObject ) |
| 301 |
{ |
| 302 |
return false; |
| 303 |
} |
| 304 |
return true; |
| 305 |
} |
| 306 |
|
| 307 |
} // namespace kiwisolver |