Skip to content

Commit 718e69a

Browse files
committed
Add getters and setters
1 parent e2b8093 commit 718e69a

File tree

5 files changed

+123
-13
lines changed

5 files changed

+123
-13
lines changed

Doc/data/stable_abi.dat

+7
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@ function,PyBool_FromLong,3.2,
1212
var,PyBool_Type,3.2,
1313
function,PyBuffer_FillContiguousStrides,3.11,
1414
function,PyBuffer_FillInfo,3.11,
15+
function,PyBuffer_FillInfoEx,3.11,
1516
function,PyBuffer_Free,3.11,
1617
function,PyBuffer_FromContiguous,3.11,
18+
function,PyBuffer_GetInternal,3.11,
19+
function,PyBuffer_GetItemSize,3.11,
20+
function,PyBuffer_GetLayout,3.11,
21+
function,PyBuffer_GetLength,3.11,
22+
function,PyBuffer_GetObject,3.11,
1723
function,PyBuffer_GetPointer,3.11,
1824
function,PyBuffer_IsContiguous,3.11,
25+
function,PyBuffer_IsReadonly,3.11,
1926
function,PyBuffer_New,3.11,
2027
function,PyBuffer_Release,3.11,
2128
function,PyBuffer_SizeFromFormat,3.11,

Include/buffer.h

+16
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ PyAPI_FUNC(int) PyBuffer_FillInfo(Py_buffer *view, PyObject *o, void *buf,
7777
Py_ssize_t len, int readonly,
7878
int flags);
7979

80+
PyAPI_FUNC(int) PyBuffer_FillInfoEx(Py_buffer *view, PyObject *obj, void *buf,
81+
Py_ssize_t len, int readonly, int flags,
82+
Py_ssize_t itemsize, int ndim,
83+
char *format, Py_ssize_t *shape,
84+
Py_ssize_t *strides, Py_ssize_t *suboffsets,
85+
void *internal);
86+
8087
/* Allocate a new buffer struct on the heap. */
8188
PyAPI_FUNC(Py_buffer *) PyBuffer_New(void);
8289

@@ -87,6 +94,15 @@ PyAPI_FUNC(void) PyBuffer_Free(Py_buffer *view);
8794
/* Releases a Py_buffer obtained from getbuffer ParseTuple's "s*". */
8895
PyAPI_FUNC(void) PyBuffer_Release(Py_buffer *view);
8996

97+
PyAPI_FUNC(PyObject *) PyBuffer_GetObject(Py_buffer *view);
98+
PyAPI_FUNC(Py_ssize_t) PyBuffer_GetLength(Py_buffer *view);
99+
PyAPI_FUNC(Py_ssize_t) PyBuffer_GetItemSize(Py_buffer *view);
100+
PyAPI_FUNC(int) PyBuffer_IsReadonly(Py_buffer *view);
101+
PyAPI_FUNC(void *) PyBuffer_GetInternal(Py_buffer *view);
102+
PyAPI_FUNC(int) PyBuffer_GetLayout(Py_buffer *view, char **format,
103+
Py_ssize_t **shape, Py_ssize_t **strides,
104+
Py_ssize_t **suboffsets);
105+
90106
/* Maximum number of dimensions */
91107
#define PyBUF_MAX_NDIM 64
92108

Misc/stable_abi.txt

+14
Original file line numberDiff line numberDiff line change
@@ -2171,12 +2171,26 @@ function PyBuffer_FillContiguousStrides
21712171
added 3.11
21722172
function PyBuffer_FillInfo
21732173
added 3.11
2174+
function PyBuffer_FillInfoEx
2175+
added 3.11
21742176
function PyBuffer_New
21752177
added 3.11
21762178
function PyBuffer_Free
21772179
added 3.11
21782180
function PyBuffer_Release
21792181
added 3.11
2182+
function PyBuffer_GetObject
2183+
added 3.11
2184+
function PyBuffer_GetLength
2185+
added 3.11
2186+
function PyBuffer_GetItemSize
2187+
added 3.11
2188+
function PyBuffer_IsReadonly
2189+
added 3.11
2190+
function PyBuffer_GetInternal
2191+
added 3.11
2192+
function PyBuffer_GetLayout
2193+
added 3.11
21802194

21812195
# (Detailed comments aren't really needed for further entries: from here on
21822196
# we can use version control logs.)

Objects/abstract.c

+79-13
Original file line numberDiff line numberDiff line change
@@ -709,10 +709,12 @@ PyBuffer_FillContiguousStrides(int nd, Py_ssize_t *shape,
709709
return;
710710
}
711711

712+
712713
int
713-
PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len,
714-
int readonly, int flags)
715-
{
714+
PyBuffer_FillInfoEx(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len,
715+
int readonly, int flags, Py_ssize_t itemsize, int ndim,
716+
char *format, Py_ssize_t *shape, Py_ssize_t *strides,
717+
Py_ssize_t *suboffsets, void *internal) {
716718
if (view == NULL) {
717719
PyErr_SetString(PyExc_BufferError,
718720
"PyBuffer_FillInfo: view==NULL argument is obsolete");
@@ -726,28 +728,48 @@ PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len,
726728
return -1;
727729
}
728730

731+
/* TODO: perform basic validation of arguments */
732+
729733
view->obj = obj;
730734
if (obj)
731735
Py_INCREF(obj);
732736
view->buf = buf;
733737
view->len = len;
734738
view->readonly = readonly;
735-
view->itemsize = 1;
736-
view->format = NULL;
737-
if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
739+
view->itemsize = itemsize;
740+
741+
if ((format == NULL) && ((flags & PyBUF_FORMAT) == PyBUF_FORMAT))
738742
view->format = "B";
739-
view->ndim = 1;
740-
view->shape = NULL;
741-
if ((flags & PyBUF_ND) == PyBUF_ND)
743+
else
744+
view->format = format;
745+
746+
view->ndim = ndim;
747+
748+
if ((shape == NULL) && ((flags & PyBUF_ND) == PyBUF_ND))
742749
view->shape = &(view->len);
743-
view->strides = NULL;
744-
if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES)
750+
else
751+
view->shape = shape;
752+
753+
if ((strides == NULL) && ((flags & PyBUF_STRIDES) == PyBUF_STRIDES))
745754
view->strides = &(view->itemsize);
746-
view->suboffsets = NULL;
747-
view->internal = NULL;
755+
else
756+
view->strides = strides;
757+
758+
view->suboffsets = suboffsets;
759+
view->internal = internal;
760+
748761
return 0;
749762
}
750763

764+
int
765+
PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len,
766+
int readonly, int flags)
767+
{
768+
return PyBuffer_FillInfoEx(
769+
view, obj, buf, len, readonly, flags, 1, 1, NULL, NULL, NULL, NULL, NULL
770+
);
771+
}
772+
751773
void
752774
PyBuffer_Release(Py_buffer *view)
753775
{
@@ -782,6 +804,50 @@ PyBuffer_Free(Py_buffer *view)
782804
}
783805
}
784806

807+
PyObject *
808+
PyBuffer_GetObject(Py_buffer *view) {
809+
return Py_XNewRef(view->obj);
810+
}
811+
812+
Py_ssize_t
813+
PyBuffer_GetLength(Py_buffer *view) {
814+
return view->len;
815+
}
816+
817+
Py_ssize_t
818+
PyBuffer_GetItemSize(Py_buffer *view) {
819+
return view->itemsize;
820+
}
821+
822+
int
823+
PyBuffer_IsReadonly(Py_buffer *view) {
824+
return view->readonly;
825+
}
826+
827+
void *
828+
PyBuffer_GetInternal(Py_buffer *view) {
829+
return view->internal;
830+
}
831+
832+
int
833+
PyBuffer_GetLayout(Py_buffer *view, char **format, Py_ssize_t **shape,
834+
Py_ssize_t **strides, Py_ssize_t **suboffsets) {
835+
if (format != NULL) {
836+
*format = view->format;
837+
}
838+
if (shape != NULL) {
839+
*shape = view->shape;
840+
}
841+
if (strides != NULL) {
842+
*strides = view->strides;
843+
}
844+
if (suboffsets != NULL) {
845+
*suboffsets = view->suboffsets;
846+
}
847+
return view->ndim;
848+
}
849+
850+
785851
PyObject *
786852
PyObject_Format(PyObject *obj, PyObject *format_spec)
787853
{

PC/python3dll.c

+7
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,17 @@ EXPORT_FUNC(PyArg_VaParseTupleAndKeywords)
9797
EXPORT_FUNC(PyBool_FromLong)
9898
EXPORT_FUNC(PyBuffer_FillContiguousStrides)
9999
EXPORT_FUNC(PyBuffer_FillInfo)
100+
EXPORT_FUNC(PyBuffer_FillInfoEx)
100101
EXPORT_FUNC(PyBuffer_Free)
101102
EXPORT_FUNC(PyBuffer_FromContiguous)
103+
EXPORT_FUNC(PyBuffer_GetInternal)
104+
EXPORT_FUNC(PyBuffer_GetItemSize)
105+
EXPORT_FUNC(PyBuffer_GetLayout)
106+
EXPORT_FUNC(PyBuffer_GetLength)
107+
EXPORT_FUNC(PyBuffer_GetObject)
102108
EXPORT_FUNC(PyBuffer_GetPointer)
103109
EXPORT_FUNC(PyBuffer_IsContiguous)
110+
EXPORT_FUNC(PyBuffer_IsReadonly)
104111
EXPORT_FUNC(PyBuffer_New)
105112
EXPORT_FUNC(PyBuffer_Release)
106113
EXPORT_FUNC(PyBuffer_SizeFromFormat)

0 commit comments

Comments
 (0)