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, new_nonzero_reference };
89 explicit python_ptr(pointer p = 0, refcount_policy rp = increment_count)
92 if(rp == increment_count)
96 else if(rp == new_nonzero_reference)
98 pythonToCppException(p);
102 python_ptr(python_ptr
const & p)
108 python_ptr & operator=(pointer p)
114 python_ptr & operator=(python_ptr
const & r)
125 void reset(pointer p = 0, refcount_policy rp = increment_count)
129 if(rp == increment_count)
133 else if(rp == new_nonzero_reference)
135 pythonToCppException(p);
141 pointer release(
bool return_borrowed_reference =
false)
145 if(return_borrowed_reference)
152 reference operator* ()
const 154 vigra_precondition(ptr_ != 0,
"python_ptr::operator*(): Cannot dereference NULL pointer.");
158 pointer operator-> ()
const 160 vigra_precondition(ptr_ != 0,
"python_ptr::operator->(): Cannot dereference NULL pointer.");
174 operator pointer()
const 179 bool operator! ()
const 186 return ptr_ && ptr_->ob_refcnt == 1;
189 void swap(python_ptr & other)
191 std::swap(ptr_, other.ptr_);
196 return ptr_ == p.ptr_;
206 return ptr_ != p.ptr_;
215 inline void swap(python_ptr & a, python_ptr & b)
223 makePythonDictionary(
char const * k1 = 0, PyObject * a1 = 0,
224 char const * k2 = 0, PyObject * a2 = 0,
225 char const * k3 = 0, PyObject * a3 = 0)
227 python_ptr dict(PyDict_New(), python_ptr::keep_count);
228 pythonToCppException(dict);
230 PyDict_SetItemString(dict, k1, a1);
232 PyDict_SetItemString(dict, k2, a2);
234 PyDict_SetItemString(dict, k3, a3);
240 inline python_ptr pythonFromData(
bool t)
242 python_ptr res(PyBool_FromLong(t ? 1 : 0), python_ptr::keep_count);
243 pythonToCppException(res);
247 inline python_ptr pythonFromData(std::string
const & s)
249 python_ptr res(PyString_FromString(s.c_str()), python_ptr::keep_count);
250 pythonToCppException(res);
254 inline python_ptr pythonFromData(
long long t)
257 if(t > (
long long)NumericTraits<long>::max() || t < (
long long)NumericTraits<long>::min())
258 res = python_ptr(PyLong_FromLongLong(t), python_ptr::keep_count);
260 res = python_ptr(PyInt_FromLong((
long)t), python_ptr::keep_count);
261 pythonToCppException(res);
265 inline python_ptr pythonFromData(
unsigned long long t)
268 if(t > (
unsigned long long)NumericTraits<long>::max())
269 res = python_ptr(PyLong_FromUnsignedLongLong(t), python_ptr::keep_count);
271 res = python_ptr(PyInt_FromLong((
long)t), python_ptr::keep_count);
272 pythonToCppException(res);
276 #define VIGRA_PYTHON_FROM_DATA(type, fct, cast_type) \ 277 inline python_ptr pythonFromData(type t) \ 279 python_ptr res(fct((cast_type)t), python_ptr::keep_count); \ 280 pythonToCppException(res); \ 284 VIGRA_PYTHON_FROM_DATA(
signed char, PyInt_FromLong,
long)
285 VIGRA_PYTHON_FROM_DATA(
unsigned char, PyInt_FromLong,
long)
286 VIGRA_PYTHON_FROM_DATA(
short, PyInt_FromLong,
long)
287 VIGRA_PYTHON_FROM_DATA(
unsigned short, PyInt_FromLong,
long)
288 VIGRA_PYTHON_FROM_DATA(
long, PyInt_FromLong,
long)
289 VIGRA_PYTHON_FROM_DATA(
unsigned long, PyInt_FromSize_t,
size_t)
290 VIGRA_PYTHON_FROM_DATA(
int, PyInt_FromSsize_t, Py_ssize_t)
291 VIGRA_PYTHON_FROM_DATA(
unsigned int, PyInt_FromSize_t,
size_t)
292 VIGRA_PYTHON_FROM_DATA(
float, PyFloat_FromDouble,
double)
293 VIGRA_PYTHON_FROM_DATA(
double, PyFloat_FromDouble,
double)
294 VIGRA_PYTHON_FROM_DATA(
char const *, PyString_FromString,
char const *)
296 #undef VIGRA_PYTHON_FROM_DATA 300 #define VIGRA_DATA_FROM_PYTHON(type, check, extract) \ 301 inline type dataFromPython(PyObject * data, type const & defaultVal) \ 303 return data && check(data) \ 304 ? (type)extract(data) \ 308 VIGRA_DATA_FROM_PYTHON(
signed char, PyInt_Check, PyInt_AsLong)
309 VIGRA_DATA_FROM_PYTHON(
unsigned char, PyInt_Check, PyInt_AsLong)
310 VIGRA_DATA_FROM_PYTHON(
short, PyInt_Check, PyInt_AsLong)
311 VIGRA_DATA_FROM_PYTHON(
unsigned short, PyInt_Check, PyInt_AsLong)
312 VIGRA_DATA_FROM_PYTHON(
long, PyInt_Check, PyInt_AsLong)
313 VIGRA_DATA_FROM_PYTHON(
unsigned long, PyInt_Check, PyInt_AsUnsignedLongMask)
314 VIGRA_DATA_FROM_PYTHON(
int, PyInt_Check, PyInt_AsLong)
315 VIGRA_DATA_FROM_PYTHON(
unsigned int, PyInt_Check, PyInt_AsUnsignedLongMask)
316 VIGRA_DATA_FROM_PYTHON(
long long, PyInt_Check, PyInt_AsSsize_t)
317 VIGRA_DATA_FROM_PYTHON(
unsigned long long, PyInt_Check, PyInt_AsUnsignedLongLongMask)
318 VIGRA_DATA_FROM_PYTHON(
float, PyFloat_Check, PyFloat_AsDouble)
319 VIGRA_DATA_FROM_PYTHON(
double, PyFloat_Check, PyFloat_AsDouble)
321 inline
std::
string dataFromPython(PyObject * data, const
char * defaultVal)
323 return data && PyString_Check(data)
324 ? std::string(PyString_AsString(data))
325 :
std::string(defaultVal);
328 inline std::string dataFromPython(PyObject * data, std::string
const & defaultVal)
330 return data && PyString_Check(data)
331 ? std::string(PyString_AsString(data))
335 inline python_ptr dataFromPython(PyObject * data, python_ptr defaultVal)
342 #undef VIGRA_DATA_FROM_PYTHON 347 T pythonGetAttr(PyObject * obj,
const char * key, T defaultValue)
352 python_ptr k(PyString_FromString(key), python_ptr::keep_count);
353 pythonToCppException(k);
354 python_ptr pres(PyObject_GetAttr(obj, k), python_ptr::keep_count);
357 return dataFromPython(pres, defaultValue);
361 pythonGetAttr(PyObject * obj,
const char * key,
const char * defaultValue)
364 return std::string(defaultValue);
366 python_ptr k(PyString_FromString(key), python_ptr::keep_count);
367 pythonToCppException(k);
368 python_ptr pres(PyObject_GetAttr(obj, k), python_ptr::keep_count);
371 return dataFromPython(pres, defaultValue);
376 template <
class T,
int N>
377 python_ptr shapeToPythonTuple(TinyVector<T, N>
const & shape)
379 python_ptr tuple(PyTuple_New(N), python_ptr::keep_count);
380 pythonToCppException(tuple);
381 for(
unsigned int k=0; k<N; ++k)
383 PyTuple_SET_ITEM((PyTupleObject *)tuple.get(), k, pythonFromData(shape[k]).release());
389 python_ptr shapeToPythonTuple(ArrayVectorView<T>
const & shape)
391 python_ptr tuple(PyTuple_New(shape.size()), python_ptr::keep_count);
392 pythonToCppException(tuple);
393 for(
unsigned int k=0; k<shape.size(); ++k)
395 PyTuple_SET_ITEM((PyTupleObject *)tuple.get(), k, pythonFromData(shape[k]).release());
404 PyThreadState * save_;
407 PyAllowThreads(PyAllowThreads
const &);
408 PyAllowThreads & operator=(PyAllowThreads
const &);
412 : save_(PyEval_SaveThread())
417 PyEval_RestoreThread(save_);
423 #endif // VIGRA_PYTHON_UTILITY_HXX Definition: array_vector.hxx:954
Definition: accessor.hxx:43
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