/usr/include/vigra/python_utility.hxx is in libvigraimpex-dev 1.9.0+dfsg-10+b2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | /************************************************************************/
/* */
/* Copyright 2009 by Ullrich Koethe and Hans Meine */
/* */
/* This file is part of the VIGRA computer vision library. */
/* The VIGRA Website is */
/* http://hci.iwr.uni-heidelberg.de/vigra/ */
/* Please direct questions, bug reports, and contributions to */
/* ullrich.koethe@iwr.uni-heidelberg.de or */
/* vigra@informatik.uni-hamburg.de */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without */
/* restriction, including without limitation the rights to use, */
/* copy, modify, merge, publish, distribute, sublicense, and/or */
/* sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following */
/* conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the */
/* Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
/* OTHER DEALINGS IN THE SOFTWARE. */
/* */
/************************************************************************/
#ifndef VIGRA_PYTHON_UTILITY_HXX
#define VIGRA_PYTHON_UTILITY_HXX
#include <Python.h>
#include <algorithm>
#include <string>
#include "vigra/error.hxx"
#include "vigra/tinyvector.hxx"
namespace vigra {
template <class PYOBJECT_PTR>
void pythonToCppException(PYOBJECT_PTR obj)
{
if(obj != 0)
return;
PyObject * type, * value, * trace;
PyErr_Fetch(&type, &value, &trace);
if(type == 0)
return;
std::string message(((PyTypeObject *)type)->tp_name);
if(PyString_Check(value))
{
message += std::string(": ") + PyString_AS_STRING(value);
}
Py_XDECREF(type);
Py_XDECREF(value);
Py_XDECREF(trace);
throw std::runtime_error(message.c_str());
}
/********************************************************/
/* */
/* python_ptr */
/* */
/********************************************************/
class python_ptr
{
private:
PyObject * ptr_;
public:
typedef PyObject element_type;
typedef PyObject value_type;
typedef PyObject * pointer;
typedef PyObject & reference;
enum refcount_policy { increment_count, borrowed_reference = increment_count,
keep_count, new_reference = keep_count };
explicit python_ptr(pointer p = 0, refcount_policy rp = increment_count)
: ptr_( p )
{
if(rp == increment_count)
{
Py_XINCREF(ptr_);
}
}
python_ptr(python_ptr const & p)
: ptr_(p.ptr_)
{
Py_XINCREF(ptr_);
}
python_ptr & operator=(pointer p)
{
reset(p);
return *this;
}
python_ptr & operator=(python_ptr const & r)
{
reset(r.ptr_);
return *this;
}
~python_ptr()
{
reset();
}
void reset(pointer p = 0, refcount_policy rp = increment_count)
{
if(p == ptr_)
return;
if(rp == increment_count)
{
Py_XINCREF(p);
}
Py_XDECREF(ptr_);
ptr_ = p;
}
pointer release(bool return_borrowed_reference = false)
{
pointer p = ptr_;
ptr_ = 0;
if(return_borrowed_reference)
{
Py_XDECREF(p);
}
return p;
}
reference operator* () const
{
vigra_precondition(ptr_ != 0, "python_ptr::operator*(): Cannot dereference NULL pointer.");
return *ptr_;
}
pointer operator-> () const
{
vigra_precondition(ptr_ != 0, "python_ptr::operator->(): Cannot dereference NULL pointer.");
return ptr_;
}
pointer ptr() const
{
return ptr_;
}
pointer get() const
{
return ptr_;
}
operator pointer() const
{
return ptr_;
}
bool operator! () const
{
return ptr_ == 0;
}
bool unique() const
{
return ptr_ && ptr_->ob_refcnt == 1;
}
void swap(python_ptr & other)
{
std::swap(ptr_, other.ptr_);
}
bool operator==(python_ptr const & p) const
{
return ptr_ == p.ptr_;
}
bool operator==(pointer p) const
{
return ptr_ == p;
}
bool operator!=(python_ptr const & p) const
{
return ptr_ != p.ptr_;
}
bool operator!=(pointer p) const
{
return ptr_ != p;
}
};
inline void swap(python_ptr & a, python_ptr & b)
{
a.swap(b);
}
/****************************************************************/
inline python_ptr
makePythonDictionary(char const * k1 = 0, PyObject * a1 = 0,
char const * k2 = 0, PyObject * a2 = 0,
char const * k3 = 0, PyObject * a3 = 0)
{
python_ptr dict(PyDict_New(), python_ptr::keep_count);
pythonToCppException(dict);
if(k1 && a1)
PyDict_SetItemString(dict, k1, a1);
if(k2 && a2)
PyDict_SetItemString(dict, k2, a2);
if(k3 && a3)
PyDict_SetItemString(dict, k3, a3);
return dict;
}
/****************************************************************/
inline python_ptr pythonFromData(bool t)
{
python_ptr res(PyBool_FromLong(t ? 1 : 0), python_ptr::keep_count);
pythonToCppException(res);
return res;
}
inline python_ptr pythonFromData(std::string const & s)
{
python_ptr res(PyString_FromString(s.c_str()), python_ptr::keep_count);
pythonToCppException(res);
return res;
}
inline python_ptr pythonFromData(long long t)
{
python_ptr res;
if(t > (long long)NumericTraits<long>::max() || t < (long long)NumericTraits<long>::min())
res = python_ptr(PyLong_FromLongLong(t), python_ptr::keep_count);
else
res = python_ptr(PyInt_FromLong((long)t), python_ptr::keep_count);
pythonToCppException(res);
return res;
}
inline python_ptr pythonFromData(unsigned long long t)
{
python_ptr res;
if(t > (unsigned long long)NumericTraits<long>::max())
res = python_ptr(PyLong_FromUnsignedLongLong(t), python_ptr::keep_count);
else
res = python_ptr(PyInt_FromLong((long)t), python_ptr::keep_count);
pythonToCppException(res);
return res;
}
#define VIGRA_PYTHON_FROM_DATA(type, fct, cast_type) \
inline python_ptr pythonFromData(type t) \
{ \
python_ptr res(fct((cast_type)t), python_ptr::keep_count); \
pythonToCppException(res); \
return res; \
}
VIGRA_PYTHON_FROM_DATA(signed char, PyInt_FromLong, long)
VIGRA_PYTHON_FROM_DATA(unsigned char, PyInt_FromLong, long)
VIGRA_PYTHON_FROM_DATA(short, PyInt_FromLong, long)
VIGRA_PYTHON_FROM_DATA(unsigned short, PyInt_FromLong, long)
VIGRA_PYTHON_FROM_DATA(long, PyInt_FromLong, long)
VIGRA_PYTHON_FROM_DATA(unsigned long, PyInt_FromSize_t, size_t)
VIGRA_PYTHON_FROM_DATA(int, PyInt_FromSsize_t, Py_ssize_t)
VIGRA_PYTHON_FROM_DATA(unsigned int, PyInt_FromSize_t, size_t)
VIGRA_PYTHON_FROM_DATA(float, PyFloat_FromDouble, double)
VIGRA_PYTHON_FROM_DATA(double, PyFloat_FromDouble, double)
VIGRA_PYTHON_FROM_DATA(char const *, PyString_FromString, char const *)
#undef VIGRA_PYTHON_FROM_DATA
/****************************************************************/
#define VIGRA_DATA_FROM_PYTHON(type, check, extract) \
inline type dataFromPython(PyObject * data, type const & defaultVal) \
{ \
return data && check(data) \
? (type)extract(data) \
: defaultVal; \
}
VIGRA_DATA_FROM_PYTHON(signed char, PyInt_Check, PyInt_AsLong)
VIGRA_DATA_FROM_PYTHON(unsigned char, PyInt_Check, PyInt_AsLong)
VIGRA_DATA_FROM_PYTHON(short, PyInt_Check, PyInt_AsLong)
VIGRA_DATA_FROM_PYTHON(unsigned short, PyInt_Check, PyInt_AsLong)
VIGRA_DATA_FROM_PYTHON(long, PyInt_Check, PyInt_AsLong)
VIGRA_DATA_FROM_PYTHON(unsigned long, PyInt_Check, PyInt_AsUnsignedLongMask)
VIGRA_DATA_FROM_PYTHON(int, PyInt_Check, PyInt_AsLong)
VIGRA_DATA_FROM_PYTHON(unsigned int, PyInt_Check, PyInt_AsUnsignedLongMask)
VIGRA_DATA_FROM_PYTHON(long long, PyInt_Check, PyInt_AsSsize_t)
VIGRA_DATA_FROM_PYTHON(unsigned long long, PyInt_Check, PyInt_AsUnsignedLongLongMask)
VIGRA_DATA_FROM_PYTHON(float, PyFloat_Check, PyFloat_AsDouble)
VIGRA_DATA_FROM_PYTHON(double, PyFloat_Check, PyFloat_AsDouble)
inline std::string dataFromPython(PyObject * data, const char * defaultVal)
{
return data && PyString_Check(data)
? std::string(PyString_AsString(data))
: std::string(defaultVal);
}
inline std::string dataFromPython(PyObject * data, std::string const & defaultVal)
{
return data && PyString_Check(data)
? std::string(PyString_AsString(data))
: defaultVal;
}
inline python_ptr dataFromPython(PyObject * data, python_ptr defaultVal)
{
return data
? python_ptr(data)
: defaultVal;
}
#undef VIGRA_DATA_FROM_PYTHON
/****************************************************************/
template <class T>
T pythonGetAttr(PyObject * obj, const char * key, T defaultValue)
{
if(!obj)
return defaultValue;
python_ptr k(PyString_FromString(key), python_ptr::keep_count);
pythonToCppException(k);
python_ptr pres(PyObject_GetAttr(obj, k), python_ptr::keep_count);
if(!pres)
PyErr_Clear();
return dataFromPython(pres, defaultValue);
}
inline std::string
pythonGetAttr(PyObject * obj, const char * key, const char * defaultValue)
{
if(!obj)
return std::string(defaultValue);
python_ptr k(PyString_FromString(key), python_ptr::keep_count);
pythonToCppException(k);
python_ptr pres(PyObject_GetAttr(obj, k), python_ptr::keep_count);
if(!pres)
PyErr_Clear();
return dataFromPython(pres, defaultValue);
}
/****************************************************************/
template <class T, int N>
python_ptr shapeToPythonTuple(TinyVector<T, N> const & shape)
{
python_ptr tuple(PyTuple_New(N), python_ptr::keep_count);
pythonToCppException(tuple);
for(unsigned int k=0; k<N; ++k)
{
PyTuple_SET_ITEM((PyTupleObject *)tuple.get(), k, pythonFromData(shape[k]).release());
}
return tuple;
}
template <class T>
python_ptr shapeToPythonTuple(ArrayVectorView<T> const & shape)
{
python_ptr tuple(PyTuple_New(shape.size()), python_ptr::keep_count);
pythonToCppException(tuple);
for(unsigned int k=0; k<shape.size(); ++k)
{
PyTuple_SET_ITEM((PyTupleObject *)tuple.get(), k, pythonFromData(shape[k]).release());
}
return tuple;
}
/****************************************************************/
class PyAllowThreads
{
PyThreadState * save_;
// make it non-copyable
PyAllowThreads(PyAllowThreads const &);
PyAllowThreads & operator=(PyAllowThreads const &);
public:
PyAllowThreads()
: save_(PyEval_SaveThread())
{}
~PyAllowThreads()
{
PyEval_RestoreThread(save_);
}
};
} // namespace vigra
#endif // VIGRA_PYTHON_UTILITY_HXX
|