~ubuntu-branches/ubuntu/maverick/openturns/maverick

« back to all changes in this revision

Viewing changes to python/src/PythonWrappingFunctions.hxx

  • Committer: Bazaar Package Importer
  • Author(s): Fabrice Coutadeur
  • Date: 2010-05-10 17:27:55 UTC
  • mfrom: (1.1.4 upstream) (5.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20100510172755-cb5ynskknqqi5rhp
Tags: 0.13.2-2ubuntu1
* Merge with Debian testing. No changes left.
* ubuntu_fix-python-2.6.patch: fix detection of python 2.6 libs, to not use
  LOCALMODLIBS. This pulls a dependency on SSL and makes the package FTBFS.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//                                               -*- C++ -*-
 
2
/**
 
3
 * @file  PythonWrappingFunctions.hxx
 
4
 * @brief This file provides functions to ease Python wrapping
 
5
 *
 
6
 * (C) Copyright 2005-2010 EDF
 
7
 *
 
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.
 
12
 *
 
13
 *
 
14
 * \author $LastChangedBy: dutka $
 
15
 * \date   $LastChangedDate: 2009-09-14 14:39:35 +0200 (Mon, 14 Sep 2009) $
 
16
 */
 
17
 
 
18
#ifndef OPENTURNS_PYTHONWRAPPINGFUNCTIONS_HXX
 
19
#define OPENTURNS_PYTHONWRAPPINGFUNCTIONS_HXX
 
20
 
 
21
#include "Python.h"
 
22
#include "Collection.hxx"
 
23
#include "Histogram.hxx"
 
24
 
 
25
namespace OpenTURNS { 
 
26
 
 
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);
 
35
 
 
36
 
 
37
 
 
38
  /** Specializations */
 
39
 
 
40
 
 
41
  /* PyObject */
 
42
  struct _PyObject_ {};
 
43
 
 
44
  template <>
 
45
  inline
 
46
  int
 
47
  isAPython<_PyObject_>(PyObject * pyObj)
 
48
  {
 
49
    return 1;
 
50
  }
 
51
 
 
52
  template <>
 
53
  inline
 
54
  String
 
55
  namePython<_PyObject_>()
 
56
  {
 
57
    return "object";
 
58
  }
 
59
 
 
60
 
 
61
 
 
62
 
 
63
  /* PyBool */
 
64
  struct _PyBool_ {};
 
65
 
 
66
  template <>
 
67
  inline
 
68
  int
 
69
  isAPython<_PyBool_>(PyObject * pyObj)
 
70
  {
 
71
    return PyBool_Check( pyObj );
 
72
  }
 
73
 
 
74
  template <>
 
75
  inline
 
76
  String
 
77
  namePython<_PyBool_>()
 
78
  {
 
79
    return "bool";
 
80
  }
 
81
 
 
82
  template <>
 
83
  struct traitsPythonType<Bool>
 
84
  {
 
85
    typedef _PyBool_ Type;
 
86
  };
 
87
 
 
88
  template <>
 
89
  inline
 
90
  Bool
 
91
  convert<_PyBool_,Bool>(PyObject * pyObj)
 
92
  {
 
93
    return pyObj == Py_True;
 
94
  }
 
95
 
 
96
 
 
97
 
 
98
 
 
99
 
 
100
  /* PyInt */
 
101
  struct _PyInt_ {};
 
102
 
 
103
  template <>
 
104
  inline
 
105
  int
 
106
  isAPython<_PyInt_>(PyObject * pyObj)
 
107
  {
 
108
    return PyInt_Check( pyObj );
 
109
  }
 
110
 
 
111
  template <>
 
112
  inline
 
113
  String
 
114
  namePython<_PyInt_>()
 
115
  {
 
116
    return "integer";
 
117
  }
 
118
 
 
119
  template <>
 
120
  struct traitsPythonType<UnsignedLong>
 
121
  {
 
122
    typedef _PyInt_ Type;
 
123
  };
 
124
 
 
125
  template <>
 
126
  inline
 
127
  UnsignedLong
 
128
  convert<_PyInt_,UnsignedLong>(PyObject * pyObj)
 
129
  {
 
130
    return PyInt_AsUnsignedLongMask( pyObj );
 
131
  }
 
132
 
 
133
 
 
134
 
 
135
 
 
136
 
 
137
  /* PyFloat */
 
138
  struct _PyFloat_ {};
 
139
 
 
140
  template <>
 
141
  inline
 
142
  int
 
143
  isAPython<_PyFloat_>(PyObject * pyObj)
 
144
  {
 
145
    return PyNumber_Check( pyObj );
 
146
  }
 
147
 
 
148
  template <>
 
149
  inline
 
150
  String
 
151
  namePython<_PyFloat_>()
 
152
  {
 
153
    return "double";
 
154
  }
 
155
 
 
156
  template <>
 
157
  struct traitsPythonType<NumericalScalar>
 
158
  {
 
159
    typedef _PyFloat_ Type;
 
160
  };
 
161
 
 
162
  template <>
 
163
  inline
 
164
  NumericalScalar
 
165
  convert<_PyFloat_,NumericalScalar>(PyObject * pyObj)
 
166
  {
 
167
    return PyFloat_AsDouble( pyObj );
 
168
  }
 
169
 
 
170
 
 
171
 
 
172
 
 
173
  /* PyString */
 
174
  struct _PyString_ {};
 
175
 
 
176
  template <>
 
177
  inline
 
178
  int
 
179
  isAPython<_PyString_>(PyObject * pyObj)
 
180
  {
 
181
    return PyString_Check( pyObj );
 
182
  }
 
183
 
 
184
  template <>
 
185
  inline
 
186
  String
 
187
  namePython<_PyString_>()
 
188
  {
 
189
    return "string";
 
190
  }
 
191
 
 
192
  template <>
 
193
  struct traitsPythonType<String>
 
194
  {
 
195
    typedef _PyString_ Type;
 
196
  };
 
197
 
 
198
  template <>
 
199
  inline
 
200
  String
 
201
  convert<_PyString_,String>(PyObject * pyObj)
 
202
  {
 
203
    return PyString_AsString( pyObj );
 
204
  }
 
205
 
 
206
 
 
207
 
 
208
 
 
209
 
 
210
  /* PySequence */
 
211
  struct _PySequence_ {};
 
212
 
 
213
  template <>
 
214
  inline
 
215
  int
 
216
  isAPython<_PySequence_>(PyObject * pyObj)
 
217
  {
 
218
    return PySequence_Check( pyObj );
 
219
  }
 
220
 
 
221
  template <>
 
222
  inline
 
223
  String
 
224
  namePython<_PySequence_>()
 
225
  {
 
226
    return "sequence object";
 
227
  }
 
228
 
 
229
  template <>
 
230
  struct traitsPythonType<NumericalComplex>
 
231
  {
 
232
    typedef _PySequence_ Type;
 
233
  };
 
234
 
 
235
  template <>
 
236
  inline
 
237
  NumericalComplex
 
238
  convert<_PySequence_,NumericalComplex>(PyObject * pyObj)
 
239
  {
 
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 ) );
 
249
  }
 
250
 
 
251
 
 
252
 
 
253
 
 
254
 
 
255
  template <class PYTHON_Type>
 
256
  static inline
 
257
  void
 
258
  check(PyObject * pyObj)
 
259
  {
 
260
    if (! isAPython<PYTHON_Type>( pyObj )) {
 
261
      throw Base::Common::InvalidArgumentException(HERE) << "Object passed as argument is not a " << namePython<PYTHON_Type>();
 
262
    }
 
263
  }
 
264
 
 
265
 
 
266
  template <class PYTHON_Type, class CPP_Type>
 
267
  static inline
 
268
  CPP_Type
 
269
  checkAndConvert(PyObject * pyObj)
 
270
  {
 
271
    check<PYTHON_Type>( pyObj );
 
272
    return convert<PYTHON_Type,CPP_Type>( pyObj );
 
273
  }
 
274
 
 
275
 
 
276
 
 
277
 
 
278
 
 
279
  template <class T>
 
280
  static inline
 
281
  Base::Type::Collection<T> *
 
282
  buildCollectionFromPySequence(PyObject * pyObj, int sz = 0)
 
283
  {
 
284
    check<_PySequence_>( pyObj );
 
285
 
 
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 << ".";
 
289
    }
 
290
    Base::Type::Collection<T> * p_coll = new Base::Type::Collection<T>( size );
 
291
 
 
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 );
 
296
    }
 
297
 
 
298
    return p_coll;
 
299
  }
 
300
 
 
301
  
 
302
 
 
303
 
 
304
} /* namespace OpenTURNS */
 
305
 
 
306
#endif /* OPENTURNS_PYTHONWRAPPINGFUNCTIONS_HXX */