27 #include "../system.hpp" 28 #include "../Macros.hpp" 29 #include "../limits.hpp" 42 #if defined(PyObject_HEAD) 43 #define PYTHON_ERROR_IF( CONDITION, TYPE, MSG, RET ) \ 48 std::ostringstream __oss; \ 49 __oss << "***** ERROR\n"; \ 50 __oss << "***** LOCATION: " LOCATION "\n"; \ 51 __oss << "***** Controlling expression (should be false): " STRINGIZE( EXP ) "\n"; \ 52 __oss << MSG << "\n"; \ 53 __oss << LvArray::system::stackTrace( true ); \ 54 PyErr_SetString( TYPE, __oss.str().c_str() ); \ 59 #define PYTHON_ERROR_IF( CONDITION, TYPE, MSG, RET ) \ 60 static_assert( false, "You are attempting to use PYTHON_ERROR_IF but haven't yet included Python.hpp" ) 70 #if defined( __clang_version__ ) 71 #if defined( __CUDACC__ ) 72 #define BEGIN_ALLOW_DESIGNATED_INITIALIZERS \ 73 _Pragma( "GCC diagnostic push" ) \ 74 _Pragma( "GCC diagnostic ignored \"-Wc99-extensions\"") \ 75 _Pragma( "GCC diagnostic ignored \"-Wgnu-designator\"") \ 76 _Pragma( "GCC diagnostic ignored \"-Wc99-designator\"") 78 #define BEGIN_ALLOW_DESIGNATED_INITIALIZERS \ 79 _Pragma( "GCC diagnostic push" ) \ 80 _Pragma( "GCC diagnostic ignored \"-Wc99-designator\"") 83 #define BEGIN_ALLOW_DESIGNATED_INITIALIZERS \ 84 _Pragma( "GCC diagnostic push" ) \ 85 _Pragma( "GCC diagnostic ignored \"-Wpedantic\"" ) \ 86 _Pragma( "GCC diagnostic ignored \"-Wmissing-field-initializers\"" ) 92 #define END_ALLOW_DESIGNATED_INITIALIZERS \ 93 _Pragma( "GCC diagnostic pop" ) 109 void xincref( PyObject *
const obj );
115 void xdecref( PyObject *
const obj );
123 bool isInstanceOf( PyObject *
const obj, PyTypeObject * type );
143 template<
typename T = PyObject >
176 { *
this = std::move( src ); }
182 { internal::xdecref( reinterpret_cast< PyObject * >( m_object ) ); }
193 internal::xincref( reinterpret_cast< PyObject * >( src.
m_object ) );
206 src.m_object =
nullptr;
218 internal::xdecref( reinterpret_cast< PyObject * >( m_object ) );
245 {
return &m_object; }
254 T *
const ret = m_object;
261 T * m_object =
nullptr;
271 template<
typename T >
272 T *
convert( PyObject *
const obj, PyTypeObject *
const type )
274 if( internal::isInstanceOf( obj, type ) )
276 return reinterpret_cast< T *
>( obj );
290 PyTypeObject *
const type,
291 char const *
const typeName );
298 PyObject *
create( std::string
const & value );
315 inline PyObject *
create( std::vector< std::string >
const & vec )
T * release()
Return the address of the managed object and release ownership.
Definition: pythonHelpers.hpp:252
std::enable_if_t< internal::canExportToNumpy< T >, PyObject *> create(T &value)
Create a NumPy 1D array of length 1 containing the scalar value.
Definition: numpyHelpers.hpp:147
PyModify
An enumeration of the various access policies for Python objects.
Definition: pythonHelpers.hpp:131
PyObject * createPyListOfStrings(std::string const *const strptr, long long const size)
Create and return a Python list of strings from an array of std::strings. The Python strings will be ...
Definition: pythonHelpers.cpp:85
PyObjectRef & operator=(PyObject *src)
Decrease the reference count to the current object and take ownership of a new reference.
Definition: pythonHelpers.hpp:216
T * m_object
A pointer to the manged object.
Definition: pythonHelpers.hpp:261
PyObjectRef(PyObjectRef &&src)
Steal a reference from src.
Definition: pythonHelpers.hpp:175
std::string stackTrace(bool const location)
Return a demangled stack trace of the last 25 frames.
Definition: system.cpp:361
~PyObjectRef()
Destructor, decreases the reference count.
Definition: pythonHelpers.hpp:181
Base class for all C++ exceptions related to Python.
Definition: pythonHelpers.hpp:327
PyObjectRef & operator=(PyObjectRef const &src)
Create a new reference to src.
Definition: pythonHelpers.hpp:190
Forward declarations of Python Objects.
bool addTypeToModule(PyObject *const module, PyTypeObject *const type, char const *const typeName)
Add the Python type type to the module module.
Definition: pythonHelpers.cpp:61
A class that manages an owned Python reference with RAII semantics.
Definition: pythonHelpers.hpp:144
The top level namespace.
Definition: Array.hpp:24
T * convert(PyObject *const obj, PyTypeObject *const type)
Return obj casted to T if obj is an instance of type or nullptr if it is not.
Definition: pythonHelpers.hpp:272
PyObjectRef & operator=(PyObjectRef &&src)
Steal a reference from src.
Definition: pythonHelpers.hpp:203
PyObjectRef(T *const src)
Take ownership of a reference to src.
Definition: pythonHelpers.hpp:158
T ** getAddress()
Return the address of the pointer to the manged object.
Definition: pythonHelpers.hpp:244
PyObjectRef(PyObjectRef const &src)
Create a new reference to src.
Definition: pythonHelpers.hpp:167