Debian Codegraph @

py/src/util.h

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 #pragma once
9 #include <cppy/cppy.h>
10 #include <map>
11 #include <mutex>
12 #include <string>
13 #include <kiwi/kiwi.h>
14 #include "types.h"
15
16
17 namespace kiwisolver
18 {
19
20 #ifdef Py_GIL_DISABLED
21 extern std::recursive_mutex global_lock;
22 #define ACQUIRE_GLOBAL_LOCK() global_lock.lock()
23 #define RELEASE_GLOBAL_LOCK() global_lock.unlock()
24 #else
25 #define ACQUIRE_GLOBAL_LOCK()
26 #define RELEASE_GLOBAL_LOCK()
27 #endif
28
29 #ifndef Py_BEGIN_CRITICAL_SECTION
30 #define Py_BEGIN_CRITICAL_SECTION(op) {
31 #define Py_END_CRITICAL_SECTION() }
32 #endif
33
34 inline bool
35 convert_to_double( PyObject* obj, double& out )
36 {
37 if( PyFloat_Check( obj ) )
38 {
39 out = PyFloat_AS_DOUBLE( obj );
40 return true;
41 }
42 if( PyLong_Check( obj ) )
43 {
44 out = PyLong_AsDouble( obj );
45 if( out == -1.0 && PyErr_Occurred() )
46 return false;
47 return true;
48 }
49 cppy::type_error( obj, "float, int, or long" );
50 return false;
51 }
52
53
54 inline bool
55 convert_pystr_to_str( PyObject* value, std::string& out )
56 {
57 out = PyUnicode_AsUTF8( value );
58 return true;
59 }
60
61
62 inline bool
63 convert_to_strength( PyObject* value, double& out )
64 {
65 if( PyUnicode_Check( value ) )
66 {
67 std::string str;
68 if( !convert_pystr_to_str( value, str ) )
69 return false;
70 if( str == "required" )
71 out = kiwi::strength::required;
72 else if( str == "strong" )
73 out = kiwi::strength::strong;
74 else if( str == "medium" )
75 out = kiwi::strength::medium;
76 else if( str == "weak" )
77 out = kiwi::strength::weak;
78 else
79 {
80 PyErr_Format(
81 PyExc_ValueError,
82 "string strength must be 'required', 'strong', 'medium', "
83 "or 'weak', not '%s'",
84 str.c_str()
85 );
86 return false;
87 }
88 return true;
89 }
90 if( !convert_to_double( value, out ) )
91 return false;
92 return true;
93 }
94
95
96 inline bool
97 convert_to_relational_op( PyObject* value, kiwi::RelationalOperator& out )
98 {
99 if( !PyUnicode_Check( value ) )
100 {
101 cppy::type_error( value, "str" );
102 return false;
103 }
104 std::string str;
105 if( !convert_pystr_to_str( value, str ) )
106 return false;
107 if( str == "==" )
108 out = kiwi::OP_EQ;
109 else if( str == "<=" )
110 out = kiwi::OP_LE;
111 else if( str == ">=" )
112 out = kiwi::OP_GE;
113 else
114 {
115 PyErr_Format(
116 PyExc_ValueError,
117 "relational operator must be '==', '<=', or '>=', not '%s'",
118 str.c_str()
119 );
120 return false;
121 }
122 return true;
123 }
124
125
126 inline PyObject*
127 make_terms( const std::map<PyObject*, double>& coeffs )
128 {
129 typedef std::map<PyObject*, double>::const_iterator iter_t;
130 cppy::ptr terms( PyTuple_New( coeffs.size() ) );
131 if( !terms )
132 return 0;
133 Py_ssize_t size = PyTuple_GET_SIZE( terms.get() );
134 for( Py_ssize_t i = 0; i < size; ++i ) // zero tuple for safe early return
135 PyTuple_SET_ITEM( terms.get(), i, 0 );
136 Py_ssize_t i = 0;
137 iter_t it = coeffs.begin();
138 iter_t end = coeffs.end();
139 for( ; it != end; ++it, ++i )
140 {
141 PyObject* pyterm = PyType_GenericNew( Term::TypeObject, 0, 0 );
142 if( !pyterm )
143 return 0;
144 Term* term = reinterpret_cast<Term*>( pyterm );
145 term->variable = cppy::incref( it->first );
146 term->coefficient = it->second;
147 PyTuple_SET_ITEM( terms.get(), i, pyterm );
148 }
149 return terms.release();
150 }
151
152
153 inline PyObject*
154 reduce_expression( PyObject* pyexpr ) // pyexpr must be an Expression
155 {
156 Expression* expr = reinterpret_cast<Expression*>( pyexpr );
157 std::map<PyObject*, double> coeffs;
158 Py_ssize_t size = PyTuple_GET_SIZE( expr->terms );
159 for( Py_ssize_t i = 0; i < size; ++i )
160 {
161 PyObject* item = PyTuple_GET_ITEM( expr->terms, i );
162 Term* term = reinterpret_cast<Term*>( item );
163 coeffs[ term->variable ] += term->coefficient;
164 }
165 cppy::ptr terms( make_terms( coeffs ) );
166 if( !terms )
167 return 0;
168 PyObject* pynewexpr = PyType_GenericNew( Expression::TypeObject, 0, 0 );
169 if( !pynewexpr )
170 return 0;
171 Expression* newexpr = reinterpret_cast<Expression*>( pynewexpr );
172 newexpr->terms = terms.release();
173 newexpr->constant = expr->constant;
174 return pynewexpr;
175 }
176
177
178 inline kiwi::Expression
179 convert_to_kiwi_expression( PyObject* pyexpr ) // pyexpr must be an Expression
180 {
181 Expression* expr = reinterpret_cast<Expression*>( pyexpr );
182 std::vector<kiwi::Term> kterms;
183 Py_ssize_t size = PyTuple_GET_SIZE( expr->terms );
184 for( Py_ssize_t i = 0; i < size; ++i )
185 {
186 PyObject* item = PyTuple_GET_ITEM( expr->terms, i );
187 Term* term = reinterpret_cast<Term*>( item );
188 Variable* var = reinterpret_cast<Variable*>( term->variable );
189 ACQUIRE_GLOBAL_LOCK();
190 kiwi::Term t( var->variable, term->coefficient );
191 RELEASE_GLOBAL_LOCK();
192 kterms.push_back( t );
193 }
194 ACQUIRE_GLOBAL_LOCK();
195 kiwi::Expression expression( kterms, expr->constant );
196 RELEASE_GLOBAL_LOCK();
197 return expression;
198 }
199
200
201 inline const char*
202 pyop_str( int op )
203 {
204 switch( op )
205 {
206 case Py_LT:
207 return "<";
208 case Py_LE:
209 return "<=";
210 case Py_EQ:
211 return "==";
212 case Py_NE:
213 return "!=";
214 case Py_GT:
215 return ">";
216 case Py_GE:
217 return ">=";
218 default:
219 return "";
220 }
221 }
222
223
224 } // namespace kiwisolver