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 #if __has_warning( "-Wc99-designator" ) 77 _Pragma(
"GCC diagnostic ignored \"-Wc99-designator\"")
80 #define BEGIN_ALLOW_DESIGNATED_INITIALIZERS \ 81 _Pragma( "GCC diagnostic push" ) \ 82 _Pragma( "GCC diagnostic ignored \"-Wc99-designator\"") 84 #elif defined( __GNUC__ ) 85 #define BEGIN_ALLOW_DESIGNATED_INITIALIZERS \ 86 _Pragma( "GCC diagnostic push" ) \ 87 _Pragma( "GCC diagnostic ignored \"-Wpedantic\"" ) \ 88 _Pragma( "GCC diagnostic ignored \"-Wmissing-field-initializers\"" ) 90 _Pragma(
"GCC diagnostic ignored \"-Wc++20-extensions\"")
97 #define END_ALLOW_DESIGNATED_INITIALIZERS \ 98 _Pragma( "GCC diagnostic pop" ) 114 void xincref( PyObject *
const obj );
120 void xdecref( PyObject *
const obj );
128 bool isInstanceOf( PyObject *
const obj, PyTypeObject * type );
148 template<
typename T = PyObject >
181 { *
this = std::move( src ); }
187 { internal::xdecref( reinterpret_cast< PyObject * >( m_object ) ); }
198 internal::xincref( reinterpret_cast< PyObject * >( src.
m_object ) );
211 src.m_object =
nullptr;
223 internal::xdecref( reinterpret_cast< PyObject * >( m_object ) );
250 {
return &m_object; }
259 T *
const ret = m_object;
266 T * m_object =
nullptr;
276 template<
typename T >
277 T *
convert( PyObject *
const obj, PyTypeObject *
const type )
279 if( internal::isInstanceOf( obj, type ) )
281 return reinterpret_cast< T *
>( obj );
295 PyTypeObject *
const type,
296 char const *
const typeName );
303 PyObject *
create( std::string
const & value );
320 inline PyObject *
create( std::vector< std::string >
const & vec )
T * release()
Return the address of the managed object and release ownership.
Definition: pythonHelpers.hpp:257
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:136
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:221
T * m_object
A pointer to the manged object.
Definition: pythonHelpers.hpp:266
PyObjectRef(PyObjectRef &&src)
Steal a reference from src.
Definition: pythonHelpers.hpp:180
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:186
Base class for all C++ exceptions related to Python.
Definition: pythonHelpers.hpp:332
PyObjectRef & operator=(PyObjectRef const &src)
Create a new reference to src.
Definition: pythonHelpers.hpp:195
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:149
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:277
PyObjectRef & operator=(PyObjectRef &&src)
Steal a reference from src.
Definition: pythonHelpers.hpp:208
PyObjectRef(T *const src)
Take ownership of a reference to src.
Definition: pythonHelpers.hpp:163
T ** getAddress()
Return the address of the pointer to the manged object.
Definition: pythonHelpers.hpp:249
PyObjectRef(PyObjectRef const &src)
Create a new reference to src.
Definition: pythonHelpers.hpp:172