[Python-3000-checkins] r56639 - python/branches/py3k-buffer/Include/abstract.h python/branches/py3k-buffer/Include/memoryobject.h python/branches/py3k-buffer/Include/object.h python/branches/py3k-buffer/Include/pyerrors.h
travis.oliphant
python-3000-checkins at python.org
Wed Aug 1 07:42:58 CEST 2007
Author: travis.oliphant
Date: Wed Aug 1 07:42:56 2007
New Revision: 56639
Added:
python/branches/py3k-buffer/Include/memoryobject.h (contents, props changed)
Modified:
python/branches/py3k-buffer/Include/abstract.h
python/branches/py3k-buffer/Include/object.h
python/branches/py3k-buffer/Include/pyerrors.h
Log:
Layout the new buffer interface.
Modified: python/branches/py3k-buffer/Include/abstract.h
==============================================================================
--- python/branches/py3k-buffer/Include/abstract.h (original)
+++ python/branches/py3k-buffer/Include/abstract.h Wed Aug 1 07:42:56 2007
@@ -475,6 +475,12 @@
This is the equivalent of the Python statement: del o[key].
*/
+ /* old buffer API
+ FIXME: usage of these should all be replaced in Python itself
+ but for backwards compatibility we will implement them.
+ Their usage without a corresponding "unlock" mechansim
+ may create issues (but they would already be there). */
+
PyAPI_FUNC(int) PyObject_AsCharBuffer(PyObject *obj,
const char **buffer,
Py_ssize_t *buffer_len);
@@ -527,6 +533,110 @@
an exception set.
*/
+ /* new buffer API */
+
+ PyAPI_FUNC(int) PyObject_CheckBuffer(PyObject *obj);
+
+ /* Return 1 if the getbuffer function is available, otherwise
+ return 0 */
+
+ PyAPI_FUNC(int) PyObject_GetBuffer(PyObject *obj, PyBuffer *view,
+ int flags);
+
+ /* This is a C-API version of the getbuffer function call. It checks
+ to make sure object has the required function pointer and issues the
+ call. Returns -1 and raises an error on failure and returns 0 on
+ success
+ */
+
+ PyAPI_FUNC(int) PyObject_ReleaseBuffer(PyObject *obj, PyBuffer *view);
+
+ /* C-API version of the releasebuffer function call. It checks
+ to make sure the object has the required function pointer and
+ issues the call. Returns 0 on success and -1 (with an error
+ raised) on failure. This function always succeeds (as a NO-OP)
+ if there is no releasebuffer function for the object so that
+ it can always be called when the consumer is done with the buffer
+ */
+
+ PyAPI_FUNC(int) PyBuffer_SizeFromFormat(const char *);
+
+ /* Return the implied itemsize of the data-format area from a
+ struct-style description */
+
+ PyAPI_FUNC(int) PyObject_GetContiguous(PyObject *obj, void **buf,
+ Py_ssize_t *len, char **format,
+ char fortran);
+
+ /* Return a contiguous chunk of memory representing the buffer
+ from an object. If a copy is made then return 1 (the
+ return variable should be checked so the memory can be
+ freed if needed after the caller is done with it). If no
+ copy was needed return 0. If an error occurred in probing
+ the buffer interface then return -1.
+
+ The contiguous chunck of memory is pointed to by *buf and
+ the length of that memory is *len. The format of that
+ memory is returned in struct-string syntax in *format.
+
+ If the object is multi-dimensional and if fortran is 'F',
+ the first dimension of the underlying array will vary the
+ fastest in the buffer. If fortran is 'C', then the last
+ dimension will vary the fastest (C-style contiguous). If
+ fortran is 'A', then it does not matter and you will get
+ whatever the object decides is more efficient.
+
+ If a copy is made, then the memory *must be freed* by
+ calling PyMem_Free when the user of this sub-routine is
+ done with the memory
+ */
+
+ PyAPI_FUNC(int) PyObject_CopyToObject(PyObject *obj, void *buf,
+ Py_ssize_t len, char fortran);
+
+ /* Copy len bytes of data from the contiguous chunk of memory
+ pointed to by buf into the buffer exported by obj. Return
+ 0 on success and return -1 and raise a PyBuffer_Error on
+ error (i.e. the object does not have a buffer interface or
+ it is not working).
+
+ If fortran is 'F', then if the object is multi-dimensional,
+ then the data will be copied into the array in
+ Fortran-style (first dimension varies the fastest). If
+ fortran is 'C', then the data will be copied into the array
+ in C-style (last dimension varies the fastest). If fortran
+ is 'A', then it does not matter and the copy will be made
+ in whatever way is more efficient.
+
+ */
+
+ PyAPI_FUNC(int) PyBuffer_IsContiguous(PyBuffer *view, char fortran);
+
+ PyAPI_FUNC(int) PyBuffer_IsAligned(PyBuffer *view);
+
+ PyAPI_FUNC(int) PyBuffer_FillContiguousStrides(int *ndims,
+ Py_ssize_t *shape,
+ int itemsize,
+ Py_ssize_t *strides,
+ char fortran);
+
+ /* Fill the strides array with byte-strides of a contiguous
+ (Fortran-style if fortran is 'F' or C-style otherwise)
+ array of the given shape with the given number of bytes
+ per element.
+ */
+
+ PyAPI_FUNC(int) PyBuffer_FillInfo(PyBuffer *view, void *buf,
+ Py_ssize_t len, int readonly,
+ int flags);
+
+ /* Fills in a buffer-info structure correctly for an exporter
+ that can only share a contiguous chunk of memory of
+ "unsigned bytes" of the given length. Returns 0 on success
+ and -1 (with raising an error) on error.
+ */
+
+
/* Iterators */
PyAPI_FUNC(PyObject *) PyObject_GetIter(PyObject *);
Added: python/branches/py3k-buffer/Include/memoryobject.h
==============================================================================
--- (empty file)
+++ python/branches/py3k-buffer/Include/memoryobject.h Wed Aug 1 07:42:56 2007
@@ -0,0 +1,35 @@
+
+/* Memory object interface */
+
+#ifndef Py_MEMORYOBJECT_H
+#define Py_MEMORYOBJECT_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct {
+ PyObject_HEAD
+ PyObject *base;
+ int ndims;
+ Py_ssize_t *starts; /* slice starts */
+ Py_ssize_t *stops; /* slice stops */
+ Py_ssize_t *steps; /* slice steps */
+} PyMemoryViewObject;
+
+
+PyAPI_DATA(PyTypeObject) PyMemoryView_Type;
+
+#define PyMemory_Check(op) (Py_Type(op) == &PyMemoryView_Type)
+
+#define Py_END_OF_MEMORY (-1)
+
+PyAPI_FUNC(PyObject *) PyObject_GetMemoryView(PyObject *base);
+
+PyAPI_FUNC(PyObject *) PyMemoryView_FromMemory(PyBuffer *info);
+ /* create new if bufptr is NULL
+ will be a new bytesobject in base */
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* !Py_BUFFEROBJECT_H */
Modified: python/branches/py3k-buffer/Include/object.h
==============================================================================
--- python/branches/py3k-buffer/Include/object.h (original)
+++ python/branches/py3k-buffer/Include/object.h Wed Aug 1 07:42:56 2007
@@ -140,11 +140,55 @@
typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
-/* ssize_t-based buffer interface */
-typedef Py_ssize_t (*readbufferproc)(PyObject *, Py_ssize_t, void **);
-typedef Py_ssize_t (*writebufferproc)(PyObject *, Py_ssize_t, void **);
-typedef Py_ssize_t (*segcountproc)(PyObject *, Py_ssize_t *);
-typedef Py_ssize_t (*charbufferproc)(PyObject *, Py_ssize_t, char **);
+
+/* buffer interface */
+struct bufferinfo {
+ void *buf;
+ Py_ssize_t len;
+ int readonly;
+ const char *format;
+ int ndim;
+ Py_ssize_t *shape;
+ Py_ssize_t *strides;
+ Py_ssize_t *suboffsets;
+ int itemsize;
+ void *internal;
+} PyBuffer;
+
+typedef int (*getbufferproc)(PyObject *, PyBuffer *, int);
+typedef int (*releasebufferproc)(PyObject *, PyBuffer *);
+
+ /* Flags for getting buffers */
+#define PyBUF_SIMPLE 0
+#define PyBUF_REQ_WRITEABLE 0x0001
+#define PyBUF_REQ_LOCKDATA 0x0002
+#define PyBUF_REQ_FORMAT 0x0004
+#define PyBUF_REQ_ALIGNED (0x0008 | PyBUF_REQ_FORMAT)
+#define PyBUF_ALW_ND 0x0010
+#define PyBUF_ALW_STRIDES (0x0020 | PyBUF_ALW_ND)
+#define PyBUF_REQ_C_CONTIGUOUS (0x0040 | PyBUF_ALW_STRIDES)
+#define PyBUF_REQ_F_CONTIGUOUS (0x0080 | PyBUF_ALW_STRIDES)
+#define PyBUF_REQ_ANY_CONTIGUOUS (0x0200 | PyBUF_ALW_STRIDES)
+#define PyBUF_ALW_INDIRECT (0x0400 | PyBUF_ALW_STRIDES)
+
+#define PyBUF_CONTIG (PyBUF_ALW_ND | PyBUF_REQ_WRITEABLE | PyBUF_REQ_ALIGNED)
+#define PyBUF_CONTIG_RO (PyBUF_ALW_ND | PyBUF_REQ_ALIGNED)
+#define PyBUF_CONTIG_LCK (PyBUF_ALW_ND | PyBUF_REQ_LOCKDATA | PyBUF_REQ_ALIGNED)
+
+#define PyBUF_STRIDED (PyBUF_ALW_STRIDES | PyBUF_REQ_WRITEABLE | PyBUF_REQ_ALIGNED)
+#define PyBUF_STRIDED_RO (PyBUF_ALW_STRIDES | PyBUF_REQ_ALIGNED)
+#define PyBUF_STRIDED_LCK (PyBUF_ALW_STRIDES | PyBUF_REQ_LOCKDATA | PyBUF_REQ_ALIGNED)
+
+#define PyBUF_RECORDS (PyBUF_ALW_STRIDES | PyBUF_REQ_WRITEABLE | PyBUF_REQ_FORMAT)
+#define PyBUF_RECORDS_RO (PyBUF_ALW_STRIDES | PyBUF_REQ_FORMAT)
+#define PyBUF_RECORDS_LCK (PyBUF_ALW_STRIDES | PyBUF_REQ_LOCKDATA | PyBUF_REQ_FORMAT)
+
+#define PyBUF_FULL (PyBUF_ALW_INDIRECT | PyBUF_REQ_WRITEABLE | PyBUF_REQ_FORMAT)
+#define PyBUF_FULL_RO (PyBUF_ALW_INDIRECT | PyBUF_REQ_FORMAT)
+#define PyBUF_FULL_LCK (PyBUF_ALW_INDIRECT | PyBUF_REQ_LOCKDATA | PyBUF_REQ_FORMAT)
+
+
+/* End buffer interface */
typedef int (*objobjproc)(PyObject *, PyObject *);
typedef int (*visitproc)(PyObject *, void *);
@@ -218,14 +262,12 @@
objobjargproc mp_ass_subscript;
} PyMappingMethods;
+
typedef struct {
- readbufferproc bf_getreadbuffer;
- writebufferproc bf_getwritebuffer;
- segcountproc bf_getsegcount;
- charbufferproc bf_getcharbuffer;
+ getbufferproc bf_getbuffer;
+ releasebufferproc bf_releasebuffer;
} PyBufferProcs;
-
typedef void (*freefunc)(void *);
typedef void (*destructor)(PyObject *);
typedef int (*printfunc)(PyObject *, FILE *, int);
Modified: python/branches/py3k-buffer/Include/pyerrors.h
==============================================================================
--- python/branches/py3k-buffer/Include/pyerrors.h (original)
+++ python/branches/py3k-buffer/Include/pyerrors.h Wed Aug 1 07:42:56 2007
@@ -138,6 +138,7 @@
PyAPI_DATA(PyObject *) PyExc_UnicodeTranslateError;
PyAPI_DATA(PyObject *) PyExc_ValueError;
PyAPI_DATA(PyObject *) PyExc_ZeroDivisionError;
+PyAPI_DATA(PyObject *) PyExc_BufferError;
#ifdef MS_WINDOWS
PyAPI_DATA(PyObject *) PyExc_WindowsError;
#endif
More information about the Python-3000-checkins
mailing list