/usr/include/openturns/PythonWrappingFunctions.hxx is in libopenturns-dev 0.15-2.
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 416 417 418 419 420 | // -*- C++ -*-
/**
* @file PythonWrappingFunctions.hxx
* @brief This file provides functions to ease Python wrapping
*
* (C) Copyright 2005-2011 EDF
*
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*
*
* \author $LastChangedBy: schueller $
* \date $LastChangedDate: 2009-09-14 14:39:35 +0200 (Mon, 14 Sep 2009) $
*/
#ifndef OPENTURNS_PYTHONWRAPPINGFUNCTIONS_HXX
#define OPENTURNS_PYTHONWRAPPINGFUNCTIONS_HXX
#include "Python.h"
#include "OT.hxx"
namespace OpenTURNS {
/** These templates are just declared, not defined. Only specializations are. */
template <class CPP_Type> struct traitsPythonType;
template <class PYTHON_Type> static inline int isAPython(PyObject * pyObj);
template <class PYTHON_Type> static inline String namePython();
template <class PYTHON_Type, class CPP_Type> static inline CPP_Type convert(PyObject * pyObj);
template <class PYTHON_Type> static inline void check(PyObject * pyObj);
template <class PYTHON_Type, class CPP_Type> static inline CPP_Type checkAndConvert(PyObject * pyObj);
template <class T> static inline T * buildObjectFromPySequence(PyObject * pyObj);
/** Specializations */
/* PyObject */
struct _PyObject_ {};
template <>
inline
int
isAPython<_PyObject_>(PyObject * pyObj)
{
return 1;
}
template <>
inline
String
namePython<_PyObject_>()
{
return "object";
}
/* PyBool */
struct _PyBool_ {};
template <>
inline
int
isAPython<_PyBool_>(PyObject * pyObj)
{
return PyBool_Check( pyObj );
}
template <>
inline
String
namePython<_PyBool_>()
{
return "bool";
}
template <>
struct traitsPythonType<Bool>
{
typedef _PyBool_ Type;
};
template <>
inline
Bool
convert<_PyBool_,Bool>(PyObject * pyObj)
{
return pyObj == Py_True;
}
/* PyInt */
struct _PyInt_ {};
template <>
inline
int
isAPython<_PyInt_>(PyObject * pyObj)
{
return PyInt_Check( pyObj );
}
template <>
inline
String
namePython<_PyInt_>()
{
return "integer";
}
template <>
struct traitsPythonType<UnsignedLong>
{
typedef _PyInt_ Type;
};
template <>
inline
UnsignedLong
convert<_PyInt_,UnsignedLong>(PyObject * pyObj)
{
return PyInt_AsUnsignedLongMask( pyObj );
}
/* PyFloat */
struct _PyFloat_ {};
template <>
inline
int
isAPython<_PyFloat_>(PyObject * pyObj)
{
return PyNumber_Check( pyObj );
}
template <>
inline
String
namePython<_PyFloat_>()
{
return "double";
}
template <>
struct traitsPythonType<NumericalScalar>
{
typedef _PyFloat_ Type;
};
template <>
inline
NumericalScalar
convert<_PyFloat_,NumericalScalar>(PyObject * pyObj)
{
return PyFloat_AsDouble( pyObj );
}
/* PyString */
struct _PyString_ {};
template <>
inline
int
isAPython<_PyString_>(PyObject * pyObj)
{
return PyString_Check( pyObj );
}
template <>
inline
String
namePython<_PyString_>()
{
return "string";
}
template <>
struct traitsPythonType<String>
{
typedef _PyString_ Type;
};
template <>
inline
String
convert<_PyString_,String>(PyObject * pyObj)
{
return PyString_AsString( pyObj );
}
/* PySequence */
struct _PySequence_ {};
template <>
inline
int
isAPython<_PySequence_>(PyObject * pyObj)
{
return PySequence_Check( pyObj );
}
template <>
inline
String
namePython<_PySequence_>()
{
return "sequence object";
}
template <>
struct traitsPythonType<NumericalComplex>
{
typedef _PySequence_ Type;
};
template <>
inline
NumericalComplex
convert<_PySequence_,NumericalComplex>(PyObject * pyObj)
{
check<_PySequence_>( pyObj );
PyObject * newPyObj = PySequence_Fast( pyObj, "" );
if (PySequence_Fast_GET_SIZE( newPyObj ) != 2)
throw Base::Common::InvalidArgumentException(HERE) << "Sequence passed as argument is not a pair (NumericalScalar, NumericalScalar)";
PyObject * item_0 = PySequence_Fast_GET_ITEM( newPyObj, 0 );
check<_PyFloat_>( item_0 );
PyObject * item_1 = PySequence_Fast_GET_ITEM( newPyObj, 1 );
check<_PyFloat_>( item_1 );
Py_XDECREF( newPyObj );
return NumericalComplex( convert<_PyFloat_,NumericalScalar>( item_0 ),
convert<_PyFloat_,NumericalScalar>( item_1 ) );
}
template <class PYTHON_Type>
static inline
int
isAPythonSequenceOf(PyObject * pyObj)
{
int ok = isAPython<_PySequence_>( pyObj ) && ( ! PyString_Check( pyObj ) );
if ( ok ) {
PyObject * newPyObj( PySequence_Fast( pyObj, "" ) );
const UnsignedLong size( PySequence_Fast_GET_SIZE( newPyObj ) );
for( UnsignedLong i = 0; ok && (i < size); ++ i ) {
PyObject * elt( PySequence_Fast_GET_ITEM( newPyObj, i ) );
int elt_ok = isAPython<PYTHON_Type>( elt );
ok *= elt_ok;
}
Py_XDECREF( newPyObj );
}
return ok;
}
template <class PYTHON_Type>
static inline
void
check(PyObject * pyObj)
{
if (! isAPython<PYTHON_Type>( pyObj )) {
throw Base::Common::InvalidArgumentException(HERE) << "Object passed as argument is not a " << namePython<PYTHON_Type>();
}
}
template <class PYTHON_Type, class CPP_Type>
static inline
CPP_Type
checkAndConvert(PyObject * pyObj)
{
check<PYTHON_Type>( pyObj );
return convert<PYTHON_Type,CPP_Type>( pyObj );
}
template <class T>
static inline
Base::Type::Collection<T> *
buildCollectionFromPySequence(PyObject * pyObj, int sz = 0)
{
check<_PySequence_>( pyObj );
PyObject * newPyObj = PySequence_Fast( pyObj, "" );
const UnsignedLong size = PySequence_Fast_GET_SIZE( newPyObj );
if ((sz != 0) && (sz != (int)size)) {
throw Base::Common::InvalidArgumentException(HERE) << "Sequence object has incorrect size " << size << ". Must be " << sz << ".";
}
Base::Type::Collection<T> * p_coll = new Base::Type::Collection<T>( size );
for(UnsignedLong i=0; i<size; ++i) {
PyObject * elt = PySequence_Fast_GET_ITEM( newPyObj, i );
check<typename traitsPythonType<T>::Type>( elt );
(*p_coll)[i] = convert<typename traitsPythonType<T>::Type,T>( elt );
}
Py_XDECREF( newPyObj );
return p_coll;
}
template <>
struct traitsPythonType<OpenTURNS::Base::Type::NumericalPoint>
{
typedef _PySequence_ Type;
};
template <>
inline
OpenTURNS::Base::Type::NumericalPoint
convert<_PySequence_,OpenTURNS::Base::Type::NumericalPoint>(PyObject * pyObj)
{
OpenTURNS::Pointer<OpenTURNS::Base::Type::Collection<OpenTURNS::NumericalScalar> > ptr = buildCollectionFromPySequence<OpenTURNS::NumericalScalar>( pyObj );
return OpenTURNS::Base::Type::NumericalPoint( *ptr );
}
template <>
struct traitsPythonType<OpenTURNS::Base::Stat::NumericalSample>
{
typedef _PySequence_ Type;
};
template <>
inline
OpenTURNS::Base::Stat::NumericalSample
convert<_PySequence_,OpenTURNS::Base::Stat::NumericalSample>(PyObject * pyObj)
{
OpenTURNS::Pointer<OpenTURNS::Base::Type::Collection<OpenTURNS::Base::Type::NumericalPoint> > ptr = buildCollectionFromPySequence<OpenTURNS::Base::Type::NumericalPoint>( pyObj );
return OpenTURNS::Base::Stat::NumericalSample( *ptr );
}
template <>
struct traitsPythonType<OpenTURNS::Base::Type::Indices>
{
typedef _PySequence_ Type;
};
template <>
inline
OpenTURNS::Base::Type::Indices
convert<_PySequence_,OpenTURNS::Base::Type::Indices>(PyObject * pyObj)
{
OpenTURNS::Pointer<OpenTURNS::Base::Type::Collection<OpenTURNS::UnsignedLong> > ptr = buildCollectionFromPySequence<OpenTURNS::UnsignedLong>( pyObj );
return OpenTURNS::Base::Type::Indices( ptr->begin(), ptr->end() );
}
template <>
struct traitsPythonType<OpenTURNS::WrapperDataFile>
{
typedef _PySequence_ Type;
};
template <>
inline
OpenTURNS::WrapperDataFile
convert<_PySequence_,OpenTURNS::WrapperDataFile>(PyObject * pyObj)
{
return OpenTURNS::WrapperDataFile();
}
template <>
struct traitsPythonType<OpenTURNS::WrapperDataVariable>
{
typedef _PySequence_ Type;
};
template <>
inline
OpenTURNS::WrapperDataVariable
convert<_PySequence_,OpenTURNS::WrapperDataVariable>(PyObject * pyObj)
{
return OpenTURNS::WrapperDataVariable();
}
} /* namespace OpenTURNS */
#endif /* OPENTURNS_PYTHONWRAPPINGFUNCTIONS_HXX */
|