3
* @file PythonWrappingFunctions.hxx
4
* @brief This file provides functions to ease Python wrapping
6
* (C) Copyright 2005-2010 EDF
8
* Permission to copy, use, modify, sell and distribute this software
9
* is granted provided this copyright notice appears in all copies.
10
* This software is provided "as is" without express or implied
11
* warranty, and with no claim as to its suitability for any purpose.
14
* \author $LastChangedBy: dutka $
15
* \date $LastChangedDate: 2009-09-14 14:39:35 +0200 (Mon, 14 Sep 2009) $
18
#ifndef OPENTURNS_PYTHONWRAPPINGFUNCTIONS_HXX
19
#define OPENTURNS_PYTHONWRAPPINGFUNCTIONS_HXX
22
#include "Collection.hxx"
23
#include "Histogram.hxx"
27
/** These templates are just declared, not defined. Only specializations are. */
28
template <class CPP_Type> struct traitsPythonType;
29
template <class PYTHON_Type> static inline int isAPython(PyObject * pyObj);
30
template <class PYTHON_Type> static inline String namePython();
31
template <class PYTHON_Type, class CPP_Type> static inline CPP_Type convert(PyObject * pyObj);
32
template <class PYTHON_Type> static inline void check(PyObject * pyObj);
33
template <class PYTHON_Type, class CPP_Type> static inline CPP_Type checkAndConvert(PyObject * pyObj);
34
template <class T> static inline T * buildObjectFromPySequence(PyObject * pyObj);
38
/** Specializations */
47
isAPython<_PyObject_>(PyObject * pyObj)
55
namePython<_PyObject_>()
69
isAPython<_PyBool_>(PyObject * pyObj)
71
return PyBool_Check( pyObj );
77
namePython<_PyBool_>()
83
struct traitsPythonType<Bool>
85
typedef _PyBool_ Type;
91
convert<_PyBool_,Bool>(PyObject * pyObj)
93
return pyObj == Py_True;
106
isAPython<_PyInt_>(PyObject * pyObj)
108
return PyInt_Check( pyObj );
114
namePython<_PyInt_>()
120
struct traitsPythonType<UnsignedLong>
122
typedef _PyInt_ Type;
128
convert<_PyInt_,UnsignedLong>(PyObject * pyObj)
130
return PyInt_AsUnsignedLongMask( pyObj );
143
isAPython<_PyFloat_>(PyObject * pyObj)
145
return PyNumber_Check( pyObj );
151
namePython<_PyFloat_>()
157
struct traitsPythonType<NumericalScalar>
159
typedef _PyFloat_ Type;
165
convert<_PyFloat_,NumericalScalar>(PyObject * pyObj)
167
return PyFloat_AsDouble( pyObj );
174
struct _PyString_ {};
179
isAPython<_PyString_>(PyObject * pyObj)
181
return PyString_Check( pyObj );
187
namePython<_PyString_>()
193
struct traitsPythonType<String>
195
typedef _PyString_ Type;
201
convert<_PyString_,String>(PyObject * pyObj)
203
return PyString_AsString( pyObj );
211
struct _PySequence_ {};
216
isAPython<_PySequence_>(PyObject * pyObj)
218
return PySequence_Check( pyObj );
224
namePython<_PySequence_>()
226
return "sequence object";
230
struct traitsPythonType<NumericalComplex>
232
typedef _PySequence_ Type;
238
convert<_PySequence_,NumericalComplex>(PyObject * pyObj)
240
check<_PySequence_>( pyObj );
241
if (PySequence_Fast_GET_SIZE( pyObj ) != 2)
242
throw Base::Common::InvalidArgumentException(HERE) << "Sequence passed as argument is not a pair (NumericalScalar, NumericalScalar)";
243
PyObject * item_0 = PySequence_Fast_GET_ITEM( pyObj, 0 );
244
check<_PyFloat_>( item_0 );
245
PyObject * item_1 = PySequence_Fast_GET_ITEM( pyObj, 1 );
246
check<_PyFloat_>( item_1 );
247
return NumericalComplex( convert<_PyFloat_,NumericalScalar>( item_0 ),
248
convert<_PyFloat_,NumericalScalar>( item_1 ) );
255
template <class PYTHON_Type>
258
check(PyObject * pyObj)
260
if (! isAPython<PYTHON_Type>( pyObj )) {
261
throw Base::Common::InvalidArgumentException(HERE) << "Object passed as argument is not a " << namePython<PYTHON_Type>();
266
template <class PYTHON_Type, class CPP_Type>
269
checkAndConvert(PyObject * pyObj)
271
check<PYTHON_Type>( pyObj );
272
return convert<PYTHON_Type,CPP_Type>( pyObj );
281
Base::Type::Collection<T> *
282
buildCollectionFromPySequence(PyObject * pyObj, int sz = 0)
284
check<_PySequence_>( pyObj );
286
const UnsignedLong size = PySequence_Fast_GET_SIZE( pyObj );
287
if ((sz != 0) && (sz != size)) {
288
throw Base::Common::InvalidArgumentException(HERE) << "Sequence object has incorrect size " << size << ". Must be " << sz << ".";
290
Base::Type::Collection<T> * p_coll = new Base::Type::Collection<T>( size );
292
for(UnsignedLong i=0; i<size; ++i) {
293
PyObject * elt = PySequence_Fast_GET_ITEM( pyObj, i );
294
check<typename traitsPythonType<T>::Type>( elt );
295
(*p_coll)[i] = convert<typename traitsPythonType<T>::Type,T>( elt );
304
} /* namespace OpenTURNS */
306
#endif /* OPENTURNS_PYTHONWRAPPINGFUNCTIONS_HXX */