Debian Codegraph @

py/src/kiwisolver.cpp

raw
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 <mutex>
10 #include <kiwi/kiwi.h>
11 #include "types.h"
12 #include "version.h"
13
14 namespace kiwisolver
15 {
16
17 std::recursive_mutex global_lock;
18
19 }
20
21 namespace
22 {
23
24
25 bool ready_types()
26 {
27 using namespace kiwisolver;
28 if( !Variable::Ready() )
29 {
30 return false;
31 }
32 if( !Term::Ready() )
33 {
34 return false;
35 }
36 if( !Expression::Ready() )
37 {
38 return false;
39 }
40 if( !Constraint::Ready() )
41 {
42 return false;
43 }
44 if( !strength::Ready() )
45 {
46 return false;
47 }
48 if( !Solver::Ready() )
49 {
50 return false;
51 }
52 return true;
53 }
54
55 bool add_objects( PyObject* mod )
56 {
57 using namespace kiwisolver;
58
59 cppy::ptr kiwiversion( PyUnicode_FromString( KIWI_VERSION ) );
60 if( !kiwiversion )
61 {
62 return false;
63 }
64 cppy::ptr pyversion( PyUnicode_FromString( PY_KIWI_VERSION ) );
65 if( !pyversion )
66 {
67 return false;
68 }
69 cppy::ptr pystrength( PyType_GenericNew( strength::TypeObject, 0, 0 ) );
70 if( !pystrength )
71 {
72 return false;
73 }
74
75 if( PyModule_AddObject( mod, "__version__", pyversion.get() ) < 0 )
76 {
77 return false;
78 }
79 pyversion.release();
80
81 if( PyModule_AddObject( mod, "__kiwi_version__", kiwiversion.get() ) < 0 )
82 {
83 return false;
84 }
85 kiwiversion.release();
86
87 if( PyModule_AddObject( mod, "strength", pystrength.get() ) < 0 )
88 {
89 return false;
90 }
91 pystrength.release();
92
93 // Variable
94 cppy::ptr var( pyobject_cast( Variable::TypeObject ) );
95 if( PyModule_AddObject( mod, "Variable", var.get() ) < 0 )
96 {
97 return false;
98 }
99 var.release();
100
101 // Term
102 cppy::ptr term( pyobject_cast( Term::TypeObject ) );
103 if( PyModule_AddObject( mod, "Term", term.get() ) < 0 )
104 {
105 return false;
106 }
107 term.release();
108
109 // Expression
110 cppy::ptr expr( pyobject_cast( Expression::TypeObject ) );
111 if( PyModule_AddObject( mod, "Expression", expr.get() ) < 0 )
112 {
113 return false;
114 }
115 expr.release();
116
117 // Constraint
118 cppy::ptr cons( pyobject_cast( Constraint::TypeObject ) );
119 if( PyModule_AddObject( mod, "Constraint", cons.get() ) < 0 )
120 {
121 return false;
122 }
123 cons.release();
124
125 cppy::ptr solver( pyobject_cast( Solver::TypeObject ) );
126 if( PyModule_AddObject( mod, "Solver", solver.get() ) < 0 )
127 {
128 return false;
129 }
130 solver.release();
131
132 PyModule_AddObject( mod, "DuplicateConstraint", DuplicateConstraint );
133 PyModule_AddObject( mod, "UnsatisfiableConstraint", UnsatisfiableConstraint );
134 PyModule_AddObject( mod, "UnknownConstraint", UnknownConstraint );
135 PyModule_AddObject( mod, "DuplicateEditVariable", DuplicateEditVariable );
136 PyModule_AddObject( mod, "UnknownEditVariable", UnknownEditVariable );
137 PyModule_AddObject( mod, "BadRequiredStrength", BadRequiredStrength );
138
139 return true;
140 }
141
142
143 int
144 kiwi_modexec( PyObject *mod )
145 {
146 if( !ready_types() )
147 {
148 return -1;
149 }
150 if( !kiwisolver::init_exceptions() )
151 {
152 return -1;
153 }
154 if( !add_objects( mod ) )
155 {
156 return -1;
157 }
158
159
160 return 0;
161 }
162
163
164 static PyMethodDef
165 kiwisolver_methods[] = {
166 { 0 } // Sentinel
167 };
168
169
170 PyModuleDef_Slot kiwisolver_slots[] = {
171 {Py_mod_exec, reinterpret_cast<void*>( kiwi_modexec ) },
172 #ifdef Py_GIL_DISABLED
173 {Py_mod_gil, Py_MOD_GIL_NOT_USED},
174 #endif
175 {0, NULL}
176 };
177
178
179 struct PyModuleDef moduledef = {
180 PyModuleDef_HEAD_INIT,
181 "_cext",
182 "kiwisolver extension module",
183 0,
184 kiwisolver_methods,
185 kiwisolver_slots,
186 NULL,
187 NULL,
188 NULL
189 };
190
191 } // namespace
192
193
194 PyMODINIT_FUNC PyInit__cext( void )
195 {
196 return PyModuleDef_Init( &moduledef );
197 }