36 #ifndef VIGRA_PYTHON_UTILITY_HXX
37 #define VIGRA_PYTHON_UTILITY_HXX
42 #include "vigra/error.hxx"
43 #include "vigra/tinyvector.hxx"
47 template <
class PYOBJECT_PTR>
48 void pythonToCppException(PYOBJECT_PTR obj)
52 PyObject * type, * value, *
trace;
53 PyErr_Fetch(&type, &value, &trace);
56 std::string message(((PyTypeObject *)type)->tp_name);
57 if(PyString_Check(value))
59 message += std::string(
": ") + PyString_AS_STRING(value);
65 throw std::runtime_error(message.c_str());
81 typedef PyObject element_type;
82 typedef PyObject value_type;
83 typedef PyObject * pointer;
84 typedef PyObject & reference;
86 enum refcount_policy { increment_count, borrowed_reference = increment_count,
87 keep_count, new_reference = keep_count };
89 explicit python_ptr(pointer p = 0, refcount_policy rp = increment_count)
92 if(rp == increment_count)
98 python_ptr(python_ptr
const & p)
104 python_ptr & operator=(pointer p)
110 python_ptr & operator=(python_ptr
const & r)
121 void reset(pointer p = 0, refcount_policy rp = increment_count)
125 if(rp == increment_count)
133 pointer release(
bool return_borrowed_reference =
false)
137 if(return_borrowed_reference)
144 reference operator* ()
const
146 vigra_precondition(ptr_ != 0,
"python_ptr::operator*(): Cannot dereference NULL pointer.");
150 pointer operator-> ()
const
152 vigra_precondition(ptr_ != 0,
"python_ptr::operator->(): Cannot dereference NULL pointer.");
166 operator pointer()
const
171 bool operator! ()
const
178 return ptr_ && ptr_->ob_refcnt == 1;
181 void swap(python_ptr & other)
183 std::swap(ptr_, other.ptr_);
188 return ptr_ == p.ptr_;
198 return ptr_ != p.ptr_;
207 inline void swap(python_ptr & a, python_ptr & b)
215 makePythonDictionary(
char const * k1 = 0, PyObject * a1 = 0,
216 char const * k2 = 0, PyObject * a2 = 0,
217 char const * k3 = 0, PyObject * a3 = 0)
219 python_ptr dict(PyDict_New(), python_ptr::keep_count);
220 pythonToCppException(dict);
222 PyDict_SetItemString(dict, k1, a1);
224 PyDict_SetItemString(dict, k2, a2);
226 PyDict_SetItemString(dict, k3, a3);
232 inline python_ptr pythonFromData(
bool t)
234 python_ptr res(PyBool_FromLong(t ? 1 : 0), python_ptr::keep_count);
235 pythonToCppException(res);
239 inline python_ptr pythonFromData(std::string
const & s)
241 python_ptr res(PyString_FromString(s.c_str()), python_ptr::keep_count);
242 pythonToCppException(res);
246 inline python_ptr pythonFromData(
long long t)
249 if(t > (
long long)NumericTraits<long>::max() || t < (
long long)NumericTraits<long>::min())
250 res = python_ptr(PyLong_FromLongLong(t), python_ptr::keep_count);
252 res = python_ptr(PyInt_FromLong((
long)t), python_ptr::keep_count);
253 pythonToCppException(res);
257 inline python_ptr pythonFromData(
unsigned long long t)
260 if(t > (
unsigned long long)NumericTraits<long>::max())
261 res = python_ptr(PyLong_FromUnsignedLongLong(t), python_ptr::keep_count);
263 res = python_ptr(PyInt_FromLong((
long)t), python_ptr::keep_count);
264 pythonToCppException(res);
268 #define VIGRA_PYTHON_FROM_DATA(type, fct, cast_type) \
269 inline python_ptr pythonFromData(type t) \
271 python_ptr res(fct((cast_type)t), python_ptr::keep_count); \
272 pythonToCppException(res); \
276 VIGRA_PYTHON_FROM_DATA(
signed char, PyInt_FromLong,
long)
277 VIGRA_PYTHON_FROM_DATA(
unsigned char, PyInt_FromLong,
long)
278 VIGRA_PYTHON_FROM_DATA(
short, PyInt_FromLong,
long)
279 VIGRA_PYTHON_FROM_DATA(
unsigned short, PyInt_FromLong,
long)
280 VIGRA_PYTHON_FROM_DATA(
long, PyInt_FromLong,
long)
281 VIGRA_PYTHON_FROM_DATA(
unsigned long, PyInt_FromSize_t,
size_t)
282 VIGRA_PYTHON_FROM_DATA(
int, PyInt_FromSsize_t, Py_ssize_t)
283 VIGRA_PYTHON_FROM_DATA(
unsigned int, PyInt_FromSize_t,
size_t)
284 VIGRA_PYTHON_FROM_DATA(
float, PyFloat_FromDouble,
double)
285 VIGRA_PYTHON_FROM_DATA(
double, PyFloat_FromDouble,
double)
286 VIGRA_PYTHON_FROM_DATA(
char const *, PyString_FromString,
char const *)
288 #undef VIGRA_PYTHON_FROM_DATA
292 #define VIGRA_DATA_FROM_PYTHON(type, check, extract) \
293 inline type dataFromPython(PyObject * data, type const & defaultVal) \
295 return data && check(data) \
296 ? (type)extract(data) \
300 VIGRA_DATA_FROM_PYTHON(
signed char, PyInt_Check, PyInt_AsLong)
301 VIGRA_DATA_FROM_PYTHON(
unsigned char, PyInt_Check, PyInt_AsLong)
302 VIGRA_DATA_FROM_PYTHON(
short, PyInt_Check, PyInt_AsLong)
303 VIGRA_DATA_FROM_PYTHON(
unsigned short, PyInt_Check, PyInt_AsLong)
304 VIGRA_DATA_FROM_PYTHON(
long, PyInt_Check, PyInt_AsLong)
305 VIGRA_DATA_FROM_PYTHON(
unsigned long, PyInt_Check, PyInt_AsUnsignedLongMask)
306 VIGRA_DATA_FROM_PYTHON(
int, PyInt_Check, PyInt_AsLong)
307 VIGRA_DATA_FROM_PYTHON(
unsigned int, PyInt_Check, PyInt_AsUnsignedLongMask)
308 VIGRA_DATA_FROM_PYTHON(
long long, PyInt_Check, PyInt_AsSsize_t)
309 VIGRA_DATA_FROM_PYTHON(
unsigned long long, PyInt_Check, PyInt_AsUnsignedLongLongMask)
310 VIGRA_DATA_FROM_PYTHON(
float, PyFloat_Check, PyFloat_AsDouble)
311 VIGRA_DATA_FROM_PYTHON(
double, PyFloat_Check, PyFloat_AsDouble)
313 inline std::
string dataFromPython(PyObject * data, const
char * defaultVal)
315 return data && PyString_Check(data)
316 ? std::string(PyString_AsString(data))
317 : std::string(defaultVal);
320 inline std::string dataFromPython(PyObject * data, std::string
const & defaultVal)
322 return data && PyString_Check(data)
323 ? std::string(PyString_AsString(data))
327 inline python_ptr dataFromPython(PyObject * data, python_ptr defaultVal)
334 #undef VIGRA_DATA_FROM_PYTHON
339 T pythonGetAttr(PyObject * obj,
const char * key, T defaultValue)
344 python_ptr k(PyString_FromString(key), python_ptr::keep_count);
345 pythonToCppException(k);
346 python_ptr pres(PyObject_GetAttr(obj, k), python_ptr::keep_count);
349 return dataFromPython(pres, defaultValue);
353 pythonGetAttr(PyObject * obj,
const char * key,
const char * defaultValue)
356 return std::string(defaultValue);
358 python_ptr k(PyString_FromString(key), python_ptr::keep_count);
359 pythonToCppException(k);
360 python_ptr pres(PyObject_GetAttr(obj, k), python_ptr::keep_count);
363 return dataFromPython(pres, defaultValue);
368 template <
class T,
int N>
369 python_ptr shapeToPythonTuple(TinyVector<T, N>
const & shape)
371 python_ptr tuple(PyTuple_New(N), python_ptr::keep_count);
372 pythonToCppException(tuple);
373 for(
unsigned int k=0; k<N; ++k)
375 PyTuple_SET_ITEM((PyTupleObject *)tuple.get(), k, pythonFromData(shape[k]).release());
381 python_ptr shapeToPythonTuple(ArrayVectorView<T>
const & shape)
383 python_ptr tuple(PyTuple_New(shape.size()), python_ptr::keep_count);
384 pythonToCppException(tuple);
385 for(
unsigned int k=0; k<shape.size(); ++k)
387 PyTuple_SET_ITEM((PyTupleObject *)tuple.get(), k, pythonFromData(shape[k]).release());
396 PyThreadState * save_;
399 PyAllowThreads(PyAllowThreads
const &);
400 PyAllowThreads & operator=(PyAllowThreads
const &);
404 : save_(PyEval_SaveThread())
409 PyEval_RestoreThread(save_);
415 #endif // VIGRA_PYTHON_UTILITY_HXX
NumericTraits< T >::Promote trace(MultiArrayView< 2, T, C > const &m)
Definition: matrix.hxx:799
bool operator!=(FFTWComplex< R > const &a, const FFTWComplex< R > &b)
not equal
Definition: fftw3.hxx:841
bool operator==(FFTWComplex< R > const &a, const FFTWComplex< R > &b)
equal
Definition: fftw3.hxx:825