32 view.strides[view.ndim-1] =
itemsize;
34 view.strides[
idim] = view.itemsize;
49 pyobj->fBuf =
nullptr;
50 pyobj->fConverter =
nullptr;
51 pyobj->fElemCnv =
nullptr;
64 delete []
pyobj->fBuf;
70 pyobj->fElemCnv &&
pyobj->fElemCnv->HasState())
71 delete pyobj->fElemCnv;
73 if (
pyobj->fConverter &&
pyobj->fConverter->HasState())
74 delete pyobj->fConverter;
81#define CPYCPPYY_LL_FLAG_GETSET(name, flag, doc) \
82static PyObject* ll_get##name(CPyCppyy::LowLevelView* pyobj) \
84 return PyBool_FromLong((long)((intptr_t)pyobj->fBufInfo.internal & flag));\
87static int ll_set##name(CPyCppyy::LowLevelView* pyobj, PyObject* value, void*)\
89 long settrue = PyLong_AsLong(value); \
90 if (settrue == -1 && PyErr_Occurred()) { \
91 PyErr_SetString(PyExc_ValueError, #doc" should be either True or False");\
96 (intptr_t&)pyobj->fBufInfo.internal |= flag; \
98 (intptr_t&)pyobj->fBufInfo.internal &= ~flag; \
120#define HAVE_PTR(suboffsets, dim) (suboffsets && suboffsets[dim] >= 0)
122#define ADJUST_PTR(ptr, suboffsets, dim) \
123 (HAVE_PTR(suboffsets, dim) ? *((char**)ptr) + suboffsets[dim] : ptr)
127#define HAVE_SUBOFFSETS_IN_LAST_DIM(view) \
128 (view->suboffsets && view->suboffsets[dest->ndim-1] >= 0)
137 src->strides[
src->ndim-1] ==
src->itemsize);
149 for (
int i = 0; i <
dest->ndim; i++) {
150 if (
dest->shape[i] !=
src->shape[i])
152 if (
dest->shape[i] == 0)
167 "low level pointer assignment: lvalue and rvalue have different structures");
226 (
char*)
src->buf,
src->strides,
src->suboffsets,
250 "negative index not supported on dimension %d with unknown size", dim + 1);
257 "multi index not supported on dimension %d with unknown stride", dim + 1);
263 "index out of bounds on dimension %d", dim + 1);
267 ptr += view.strides[dim] *
index;
290 "cannot index %d-dimension view with %zd-element tuple", view.ndim,
nindices);
294 char* ptr = (
char*)
llview->get_buf();
316 if (!
self->get_buf())
318 return self->fBufInfo.ndim == 0 ? 1 :
self->fBufInfo.shape[0];
326#if PY_VERSION_HEX < 0x03000000
335 if (!base->suboffsets || dim == 0) {
337 base->buf = (
char *)base->buf + base->strides[dim] * start;
341 while (
n >= 0 && base->suboffsets[
n] < 0)
345 base->suboffsets[
n] = base->suboffsets[
n] + base->strides[dim] * start;
348 base->strides[dim] = base->strides[dim] * step;
395 if (!
self->get_buf()) {
400 if (view.ndim == 0) {
409 return self->fConverter->FromMemory(ptr);
410 return self->fConverter->FromMemory((
void*)&ptr);
426 "sub-views are not implemented");
433 return ptr ?
self->fElemCnv->FromMemory(ptr) :
nullptr;
448 if (view.ndim == 0) {
450 return self->fConverter->FromMemory(
self->get_buf());
458 "invalid indexing of 0-dim memory");
470 if (view.ndim == 1) {
479 char* buf = (
char*)
self->get_buf();
481 size_t isize = view.itemsize;
499 "multi-dimensional slicing is not implemented");
508 "multi-dimensional slicing is not implemented");
527 if (
value ==
nullptr) {
532 if (view.ndim == 0) {
535 return self->fConverter->ToMemory(
value,
self->get_buf()) ? 0 : -1;
539 "invalid indexing of 0-dim memory");
548 "sub-views are not implemented");
557 return self->fConverter->ToMemory(
value, ptr) ? 0 : -1;
575 if (view.suboffsets) {
576 dest.suboffsets = &
arrays[2];
dest.suboffsets[0] = view.suboffsets[0];
592 "sub-views are not implemented");
598 return self->fElemCnv->ToMemory(
value, ptr) ? 0 : -1;
604 "LowLevelView slice assignments are currently restricted "
613#if PY_VERSION_HEX < 0x03000000
623 return self->fBufInfo.len;
640 *view =
self->fBufInfo;
653 "underlying buffer is not Fortran contiguous");
660 if (view->format !=
NULL) {
664 "cannot cast to unsigned bytes if the format flag is present");
689 if (!
ii)
return nullptr;
724#if PY_VERSION_HEX < 0x03000000
793 size_t itemsize = view.strides[view.ndim-1];
808 if (
idim == 0) view.len =
nlen * view.itemsize;
885 if (
strcmp(view.format,
"b") != 0 || view.ndim != 1) {
887 "as_string only supported for 1-dim char strings (format: %s, dim: %d)",
888 view.format, (
int)view.ndim);
892 char* buf = (
char*)
self->get_buf();
893 size_t sz =
strnlen(buf, (
size_t)view.shape[0]);
900 (
char*)
"change the shape (not layout) of the low level view"},
902 (
char*)
"interpret memory as a null-terminated char string and return Python str"},
904 (
char*)
"return a numpy array from the low level view"},
905 {(
char*)
nullptr,
nullptr, 0,
nullptr}
911 (
char*)
"If true, python manages the life time of this buffer",
nullptr},
913 (
char*)
"If true, this array was allocated with C++\'s new[]",
nullptr},
917 {(
char*)
nullptr,
nullptr,
nullptr,
nullptr,
nullptr }
926 (
char*)
"cppyy.LowLevelView",
946 (
char*)
"memory view on C++ pointer",
971#if PY_VERSION_HEX >= 0x02030000
974#if PY_VERSION_HEX >= 0x02060000
977#if PY_VERSION_HEX >= 0x03040000
980#if PY_VERSION_HEX >= 0x03080000
983#if PY_VERSION_HEX >= 0x030c0000
986#if PY_VERSION_HEX >= 0x030d0000
995template<
typename T>
struct typecode_traits {};
996template<>
struct typecode_traits<
bool> {
997 static constexpr const char*
format =
"?";
static constexpr const char*
name =
"bool"; };
998template<>
struct typecode_traits<char> {
999 static constexpr const char*
format =
"b";
static constexpr const char*
name =
"char"; };
1000template<>
struct typecode_traits<
signed char> {
1001 static constexpr const char*
format =
"b";
static constexpr const char*
name =
"signed char"; };
1002template<>
struct typecode_traits<unsigned char> {
1003 static constexpr const char*
format =
"B";
static constexpr const char*
name =
"UCharAsInt"; };
1004#if __cplusplus > 201402L
1005template<>
struct typecode_traits<std::
byte> {
1006 static constexpr const char*
format =
"B";
static constexpr const char*
name =
"UCharAsInt"; };
1008template<>
struct typecode_traits<char*> {
1009 static constexpr const char*
format =
"b";
static constexpr const char*
name =
"char*"; };
1010template<>
struct typecode_traits<const char*> {
1011 static constexpr const char*
format =
"b";
static constexpr const char*
name =
"const char*"; };
1012template<>
struct typecode_traits<short> {
1013 static constexpr const char*
format =
"h";
static constexpr const char*
name =
"short"; };
1014template<>
struct typecode_traits<unsigned short> {
1015 static constexpr const char*
format =
"H";
static constexpr const char*
name =
"unsigned short"; };
1016template<>
struct typecode_traits<
int> {
1017 static constexpr const char*
format =
"i";
static constexpr const char*
name =
"int"; };
1018template<>
struct typecode_traits<unsigned
int> {
1019 static constexpr const char*
format =
"I";
static constexpr const char*
name =
"unsigned int"; };
1020template<>
struct typecode_traits<long> {
1021 static constexpr const char*
format =
"l";
static constexpr const char*
name =
"long"; };
1022template<>
struct typecode_traits<unsigned long> {
1023 static constexpr const char*
format =
"L";
static constexpr const char*
name =
"unsigned long"; };
1024template<>
struct typecode_traits<long long> {
1025 static constexpr const char*
format =
"q";
static constexpr const char*
name =
"long long"; };
1026template<>
struct typecode_traits<unsigned long long> {
1027 static constexpr const char*
format =
"Q";
static constexpr const char*
name =
"unsigned long long"; };
1028template<>
struct typecode_traits<float> {
1029 static constexpr const char*
format =
"f";
static constexpr const char*
name =
"float"; };
1030template<>
struct typecode_traits<
double> {
1031 static constexpr const char*
format =
"d";
static constexpr const char*
name =
"double"; };
1032template<>
struct typecode_traits<long
double> {
1033 static constexpr const char*
format =
"D";
static constexpr const char*
name =
"long double"; };
1034template<>
struct typecode_traits<std::complex<float>> {
1035 static constexpr const char*
format =
"Zf";
static constexpr const char*
name =
"std::complex<float>"; };
1036template<>
struct typecode_traits<std::complex<double>> {
1037 static constexpr const char*
format =
"Zd";
static constexpr const char*
name =
"std::complex<double>"; };
1038template<>
struct typecode_traits<std::complex<int>> {
1039 static constexpr const char*
format =
"Zi";
static constexpr const char*
name =
"std::complex<int>"; };
1040template<>
struct typecode_traits<std::complex<long>> {
1041 static constexpr const char*
format =
"Zl";
static constexpr const char*
name =
"std::complex<long>"; };
1050 if (bi.ndim == 1 &&
bi.shape) {
1051 bi.len =
sz *
bi.itemsize;
1076 view.format = (
char*)(
format ?
format : typecode_traits<T>::format);
1081 view.suboffsets =
nullptr;
1085 for (
int i = 0; i < shape.
ndim(); ++i)
1091 if (view.ndim == 1) {
1093 view.len =
nx *
sizeof(T);
1095 llp->fConverter =
llp->fElemCnv;
1098 view.len =
nx *
sizeof(
void*);
1099 view.itemsize =
sizeof(
void*);
1122 llp->set_buf((
void**)address);
1127#define CPPYY_RET_W_CREATOR(type, fname) \
1128 PyObject* (*c)(type, cdims_t) = &fname; \
1129 ll->fCreator = (LowLevelView::Creator_t)c; \
1130 return (PyObject*)ll
1132#define CPPYY_IMPL_VIEW_CREATOR(type) \
1133PyObject* CPyCppyy::CreateLowLevelView(type* address, cdims_t shape) { \
1134 LowLevelView* ll = CreateLowLevelViewT<type>(address, shape); \
1135 CPPYY_RET_W_CREATOR(type*, CreateLowLevelView); \
1137PyObject* CPyCppyy::CreateLowLevelView(type** address, cdims_t shape) { \
1138 LowLevelView* ll = CreateLowLevelViewT<type>(address, shape); \
1139 CPPYY_RET_W_CREATOR(type**, CreateLowLevelView); \
1145#if __cplusplus > 201402L
#define PyInt_FromSsize_t
#define CPyCppyy_PyText_FromStringAndSize
Py_ssize_t PyNumber_AsSsize_t(PyObject *obj, PyObject *)
#define CPyCppyy_PyText_AsString
void CPyCppyy_PyBuffer_Release(PyObject *, Py_buffer *view)
#define CPyCppyy_PyText_AsStringChecked
#define CPyCppyy_PyText_FromString
#define PyVarObject_HEAD_INIT(type, size)
#define PyIndex_Check(obj)
#define ADJUST_PTR(ptr, suboffsets, dim)
static PyObject * ll_typecode(CPyCppyy::LowLevelView *self, void *)
static void * ptr_from_index(CPyCppyy::LowLevelView *llview, Py_ssize_t index)
static int ll_ass_sub(CPyCppyy::LowLevelView *self, PyObject *key, PyObject *value)
static PyObject * ll_item_multi(CPyCppyy::LowLevelView *self, PyObject *tup)
static Py_ssize_t is_multiindex(PyObject *key)
static Py_ssize_t ll_oldgetbuf(CPyCppyy::LowLevelView *self, Py_ssize_t seg, void **pptr)
static PySequenceMethods ll_as_sequence
static Py_ssize_t ll_getsegcount(PyObject *, Py_ssize_t *lenp)
static PyObject * ll_reshape(CPyCppyy::LowLevelView *self, PyObject *shape)
static CPyCppyy::LowLevelView * CreateLowLevelViewT(T *address, CPyCppyy::cdims_t shape, const char *format=nullptr, const char *name=nullptr, Py_ssize_t itemsize=-1)
static PyObject * ll_getcpparray(CPyCppyy::LowLevelView *pyobj)
#define CPYCPPYY_LL_FLAG_GETSET(name, flag, doc)
static bool equiv_shape(const Py_buffer *dest, const Py_buffer *src)
#define CPPYY_RET_W_CREATOR(type, fname)
static int ll_getbuf(CPyCppyy::LowLevelView *self, Py_buffer *view, int flags)
static void * ptr_from_tuple(CPyCppyy::LowLevelView *llview, PyObject *tup)
static int last_dim_is_contiguous(const Py_buffer *dest, const Py_buffer *src)
static PyBufferProcs ll_as_buffer
static PyMethodDef ll_methods[]
static bool equiv_structure(const Py_buffer *dest, const Py_buffer *src)
static void copy_base(const Py_ssize_t *shape, Py_ssize_t itemsize, char *dptr, const Py_ssize_t *dstrides, const Py_ssize_t *dsuboffsets, char *sptr, const Py_ssize_t *sstrides, const Py_ssize_t *ssuboffsets, char *mem)
static PyObject * ll_shape(CPyCppyy::LowLevelView *self)
static void ll_dealloc(CPyCppyy::LowLevelView *pyobj)
static int ll_setcpparray(CPyCppyy::LowLevelView *pyobj, PyObject *value, void *)
static PyMappingMethods ll_as_mapping
#define CPPYY_IMPL_VIEW_CREATOR(type)
static int ll_setownership(CPyCppyy::LowLevelView *pyobj, PyObject *value, void *)
static PyGetSetDef ll_getset[]
static CPyCppyy::LowLevelView * ll_new(PyTypeObject *subtype, PyObject *, PyObject *)
static PyObject * ll_item(CPyCppyy::LowLevelView *self, Py_ssize_t index)
static int copy_single(Py_buffer *dest, Py_buffer *src)
static PyObject * ll_iter(PyObject *self)
static int init_slice(Py_buffer *base, PyObject *_key, int dim)
static Py_ssize_t ll_length(CPyCppyy::LowLevelView *self)
static void set_strides(Py_buffer &view, size_t itemsize, bool isfix)
static bool is_multislice(PyObject *key)
static PyObject * ll_array(CPyCppyy::LowLevelView *self, PyObject *args, PyObject *kwds)
static PyObject * ll_getownership(CPyCppyy::LowLevelView *pyobj)
static PyObject * ll_subscript(CPyCppyy::LowLevelView *self, PyObject *key)
static char * lookup_dimension(Py_buffer &view, char *ptr, int dim, Py_ssize_t index)
static PyObject * ll_as_string(CPyCppyy::LowLevelView *self)
#define HAVE_SUBOFFSETS_IN_LAST_DIM(view)
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t dest
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t nitems
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t format
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t src
PyObject_HEAD Py_buffer fBufInfo
PyObject * CreateLowLevelView(bool *, cdims_t shape)
PyObject * CreateLowLevelViewString(char **, cdims_t shape)
static const dim_t UNKNOWN_SIZE
CPYCPPYY_EXTERN Converter * CreateConverter(const std::string &name, cdims_t=0)
PyObject * CreateLowLevelView_i8(int8_t *, cdims_t shape)
PyTypeObject LowLevelView_Type