-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpythonHelpers.hpp
More file actions
344 lines (306 loc) · 9.76 KB
/
pythonHelpers.hpp
File metadata and controls
344 lines (306 loc) · 9.76 KB
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
/*
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Copyright (c) 2019, Lawrence Livermore National Security, LLC.
*
* Produced at the Lawrence Livermore National Laboratory
*
* LLNL-CODE-746361
*
* All rights reserved. See COPYRIGHT for details.
*
* This file is part of the GEOSX Simulation Framework.
*
* GEOSX is a free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License (as published by the
* Free Software Foundation) version 2.1 dated February 1999.
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
#pragma once
/**
* @file pythonHelpers.hpp
*/
// Source includes
#include "pythonForwardDeclarations.hpp"
#include "../system.hpp"
#include "../Macros.hpp"
#include "../limits.hpp"
// system includes
#include <vector>
#include <stdexcept>
/**
* @brief Raise a Python exception if @c CONDITION is met.
* @param CONDITION The condition to check.
* @param TYPE The Type of Python exception to raise.
* @param MSG The message to give the exception.
* @param RET The return value if there was an error.
*/
#if defined(PyObject_HEAD)
#define PYTHON_ERROR_IF( CONDITION, TYPE, MSG, RET ) \
do \
{ \
if( CONDITION ) \
{ \
std::ostringstream __oss; \
__oss << "***** ERROR\n"; \
__oss << "***** LOCATION: " LOCATION "\n"; \
__oss << "***** Controlling expression (should be false): " STRINGIZE( EXP ) "\n"; \
__oss << MSG << "\n"; \
__oss << LvArray::system::stackTrace( true ); \
PyErr_SetString( TYPE, __oss.str().c_str() ); \
return RET; \
} \
} while( false )
#else
#define PYTHON_ERROR_IF( CONDITION, TYPE, MSG, RET ) \
static_assert( false, "You are attempting to use PYTHON_ERROR_IF but haven't yet included Python.hpp" )
#endif
/// @cond DO_NOT_DOCUMENT
// UNCRUSTIFY-OFF
/// Doxygen and uncrustify don't like this macro/pragma nest.
/**
* @brief Begin mixing designated and non-designated initializers in the same initializer list.
*/
#if defined( __clang_version__ )
#if defined( __CUDACC__ )
#define BEGIN_ALLOW_DESIGNATED_INITIALIZERS \
_Pragma( "GCC diagnostic push" ) \
_Pragma( "GCC diagnostic ignored \"-Wc99-extensions\"") \
_Pragma( "GCC diagnostic ignored \"-Wgnu-designator\"")
#if __has_warning( "-Wc99-designator" )
_Pragma( "GCC diagnostic ignored \"-Wc99-designator\"")
#endif
#else
#define BEGIN_ALLOW_DESIGNATED_INITIALIZERS \
_Pragma( "GCC diagnostic push" ) \
_Pragma( "GCC diagnostic ignored \"-Wc99-designator\"")
#if __has_warning( "-Wmissing-designated-field-initializers" )
_Pragma( "GCC diagnostic ignored \"-Wmissing-designated-field-initializers\"")
#endif
#endif
#elif defined( __GNUC__ )
#define BEGIN_ALLOW_DESIGNATED_INITIALIZERS \
_Pragma( "GCC diagnostic push" ) \
_Pragma( "GCC diagnostic ignored \"-Wpedantic\"" ) \
_Pragma( "GCC diagnostic ignored \"-Wmissing-field-initializers\"" )
#if __GNUC__ > 11
_Pragma( "GCC diagnostic ignored \"-Wc++20-extensions\"")
#endif
#endif
/**
* @brief End mixing designated and non-designated initializers in the same initializer list.
*/
#define END_ALLOW_DESIGNATED_INITIALIZERS \
_Pragma( "GCC diagnostic pop" )
// UNCRUSTIFY-ON
/// @endcond DO_NOT_DOCUMENT
namespace LvArray
{
namespace python
{
namespace internal
{
/**
* @brief A wrapper around Py_XINCREF.
* @param obj The object to increase the reference count of, may be a nullptr.
*/
void xincref( PyObject * const obj );
/**
* @brief A wrapper around Py_XDECREF.
* @param obj The object to decrease the reference count of, may be a nullptr.
*/
void xdecref( PyObject * const obj );
/**
* @brief Return @c true if @p obj is an instance of @p type.
* @param obj The object.
* @param type The type.
* @return @c true if @p obj is an instance of @p type.
*/
bool isInstanceOf( PyObject * const obj, PyTypeObject * type );
} // namespace internal
/**
* @enum PyModify
* @brief An enumeration of the various access policies for Python objects.
*/
enum class PyModify
{
READ_ONLY = 0,
MODIFIABLE = 1,
RESIZEABLE = 2,
};
/**
* @class PyObjectRef
* @brief A class that manages an owned Python reference with RAII semantics.
* @tparam T The type of the managed object, must be castable to a PyObject.
*/
template< typename T = PyObject >
class PyObjectRef
{
public:
/**
* @brief Create an uninitialized (nullptr) reference.
*/
PyObjectRef() = default;
/**
* @brief Take ownership of a reference to @p src.
* @param src The object to be referenced.
* @note Does not increase the reference count.
*/
PyObjectRef( T * const src ):
m_object( src )
{}
/**
* @brief Create a new reference to @p src.
* @param src The object to create a new reference to.
* @note Increases the reference count.
*/
PyObjectRef( PyObjectRef const & src )
{ *this = src; }
/**
* @brief Steal a reference from @p src.
* @param src The object to steal a reference to.
* @note Does not increase the reference count.
*/
PyObjectRef( PyObjectRef && src )
{ *this = std::move( src ); }
/**
* @brief Destructor, decreases the reference count.
*/
~PyObjectRef()
{ internal::xdecref( reinterpret_cast< PyObject * >( m_object ) ); }
/**
* @brief Create a new reference to @p src.
* @param src The object to create a new reference to.
* @return *this.
* @note Increases the reference count.
*/
PyObjectRef & operator=( PyObjectRef const & src )
{
m_object = src.m_object;
internal::xincref( reinterpret_cast< PyObject * >( src.m_object ) );
return *this;
}
/**
* @brief Steal a reference from @p src.
* @param src The object to steal a reference to.
* @return *this.
* @note Does not increase the reference count.
*/
PyObjectRef & operator=( PyObjectRef && src )
{
m_object = src.m_object;
src.m_object = nullptr;
return *this;
}
/**
* @brief Decrease the reference count to the current object and take ownership
* of a new reference.
* @param src The new object to be referenced.
* @return *this.
*/
PyObjectRef & operator=( PyObject * src )
{
internal::xdecref( reinterpret_cast< PyObject * >( m_object ) );
m_object = src;
return *this;
}
/**
* @brief Conversion operator to a T *.
* @return A pointer to the managed object.
*/
operator T *()
{ return m_object; }
/**
* @brief Return a pointer to the managed object.
* @return A pointer to the managed object.
*/
T * get() const
{ return m_object; }
/**
* @brief Return the address of the pointer to the manged object.
* @return The address of the pointer to the manged object.
* @details Useful for functions which take a PyObject** as an output parameter
* and fill it with a new reference.
*/
T * * getAddress()
{ return &m_object; }
/**
* @brief Return the address of the managed object and release ownership.
* @return The address of the managed object and release ownership.
* @details Useful for functions which return a PyObject *.
*/
T * release()
{
T * const ret = m_object;
m_object = nullptr;
return ret;
}
private:
/// A pointer to the manged object.
T * m_object = nullptr;
};
/**
* @brief Return @p obj casted to T if @p obj is an instance of @p type or @c nullptr if it is not.
* @tparam T The type to cast @p obj to.
* @param obj The object.
* @param type The type.
* @return @p obj casted to T if @p obj is an instance of @p type or @c nullptr if it is not.
*/
template< typename T >
T * convert( PyObject * const obj, PyTypeObject * const type )
{
if( internal::isInstanceOf( obj, type ) )
{
return reinterpret_cast< T * >( obj );
}
return nullptr;
}
/**
* @brief Add the Python type @p type to the module @p module.
* @param module The Python module to add @p type to.
* @param type The Python type to add to @p module.
* @param typeName The name to give the type.
* @return @c true iff the operation was successful.
*/
bool addTypeToModule( PyObject * const module,
PyTypeObject * const type,
char const * const typeName );
/**
* @brief Return a Python string copy of @c value.
* @param value The string to copy into Python.
* @return A Python string copy of @c value.
*/
PyObject * create( std::string const & value );
/**
* @brief Create and return a Python list of strings from an array of std::strings.
* The Python strings will be copies.
* @param strptr A pointer to the strings to convert, must be of length @c size.
* @param size The number of strings in @c strptr.
* @return A Python list of strings, or @c nullptr if there was an error.
*/
PyObject * createPyListOfStrings( std::string const * const strptr, long long const size );
/**
* @brief Create and return a Python list of strings from a std::vector of std::strings.
* The Python strings will be copies.
* @param vec the vector to convert.
* @return A Python list of strings, or @c nullptr if there was an error.
*/
inline PyObject * create( std::vector< std::string > const & vec )
{ return createPyListOfStrings( vec.data(), integerConversion< long long >( vec.size() ) ); }
/**
* @class PythonError
* @brief Base class for all C++ exceptions related to Python.
* @details Intended to be thrown when an actual Python exception is raised,
* then caught before returning control to Python. When used this
* way, it provides an easy way to return control to Python
* when a Python exception occurs (instead of e.g. propagating nullptr
* all the way back to Python, which is usually how error handling is done).
*/
class PythonError : public std::runtime_error
{
public:
PythonError():
std::runtime_error( LvArray::system::stackTrace( true ) )
{}
};
} // namespace python
} // namespace LvArray