| 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 "types.h" |
| 11 |
#include "util.h" |
| 12 |
|
| 13 |
|
| 14 |
namespace kiwisolver |
| 15 |
{ |
| 16 |
|
| 17 |
namespace |
| 18 |
{ |
| 19 |
|
| 20 |
PyObject* |
| 21 |
Solver_new( PyTypeObject* type, PyObject* args, PyObject* kwargs ) |
| 22 |
{ |
| 23 |
if( PyTuple_GET_SIZE( args ) != 0 || ( kwargs && PyDict_Size( kwargs ) != 0 ) ) |
| 24 |
return cppy::type_error( "Solver.__new__ takes no arguments" ); |
| 25 |
PyObject* pysolver = PyType_GenericNew( type, args, kwargs ); |
| 26 |
if( !pysolver ) |
| 27 |
return 0; |
| 28 |
Solver* self = reinterpret_cast<Solver*>( pysolver ); |
| 29 |
ACQUIRE_GLOBAL_LOCK(); |
| 30 |
new( &self->solver ) kiwi::Solver(); |
| 31 |
RELEASE_GLOBAL_LOCK(); |
| 32 |
return pysolver; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
void |
| 37 |
Solver_dealloc( Solver* self ) |
| 38 |
{ |
| 39 |
ACQUIRE_GLOBAL_LOCK(); |
| 40 |
self->solver.~Solver(); |
| 41 |
RELEASE_GLOBAL_LOCK(); |
| 42 |
Py_TYPE( self )->tp_free( pyobject_cast( self ) ); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
PyObject* |
| 47 |
Solver_addConstraint( Solver* self, PyObject* other ) |
| 48 |
{ |
| 49 |
if( !Constraint::TypeCheck( other ) ) |
| 50 |
return cppy::type_error( other, "Constraint" ); |
| 51 |
Constraint* cn = reinterpret_cast<Constraint*>( other ); |
| 52 |
try |
| 53 |
{ |
| 54 |
ACQUIRE_GLOBAL_LOCK(); |
| 55 |
self->solver.addConstraint( cn->constraint ); |
| 56 |
RELEASE_GLOBAL_LOCK(); |
| 57 |
} |
| 58 |
catch( const kiwi::DuplicateConstraint& ) |
| 59 |
{ |
| 60 |
RELEASE_GLOBAL_LOCK(); |
| 61 |
PyErr_SetObject( DuplicateConstraint, other ); |
| 62 |
return 0; |
| 63 |
} |
| 64 |
catch( const kiwi::UnsatisfiableConstraint& ) |
| 65 |
{ |
| 66 |
RELEASE_GLOBAL_LOCK(); |
| 67 |
PyErr_SetObject( UnsatisfiableConstraint, other ); |
| 68 |
return 0; |
| 69 |
} |
| 70 |
Py_RETURN_NONE; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
PyObject* |
| 75 |
Solver_removeConstraint( Solver* self, PyObject* other ) |
| 76 |
{ |
| 77 |
if( !Constraint::TypeCheck( other ) ) |
| 78 |
return cppy::type_error( other, "Constraint" ); |
| 79 |
Constraint* cn = reinterpret_cast<Constraint*>( other ); |
| 80 |
try |
| 81 |
{ |
| 82 |
ACQUIRE_GLOBAL_LOCK(); |
| 83 |
self->solver.removeConstraint( cn->constraint ); |
| 84 |
RELEASE_GLOBAL_LOCK(); |
| 85 |
} |
| 86 |
catch( const kiwi::UnknownConstraint& ) |
| 87 |
{ |
| 88 |
RELEASE_GLOBAL_LOCK(); |
| 89 |
PyErr_SetObject( UnknownConstraint, other ); |
| 90 |
return 0; |
| 91 |
} |
| 92 |
Py_RETURN_NONE; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
PyObject* |
| 97 |
Solver_hasConstraint( Solver* self, PyObject* other ) |
| 98 |
{ |
| 99 |
if( !Constraint::TypeCheck( other ) ) |
| 100 |
return cppy::type_error( other, "Constraint" ); |
| 101 |
Constraint* cn = reinterpret_cast<Constraint*>( other ); |
| 102 |
|
| 103 |
ACQUIRE_GLOBAL_LOCK(); |
| 104 |
bool hasConstraint = self->solver.hasConstraint( cn->constraint ); |
| 105 |
RELEASE_GLOBAL_LOCK(); |
| 106 |
|
| 107 |
return cppy::incref( hasConstraint ? Py_True : Py_False ); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
PyObject* |
| 112 |
Solver_addEditVariable( Solver* self, PyObject* args ) |
| 113 |
{ |
| 114 |
PyObject* pyvar; |
| 115 |
PyObject* pystrength; |
| 116 |
if( !PyArg_ParseTuple( args, "OO", &pyvar, &pystrength ) ) |
| 117 |
return 0; |
| 118 |
if( !Variable::TypeCheck( pyvar ) ) |
| 119 |
return cppy::type_error( pyvar, "Variable" ); |
| 120 |
double strength; |
| 121 |
if( !convert_to_strength( pystrength, strength ) ) |
| 122 |
return 0; |
| 123 |
Variable* var = reinterpret_cast<Variable*>( pyvar ); |
| 124 |
try |
| 125 |
{ |
| 126 |
ACQUIRE_GLOBAL_LOCK(); |
| 127 |
self->solver.addEditVariable( var->variable, strength ); |
| 128 |
RELEASE_GLOBAL_LOCK(); |
| 129 |
} |
| 130 |
catch( const kiwi::DuplicateEditVariable& ) |
| 131 |
{ |
| 132 |
RELEASE_GLOBAL_LOCK(); |
| 133 |
PyErr_SetObject( DuplicateEditVariable, pyvar ); |
| 134 |
return 0; |
| 135 |
} |
| 136 |
catch( const kiwi::BadRequiredStrength& e ) |
| 137 |
{ |
| 138 |
RELEASE_GLOBAL_LOCK(); |
| 139 |
PyErr_SetString( BadRequiredStrength, e.what() ); |
| 140 |
return 0; |
| 141 |
} |
| 142 |
Py_RETURN_NONE; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
PyObject* |
| 147 |
Solver_removeEditVariable( Solver* self, PyObject* other ) |
| 148 |
{ |
| 149 |
if( !Variable::TypeCheck( other ) ) |
| 150 |
return cppy::type_error( other, "Variable" ); |
| 151 |
Variable* var = reinterpret_cast<Variable*>( other ); |
| 152 |
try |
| 153 |
{ |
| 154 |
ACQUIRE_GLOBAL_LOCK(); |
| 155 |
self->solver.removeEditVariable( var->variable ); |
| 156 |
RELEASE_GLOBAL_LOCK(); |
| 157 |
} |
| 158 |
catch( const kiwi::UnknownEditVariable& ) |
| 159 |
{ |
| 160 |
RELEASE_GLOBAL_LOCK(); |
| 161 |
PyErr_SetObject( UnknownEditVariable, other ); |
| 162 |
return 0; |
| 163 |
} |
| 164 |
Py_RETURN_NONE; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
PyObject* |
| 169 |
Solver_hasEditVariable( Solver* self, PyObject* other ) |
| 170 |
{ |
| 171 |
if( !Variable::TypeCheck( other ) ) |
| 172 |
return cppy::type_error( other, "Variable" ); |
| 173 |
Variable* var = reinterpret_cast<Variable*>( other ); |
| 174 |
ACQUIRE_GLOBAL_LOCK(); |
| 175 |
bool hasEditVariable = self->solver.hasEditVariable( var->variable ); |
| 176 |
RELEASE_GLOBAL_LOCK(); |
| 177 |
return cppy::incref( hasEditVariable ? Py_True : Py_False ); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
PyObject* |
| 182 |
Solver_suggestValue( Solver* self, PyObject* args ) |
| 183 |
{ |
| 184 |
PyObject* pyvar; |
| 185 |
PyObject* pyvalue; |
| 186 |
if( !PyArg_ParseTuple( args, "OO", &pyvar, &pyvalue ) ) |
| 187 |
return 0; |
| 188 |
if( !Variable::TypeCheck( pyvar ) ) |
| 189 |
return cppy::type_error( pyvar, "Variable" ); |
| 190 |
double value; |
| 191 |
if( !convert_to_double( pyvalue, value ) ) |
| 192 |
return 0; |
| 193 |
Variable* var = reinterpret_cast<Variable*>( pyvar ); |
| 194 |
try |
| 195 |
{ |
| 196 |
ACQUIRE_GLOBAL_LOCK(); |
| 197 |
self->solver.suggestValue( var->variable, value ); |
| 198 |
RELEASE_GLOBAL_LOCK(); |
| 199 |
} |
| 200 |
catch( const kiwi::UnknownEditVariable& ) |
| 201 |
{ |
| 202 |
RELEASE_GLOBAL_LOCK(); |
| 203 |
PyErr_SetObject( UnknownEditVariable, pyvar ); |
| 204 |
return 0; |
| 205 |
} |
| 206 |
Py_RETURN_NONE; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
PyObject* |
| 211 |
Solver_updateVariables( Solver* self ) |
| 212 |
{ |
| 213 |
ACQUIRE_GLOBAL_LOCK(); |
| 214 |
self->solver.updateVariables(); |
| 215 |
RELEASE_GLOBAL_LOCK(); |
| 216 |
Py_RETURN_NONE; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
PyObject* |
| 221 |
Solver_reset( Solver* self ) |
| 222 |
{ |
| 223 |
ACQUIRE_GLOBAL_LOCK(); |
| 224 |
self->solver.reset(); |
| 225 |
RELEASE_GLOBAL_LOCK(); |
| 226 |
Py_RETURN_NONE; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
PyObject* |
| 231 |
Solver_dump( Solver* self ) |
| 232 |
{ |
| 233 |
ACQUIRE_GLOBAL_LOCK(); |
| 234 |
std::string dumps = self->solver.dumps(); |
| 235 |
RELEASE_GLOBAL_LOCK(); |
| 236 |
cppy::ptr dump_str( PyUnicode_FromString( dumps.c_str() ) ); |
| 237 |
PyObject_Print( dump_str.get(), stdout, 0 ); |
| 238 |
Py_RETURN_NONE; |
| 239 |
} |
| 240 |
|
| 241 |
PyObject* |
| 242 |
Solver_dumps( Solver* self ) |
| 243 |
{ |
| 244 |
ACQUIRE_GLOBAL_LOCK(); |
| 245 |
std::string dumps = self->solver.dumps(); |
| 246 |
RELEASE_GLOBAL_LOCK(); |
| 247 |
return PyUnicode_FromString( dumps.c_str() ); |
| 248 |
} |
| 249 |
|
| 250 |
static PyMethodDef |
| 251 |
Solver_methods[] = { |
| 252 |
{ "addConstraint", ( PyCFunction )Solver_addConstraint, METH_O, |
| 253 |
"Add a constraint to the solver." }, |
| 254 |
{ "removeConstraint", ( PyCFunction )Solver_removeConstraint, METH_O, |
| 255 |
"Remove a constraint from the solver." }, |
| 256 |
{ "hasConstraint", ( PyCFunction )Solver_hasConstraint, METH_O, |
| 257 |
"Check whether the solver contains a constraint." }, |
| 258 |
{ "addEditVariable", ( PyCFunction )Solver_addEditVariable, METH_VARARGS, |
| 259 |
"Add an edit variable to the solver." }, |
| 260 |
{ "removeEditVariable", ( PyCFunction )Solver_removeEditVariable, METH_O, |
| 261 |
"Remove an edit variable from the solver." }, |
| 262 |
{ "hasEditVariable", ( PyCFunction )Solver_hasEditVariable, METH_O, |
| 263 |
"Check whether the solver contains an edit variable." }, |
| 264 |
{ "suggestValue", ( PyCFunction )Solver_suggestValue, METH_VARARGS, |
| 265 |
"Suggest a desired value for an edit variable." }, |
| 266 |
{ "updateVariables", ( PyCFunction )Solver_updateVariables, METH_NOARGS, |
| 267 |
"Update the values of the solver variables." }, |
| 268 |
{ "reset", ( PyCFunction )Solver_reset, METH_NOARGS, |
| 269 |
"Reset the solver to the initial empty starting condition." }, |
| 270 |
{ "dump", ( PyCFunction )Solver_dump, METH_NOARGS, |
| 271 |
"Dump a representation of the solver internals to stdout." }, |
| 272 |
{ "dumps", ( PyCFunction )Solver_dumps, METH_NOARGS, |
| 273 |
"Dump a representation of the solver internals to a string." }, |
| 274 |
{ 0 } // sentinel |
| 275 |
}; |
| 276 |
|
| 277 |
|
| 278 |
static PyType_Slot Solver_Type_slots[] = { |
| 279 |
{ Py_tp_dealloc, void_cast( Solver_dealloc ) }, /* tp_dealloc */ |
| 280 |
{ Py_tp_methods, void_cast( Solver_methods ) }, /* tp_methods */ |
| 281 |
{ Py_tp_new, void_cast( Solver_new ) }, /* tp_new */ |
| 282 |
{ Py_tp_alloc, void_cast( PyType_GenericAlloc ) }, /* tp_alloc */ |
| 283 |
{ Py_tp_free, void_cast( PyObject_Del ) }, /* tp_free */ |
| 284 |
{ 0, 0 }, |
| 285 |
}; |
| 286 |
|
| 287 |
|
| 288 |
} // namespace |
| 289 |
|
| 290 |
|
| 291 |
// Initialize static variables (otherwise the compiler eliminates them) |
| 292 |
PyTypeObject* Solver::TypeObject = NULL; |
| 293 |
|
| 294 |
|
| 295 |
PyType_Spec Solver::TypeObject_Spec = { |
| 296 |
"kiwisolver.Solver", /* tp_name */ |
| 297 |
sizeof( Solver ), /* tp_basicsize */ |
| 298 |
0, /* tp_itemsize */ |
| 299 |
Py_TPFLAGS_DEFAULT| |
| 300 |
Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 301 |
Solver_Type_slots /* slots */ |
| 302 |
}; |
| 303 |
|
| 304 |
|
| 305 |
bool Solver::Ready() |
| 306 |
{ |
| 307 |
// The reference will be handled by the module to which we will add the type |
| 308 |
TypeObject = pytype_cast( PyType_FromSpec( &TypeObject_Spec ) ); |
| 309 |
if( !TypeObject ) |
| 310 |
{ |
| 311 |
return false; |
| 312 |
} |
| 313 |
return true; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
PyObject* DuplicateConstraint; |
| 318 |
|
| 319 |
PyObject* UnsatisfiableConstraint; |
| 320 |
|
| 321 |
PyObject* UnknownConstraint; |
| 322 |
|
| 323 |
PyObject* DuplicateEditVariable; |
| 324 |
|
| 325 |
PyObject* UnknownEditVariable; |
| 326 |
|
| 327 |
PyObject* BadRequiredStrength; |
| 328 |
|
| 329 |
|
| 330 |
bool init_exceptions() |
| 331 |
{ |
| 332 |
cppy::ptr mod( PyImport_ImportModule( "kiwisolver.exceptions" ) ); |
| 333 |
if( !mod ) |
| 334 |
{ |
| 335 |
return false; |
| 336 |
} |
| 337 |
|
| 338 |
DuplicateConstraint = mod.getattr( "DuplicateConstraint" ); |
| 339 |
if( !DuplicateConstraint ) |
| 340 |
{ |
| 341 |
return false; |
| 342 |
} |
| 343 |
|
| 344 |
UnsatisfiableConstraint = mod.getattr( "UnsatisfiableConstraint" ); |
| 345 |
if( !UnsatisfiableConstraint ) |
| 346 |
{ |
| 347 |
return false; |
| 348 |
} |
| 349 |
|
| 350 |
UnknownConstraint = mod.getattr( "UnknownConstraint" ); |
| 351 |
if( !UnknownConstraint ) |
| 352 |
{ |
| 353 |
return false; |
| 354 |
} |
| 355 |
|
| 356 |
DuplicateEditVariable = mod.getattr( "DuplicateEditVariable" ); |
| 357 |
if( !DuplicateEditVariable ) |
| 358 |
{ |
| 359 |
return false; |
| 360 |
} |
| 361 |
|
| 362 |
UnknownEditVariable = mod.getattr( "UnknownEditVariable" ); |
| 363 |
if( !UnknownEditVariable ) |
| 364 |
{ |
| 365 |
return false; |
| 366 |
} |
| 367 |
|
| 368 |
BadRequiredStrength = mod.getattr( "BadRequiredStrength" ); |
| 369 |
if( !BadRequiredStrength ) |
| 370 |
{ |
| 371 |
return false; |
| 372 |
} |
| 373 |
|
| 374 |
return true; |
| 375 |
} |
| 376 |
|
| 377 |
} // namespace |