mirror of https://github.com/python/cpython
Fix problems with memoryview object. There is still more to do to finish PEP 3118. The memory-view object needs to be fleshed out and the struct module needs to be modified.
This commit is contained in:
parent
9b30784ab2
commit
fe9bed02e4
|
@ -8,9 +8,9 @@ extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
PyObject_HEAD
|
PyObject_HEAD
|
||||||
PyObject *base;
|
PyObject *base;
|
||||||
Py_buffer view;
|
Py_buffer view;
|
||||||
} PyMemoryViewObject;
|
} PyMemoryViewObject;
|
||||||
|
|
||||||
|
|
||||||
|
@ -21,39 +21,40 @@ PyAPI_DATA(PyTypeObject) PyMemoryView_Type;
|
||||||
|
|
||||||
#define Py_END_OF_MEMORY (-1)
|
#define Py_END_OF_MEMORY (-1)
|
||||||
|
|
||||||
PyAPI_FUNC(PyObject *) PyMemoryView_GetContiguous(PyObject *base, int buffertype,
|
PyAPI_FUNC(PyObject *) PyMemoryView_GetContiguous(PyObject *base,
|
||||||
char fort);
|
int buffertype,
|
||||||
|
char fort);
|
||||||
|
|
||||||
/* Return a contiguous chunk of memory representing the buffer
|
/* Return a contiguous chunk of memory representing the buffer
|
||||||
from an object in a memory view object. If a copy is made then the
|
from an object in a memory view object. If a copy is made then the
|
||||||
base object for the memory view will be a *new* bytes object.
|
base object for the memory view will be a *new* bytes object.
|
||||||
|
|
||||||
Otherwise, the base-object will be the object itself and no
|
|
||||||
data-copying will be done.
|
|
||||||
|
|
||||||
The buffertype argument can be PyBUF_READ, PyBUF_WRITE,
|
|
||||||
PyBUF_UPDATEIFCOPY to determine whether the returned buffer
|
|
||||||
should be READONLY, WRITABLE, or set to update the
|
|
||||||
original buffer if a copy must be made. If buffertype is
|
|
||||||
PyBUF_WRITE and the buffer is not contiguous an error will
|
|
||||||
be raised. In this circumstance, the user can use
|
|
||||||
PyBUF_UPDATEIFCOPY to ensure that a a writable temporary
|
|
||||||
contiguous buffer is returned. The contents of this
|
|
||||||
contiguous buffer will be copied back into the original
|
|
||||||
object after the memoryview object is deleted as long as
|
|
||||||
the original object is writable and allows setting its
|
|
||||||
memory to "readonly". If this is not allowed by the
|
|
||||||
original object, then a BufferError is raised.
|
|
||||||
|
|
||||||
If the object is multi-dimensional and if fortran is 'F',
|
Otherwise, the base-object will be the object itself and no
|
||||||
the first dimension of the underlying array will vary the
|
data-copying will be done.
|
||||||
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.
|
|
||||||
|
|
||||||
A new reference is returned that must be DECREF'd when finished.
|
The buffertype argument can be PyBUF_READ, PyBUF_WRITE,
|
||||||
*/
|
PyBUF_SHADOW to determine whether the returned buffer
|
||||||
|
should be READONLY, WRITABLE, or set to update the
|
||||||
|
original buffer if a copy must be made. If buffertype is
|
||||||
|
PyBUF_WRITE and the buffer is not contiguous an error will
|
||||||
|
be raised. In this circumstance, the user can use
|
||||||
|
PyBUF_SHADOW to ensure that a a writable temporary
|
||||||
|
contiguous buffer is returned. The contents of this
|
||||||
|
contiguous buffer will be copied back into the original
|
||||||
|
object after the memoryview object is deleted as long as
|
||||||
|
the original object is writable and allows setting an
|
||||||
|
exclusive write lock. If this is not allowed by the
|
||||||
|
original object, then a BufferError is raised.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
A new reference is returned that must be DECREF'd when finished.
|
||||||
|
*/
|
||||||
|
|
||||||
PyAPI_FUNC(PyObject *) PyMemoryView_FromObject(PyObject *base);
|
PyAPI_FUNC(PyObject *) PyMemoryView_FromObject(PyObject *base);
|
||||||
|
|
||||||
|
|
|
@ -164,7 +164,7 @@ typedef void (*releasebufferproc)(PyObject *, Py_buffer *);
|
||||||
#define PyBUF_WRITABLE 0x0002
|
#define PyBUF_WRITABLE 0x0002
|
||||||
/* we used to include an E, backwards compatible alias */
|
/* we used to include an E, backwards compatible alias */
|
||||||
#define PyBUF_WRITEABLE PyBUF_WRITABLE
|
#define PyBUF_WRITEABLE PyBUF_WRITABLE
|
||||||
#define PyBUF_LOCKDATA 0x0004
|
#define PyBUF_LOCK 0x0004
|
||||||
#define PyBUF_FORMAT 0x0008
|
#define PyBUF_FORMAT 0x0008
|
||||||
#define PyBUF_ND 0x0010
|
#define PyBUF_ND 0x0010
|
||||||
#define PyBUF_STRIDES (0x0020 | PyBUF_ND)
|
#define PyBUF_STRIDES (0x0020 | PyBUF_ND)
|
||||||
|
@ -175,19 +175,25 @@ typedef void (*releasebufferproc)(PyObject *, Py_buffer *);
|
||||||
|
|
||||||
#define PyBUF_CONTIG (PyBUF_ND | PyBUF_WRITABLE)
|
#define PyBUF_CONTIG (PyBUF_ND | PyBUF_WRITABLE)
|
||||||
#define PyBUF_CONTIG_RO (PyBUF_ND)
|
#define PyBUF_CONTIG_RO (PyBUF_ND)
|
||||||
#define PyBUF_CONTIG_LCK (PyBUF_ND | PyBUF_LOCKDATA)
|
#define PyBUF_CONTIG_LCK (PyBUF_ND | PyBUF_LOCK)
|
||||||
|
#define PyBUF_CONTIG_XLCK (PyBUF_ND | PyBUF_LOCK | PyBUF_WRITABLE)
|
||||||
|
|
||||||
#define PyBUF_STRIDED (PyBUF_STRIDES | PyBUF_WRITABLE)
|
#define PyBUF_STRIDED (PyBUF_STRIDES | PyBUF_WRITABLE)
|
||||||
#define PyBUF_STRIDED_RO (PyBUF_STRIDES)
|
#define PyBUF_STRIDED_RO (PyBUF_STRIDES)
|
||||||
#define PyBUF_STRIDED_LCK (PyBUF_STRIDES | PyBUF_LOCKDATA)
|
#define PyBUF_STRIDED_LCK (PyBUF_STRIDES | PyBUF_LOCK)
|
||||||
|
#define PyBUF_STRIDED_XLCK (PyBUF_STRIDES | PyBUF_LOCK | PyBUF_WRITABLE)
|
||||||
|
|
||||||
#define PyBUF_RECORDS (PyBUF_STRIDES | PyBUF_WRITABLE | PyBUF_FORMAT)
|
#define PyBUF_RECORDS (PyBUF_STRIDES | PyBUF_WRITABLE | PyBUF_FORMAT)
|
||||||
#define PyBUF_RECORDS_RO (PyBUF_STRIDES | PyBUF_FORMAT)
|
#define PyBUF_RECORDS_RO (PyBUF_STRIDES | PyBUF_FORMAT)
|
||||||
#define PyBUF_RECORDS_LCK (PyBUF_STRIDES | PyBUF_LOCKDATA | PyBUF_FORMAT)
|
#define PyBUF_RECORDS_LCK (PyBUF_STRIDES | PyBUF_LOCK | PyBUF_FORMAT)
|
||||||
|
#define PyBUF_RECORDS_XLCK (PyBUF_STRIDES | PyBUF_LOCK | PyBUF_WRITABLE \
|
||||||
|
| PyBUF_FORMAT)
|
||||||
|
|
||||||
#define PyBUF_FULL (PyBUF_INDIRECT | PyBUF_WRITABLE | PyBUF_FORMAT)
|
#define PyBUF_FULL (PyBUF_INDIRECT | PyBUF_WRITABLE | PyBUF_FORMAT)
|
||||||
#define PyBUF_FULL_RO (PyBUF_INDIRECT | PyBUF_FORMAT)
|
#define PyBUF_FULL_RO (PyBUF_INDIRECT | PyBUF_FORMAT)
|
||||||
#define PyBUF_FULL_LCK (PyBUF_INDIRECT | PyBUF_LOCKDATA | PyBUF_FORMAT)
|
#define PyBUF_FULL_LCK (PyBUF_INDIRECT | PyBUF_LOCK | PyBUF_FORMAT)
|
||||||
|
#define PyBUF_FULL_XLCK (PyBUF_INDIRECT | PyBUF_LOCK | PyBUF_WRITABLE \
|
||||||
|
| PyBUF_FORMAT)
|
||||||
|
|
||||||
|
|
||||||
#define PyBUF_READ 0x100
|
#define PyBUF_READ 0x100
|
||||||
|
|
|
@ -220,11 +220,11 @@ PyObject_DelItemString(PyObject *o, char *key)
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
PyObject_AsCharBuffer(PyObject *obj,
|
PyObject_AsCharBuffer(PyObject *obj,
|
||||||
const char **buffer,
|
const char **buffer,
|
||||||
Py_ssize_t *buffer_len)
|
Py_ssize_t *buffer_len)
|
||||||
{
|
{
|
||||||
PyBufferProcs *pb;
|
PyBufferProcs *pb;
|
||||||
Py_buffer view;
|
Py_buffer view;
|
||||||
|
|
||||||
if (obj == NULL || buffer == NULL || buffer_len == NULL) {
|
if (obj == NULL || buffer == NULL || buffer_len == NULL) {
|
||||||
null_error();
|
null_error();
|
||||||
|
@ -235,30 +235,30 @@ PyObject_AsCharBuffer(PyObject *obj,
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
"expected an object with the buffer interface");
|
"expected an object with the buffer interface");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if ((*pb->bf_getbuffer)(obj, &view, PyBUF_CHARACTER)) return -1;
|
if ((*pb->bf_getbuffer)(obj, &view, PyBUF_CHARACTER)) return -1;
|
||||||
|
|
||||||
*buffer = view.buf;
|
*buffer = view.buf;
|
||||||
*buffer_len = view.len;
|
*buffer_len = view.len;
|
||||||
if (pb->bf_releasebuffer != NULL)
|
if (pb->bf_releasebuffer != NULL)
|
||||||
(*pb->bf_releasebuffer)(obj, &view);
|
(*pb->bf_releasebuffer)(obj, &view);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
PyObject_CheckReadBuffer(PyObject *obj)
|
PyObject_CheckReadBuffer(PyObject *obj)
|
||||||
{
|
{
|
||||||
PyBufferProcs *pb = obj->ob_type->tp_as_buffer;
|
PyBufferProcs *pb = obj->ob_type->tp_as_buffer;
|
||||||
|
|
||||||
if (pb == NULL ||
|
if (pb == NULL ||
|
||||||
pb->bf_getbuffer == NULL)
|
pb->bf_getbuffer == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
if ((*pb->bf_getbuffer)(obj, NULL, PyBUF_SIMPLE) == -1) {
|
if ((*pb->bf_getbuffer)(obj, NULL, PyBUF_SIMPLE) == -1) {
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (*pb->bf_releasebuffer != NULL)
|
if (*pb->bf_releasebuffer != NULL)
|
||||||
(*pb->bf_releasebuffer)(obj, NULL);
|
(*pb->bf_releasebuffer)(obj, NULL);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -267,7 +267,7 @@ int PyObject_AsReadBuffer(PyObject *obj,
|
||||||
Py_ssize_t *buffer_len)
|
Py_ssize_t *buffer_len)
|
||||||
{
|
{
|
||||||
PyBufferProcs *pb;
|
PyBufferProcs *pb;
|
||||||
Py_buffer view;
|
Py_buffer view;
|
||||||
|
|
||||||
if (obj == NULL || buffer == NULL || buffer_len == NULL) {
|
if (obj == NULL || buffer == NULL || buffer_len == NULL) {
|
||||||
null_error();
|
null_error();
|
||||||
|
@ -275,18 +275,18 @@ int PyObject_AsReadBuffer(PyObject *obj,
|
||||||
}
|
}
|
||||||
pb = obj->ob_type->tp_as_buffer;
|
pb = obj->ob_type->tp_as_buffer;
|
||||||
if (pb == NULL ||
|
if (pb == NULL ||
|
||||||
pb->bf_getbuffer == NULL) {
|
pb->bf_getbuffer == NULL) {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
"expected an object with a buffer interface");
|
"expected an object with a buffer interface");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE)) return -1;
|
if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE)) return -1;
|
||||||
|
|
||||||
*buffer = view.buf;
|
*buffer = view.buf;
|
||||||
*buffer_len = view.len;
|
*buffer_len = view.len;
|
||||||
if (pb->bf_releasebuffer != NULL)
|
if (pb->bf_releasebuffer != NULL)
|
||||||
(*pb->bf_releasebuffer)(obj, &view);
|
(*pb->bf_releasebuffer)(obj, &view);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -295,7 +295,7 @@ int PyObject_AsWriteBuffer(PyObject *obj,
|
||||||
Py_ssize_t *buffer_len)
|
Py_ssize_t *buffer_len)
|
||||||
{
|
{
|
||||||
PyBufferProcs *pb;
|
PyBufferProcs *pb;
|
||||||
Py_buffer view;
|
Py_buffer view;
|
||||||
|
|
||||||
if (obj == NULL || buffer == NULL || buffer_len == NULL) {
|
if (obj == NULL || buffer == NULL || buffer_len == NULL) {
|
||||||
null_error();
|
null_error();
|
||||||
|
@ -303,17 +303,17 @@ int PyObject_AsWriteBuffer(PyObject *obj,
|
||||||
}
|
}
|
||||||
pb = obj->ob_type->tp_as_buffer;
|
pb = obj->ob_type->tp_as_buffer;
|
||||||
if (pb == NULL ||
|
if (pb == NULL ||
|
||||||
pb->bf_getbuffer == NULL ||
|
pb->bf_getbuffer == NULL ||
|
||||||
((*pb->bf_getbuffer)(obj, &view, PyBUF_WRITABLE) != 0)) {
|
((*pb->bf_getbuffer)(obj, &view, PyBUF_WRITABLE) != 0)) {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
"expected an object with a writable buffer interface");
|
"expected an object with a writable buffer interface");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
*buffer = view.buf;
|
*buffer = view.buf;
|
||||||
*buffer_len = view.len;
|
*buffer_len = view.len;
|
||||||
if (pb->bf_releasebuffer != NULL)
|
if (pb->bf_releasebuffer != NULL)
|
||||||
(*pb->bf_releasebuffer)(obj, &view);
|
(*pb->bf_releasebuffer)(obj, &view);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -322,128 +322,128 @@ int PyObject_AsWriteBuffer(PyObject *obj,
|
||||||
int
|
int
|
||||||
PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
|
PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
|
||||||
{
|
{
|
||||||
if (!PyObject_CheckBuffer(obj)) {
|
if (!PyObject_CheckBuffer(obj)) {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
"object does not have the buffer interface");
|
"object does not have the buffer interface");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return (*(obj->ob_type->tp_as_buffer->bf_getbuffer))(obj, view, flags);
|
return (*(obj->ob_type->tp_as_buffer->bf_getbuffer))(obj, view, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
PyObject_ReleaseBuffer(PyObject *obj, Py_buffer *view)
|
PyObject_ReleaseBuffer(PyObject *obj, Py_buffer *view)
|
||||||
{
|
{
|
||||||
if (obj->ob_type->tp_as_buffer != NULL &&
|
if (obj->ob_type->tp_as_buffer != NULL &&
|
||||||
obj->ob_type->tp_as_buffer->bf_releasebuffer != NULL) {
|
obj->ob_type->tp_as_buffer->bf_releasebuffer != NULL) {
|
||||||
(*(obj->ob_type->tp_as_buffer->bf_releasebuffer))(obj, view);
|
(*(obj->ob_type->tp_as_buffer->bf_releasebuffer))(obj, view);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
_IsFortranContiguous(Py_buffer *view)
|
_IsFortranContiguous(Py_buffer *view)
|
||||||
{
|
{
|
||||||
Py_ssize_t sd, dim;
|
Py_ssize_t sd, dim;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (view->ndim == 0) return 1;
|
if (view->ndim == 0) return 1;
|
||||||
if (view->strides == NULL) return (view->ndim == 1);
|
if (view->strides == NULL) return (view->ndim == 1);
|
||||||
|
|
||||||
sd = view->itemsize;
|
sd = view->itemsize;
|
||||||
if (view->ndim == 1) return (view->shape[0] == 1 ||
|
if (view->ndim == 1) return (view->shape[0] == 1 ||
|
||||||
sd == view->strides[0]);
|
sd == view->strides[0]);
|
||||||
for (i=0; i<view->ndim; i++) {
|
for (i=0; i<view->ndim; i++) {
|
||||||
dim = view->shape[i];
|
dim = view->shape[i];
|
||||||
if (dim == 0) return 1;
|
if (dim == 0) return 1;
|
||||||
if (view->strides[i] != sd) return 0;
|
if (view->strides[i] != sd) return 0;
|
||||||
sd *= dim;
|
sd *= dim;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
_IsCContiguous(Py_buffer *view)
|
_IsCContiguous(Py_buffer *view)
|
||||||
{
|
{
|
||||||
Py_ssize_t sd, dim;
|
Py_ssize_t sd, dim;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (view->ndim == 0) return 1;
|
if (view->ndim == 0) return 1;
|
||||||
if (view->strides == NULL) return 1;
|
if (view->strides == NULL) return 1;
|
||||||
|
|
||||||
sd = view->itemsize;
|
sd = view->itemsize;
|
||||||
if (view->ndim == 1) return (view->shape[0] == 1 ||
|
if (view->ndim == 1) return (view->shape[0] == 1 ||
|
||||||
sd == view->strides[0]);
|
sd == view->strides[0]);
|
||||||
for (i=view->ndim-1; i>=0; i--) {
|
for (i=view->ndim-1; i>=0; i--) {
|
||||||
dim = view->shape[i];
|
dim = view->shape[i];
|
||||||
if (dim == 0) return 1;
|
if (dim == 0) return 1;
|
||||||
if (view->strides[i] != sd) return 0;
|
if (view->strides[i] != sd) return 0;
|
||||||
sd *= dim;
|
sd *= dim;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
PyBuffer_IsContiguous(Py_buffer *view, char fort)
|
PyBuffer_IsContiguous(Py_buffer *view, char fort)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (view->suboffsets != NULL) return 0;
|
if (view->suboffsets != NULL) return 0;
|
||||||
|
|
||||||
if (fort == 'C')
|
if (fort == 'C')
|
||||||
return _IsCContiguous(view);
|
return _IsCContiguous(view);
|
||||||
else if (fort == 'F')
|
else if (fort == 'F')
|
||||||
return _IsFortranContiguous(view);
|
return _IsFortranContiguous(view);
|
||||||
else if (fort == 'A')
|
else if (fort == 'A')
|
||||||
return (_IsCContiguous(view) || _IsFortranContiguous(view));
|
return (_IsCContiguous(view) || _IsFortranContiguous(view));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void*
|
void*
|
||||||
PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices)
|
PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices)
|
||||||
{
|
{
|
||||||
char* pointer;
|
char* pointer;
|
||||||
int i;
|
int i;
|
||||||
pointer = (char *)view->buf;
|
pointer = (char *)view->buf;
|
||||||
for (i = 0; i < view->ndim; i++) {
|
for (i = 0; i < view->ndim; i++) {
|
||||||
pointer += view->strides[i]*indices[i];
|
pointer += view->strides[i]*indices[i];
|
||||||
if ((view->suboffsets != NULL) && (view->suboffsets[i] >= 0)) {
|
if ((view->suboffsets != NULL) && (view->suboffsets[i] >= 0)) {
|
||||||
pointer = *((char**)pointer) + view->suboffsets[i];
|
pointer = *((char**)pointer) + view->suboffsets[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (void*)pointer;
|
return (void*)pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
_add_one_to_index_F(int nd, Py_ssize_t *index, Py_ssize_t *shape)
|
_add_one_to_index_F(int nd, Py_ssize_t *index, Py_ssize_t *shape)
|
||||||
{
|
{
|
||||||
int k;
|
int k;
|
||||||
|
|
||||||
for (k=0; k<nd; k++) {
|
for (k=0; k<nd; k++) {
|
||||||
if (index[k] < shape[k]-1) {
|
if (index[k] < shape[k]-1) {
|
||||||
index[k]++;
|
index[k]++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
index[k] = 0;
|
index[k] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
_add_one_to_index_C(int nd, Py_ssize_t *index, Py_ssize_t *shape)
|
_add_one_to_index_C(int nd, Py_ssize_t *index, Py_ssize_t *shape)
|
||||||
{
|
{
|
||||||
int k;
|
int k;
|
||||||
|
|
||||||
for (k=nd-1; k>=0; k--) {
|
for (k=nd-1; k>=0; k--) {
|
||||||
if (index[k] < shape[k]-1) {
|
if (index[k] < shape[k]-1) {
|
||||||
index[k]++;
|
index[k]++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
index[k] = 0;
|
index[k] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* view is not checked for consistency in either of these. It is
|
/* view is not checked for consistency in either of these. It is
|
||||||
|
@ -454,235 +454,235 @@ _add_one_to_index_C(int nd, Py_ssize_t *index, Py_ssize_t *shape)
|
||||||
int
|
int
|
||||||
PyBuffer_ToContiguous(void *buf, Py_buffer *view, Py_ssize_t len, char fort)
|
PyBuffer_ToContiguous(void *buf, Py_buffer *view, Py_ssize_t len, char fort)
|
||||||
{
|
{
|
||||||
int k;
|
int k;
|
||||||
void (*addone)(int, Py_ssize_t *, Py_ssize_t *);
|
void (*addone)(int, Py_ssize_t *, Py_ssize_t *);
|
||||||
Py_ssize_t *indices, elements;
|
Py_ssize_t *indices, elements;
|
||||||
char *dest, *ptr;
|
char *dest, *ptr;
|
||||||
|
|
||||||
if (len > view->len) {
|
if (len > view->len) {
|
||||||
len = view->len;
|
len = view->len;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PyBuffer_IsContiguous(view, fort)) {
|
if (PyBuffer_IsContiguous(view, fort)) {
|
||||||
/* simplest copy is all that is needed */
|
/* simplest copy is all that is needed */
|
||||||
memcpy(buf, view->buf, len);
|
memcpy(buf, view->buf, len);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Otherwise a more elaborate scheme is needed */
|
/* Otherwise a more elaborate scheme is needed */
|
||||||
|
|
||||||
/* XXX(nnorwitz): need to check for overflow! */
|
/* XXX(nnorwitz): need to check for overflow! */
|
||||||
indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
|
indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
|
||||||
if (indices == NULL) {
|
if (indices == NULL) {
|
||||||
PyErr_NoMemory();
|
PyErr_NoMemory();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
for (k=0; k<view->ndim;k++) {
|
for (k=0; k<view->ndim;k++) {
|
||||||
indices[k] = 0;
|
indices[k] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fort == 'F') {
|
if (fort == 'F') {
|
||||||
addone = _add_one_to_index_F;
|
addone = _add_one_to_index_F;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
addone = _add_one_to_index_C;
|
addone = _add_one_to_index_C;
|
||||||
}
|
}
|
||||||
dest = buf;
|
dest = buf;
|
||||||
/* XXX : This is not going to be the fastest code in the world
|
/* XXX : This is not going to be the fastest code in the world
|
||||||
several optimizations are possible.
|
several optimizations are possible.
|
||||||
*/
|
*/
|
||||||
elements = len / view->itemsize;
|
elements = len / view->itemsize;
|
||||||
while (elements--) {
|
while (elements--) {
|
||||||
addone(view->ndim, indices, view->shape);
|
addone(view->ndim, indices, view->shape);
|
||||||
ptr = PyBuffer_GetPointer(view, indices);
|
ptr = PyBuffer_GetPointer(view, indices);
|
||||||
memcpy(dest, ptr, view->itemsize);
|
memcpy(dest, ptr, view->itemsize);
|
||||||
dest += view->itemsize;
|
dest += view->itemsize;
|
||||||
}
|
}
|
||||||
PyMem_Free(indices);
|
PyMem_Free(indices);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
|
PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
|
||||||
{
|
{
|
||||||
int k;
|
int k;
|
||||||
void (*addone)(int, Py_ssize_t *, Py_ssize_t *);
|
void (*addone)(int, Py_ssize_t *, Py_ssize_t *);
|
||||||
Py_ssize_t *indices, elements;
|
Py_ssize_t *indices, elements;
|
||||||
char *src, *ptr;
|
char *src, *ptr;
|
||||||
|
|
||||||
if (len > view->len) {
|
if (len > view->len) {
|
||||||
len = view->len;
|
len = view->len;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PyBuffer_IsContiguous(view, fort)) {
|
if (PyBuffer_IsContiguous(view, fort)) {
|
||||||
/* simplest copy is all that is needed */
|
/* simplest copy is all that is needed */
|
||||||
memcpy(view->buf, buf, len);
|
memcpy(view->buf, buf, len);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Otherwise a more elaborate scheme is needed */
|
/* Otherwise a more elaborate scheme is needed */
|
||||||
|
|
||||||
/* XXX(nnorwitz): need to check for overflow! */
|
/* XXX(nnorwitz): need to check for overflow! */
|
||||||
indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
|
indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
|
||||||
if (indices == NULL) {
|
if (indices == NULL) {
|
||||||
PyErr_NoMemory();
|
PyErr_NoMemory();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
for (k=0; k<view->ndim;k++) {
|
for (k=0; k<view->ndim;k++) {
|
||||||
indices[k] = 0;
|
indices[k] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fort == 'F') {
|
if (fort == 'F') {
|
||||||
addone = _add_one_to_index_F;
|
addone = _add_one_to_index_F;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
addone = _add_one_to_index_C;
|
addone = _add_one_to_index_C;
|
||||||
}
|
}
|
||||||
src = buf;
|
src = buf;
|
||||||
/* XXX : This is not going to be the fastest code in the world
|
/* XXX : This is not going to be the fastest code in the world
|
||||||
several optimizations are possible.
|
several optimizations are possible.
|
||||||
*/
|
*/
|
||||||
elements = len / view->itemsize;
|
elements = len / view->itemsize;
|
||||||
while (elements--) {
|
while (elements--) {
|
||||||
addone(view->ndim, indices, view->shape);
|
addone(view->ndim, indices, view->shape);
|
||||||
ptr = PyBuffer_GetPointer(view, indices);
|
ptr = PyBuffer_GetPointer(view, indices);
|
||||||
memcpy(ptr, src, view->itemsize);
|
memcpy(ptr, src, view->itemsize);
|
||||||
src += view->itemsize;
|
src += view->itemsize;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyMem_Free(indices);
|
PyMem_Free(indices);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int PyObject_CopyData(PyObject *dest, PyObject *src)
|
int PyObject_CopyData(PyObject *dest, PyObject *src)
|
||||||
{
|
{
|
||||||
Py_buffer view_dest, view_src;
|
Py_buffer view_dest, view_src;
|
||||||
int k;
|
int k;
|
||||||
Py_ssize_t *indices, elements;
|
Py_ssize_t *indices, elements;
|
||||||
char *dptr, *sptr;
|
char *dptr, *sptr;
|
||||||
|
|
||||||
if (!PyObject_CheckBuffer(dest) ||
|
if (!PyObject_CheckBuffer(dest) ||
|
||||||
!PyObject_CheckBuffer(src)) {
|
!PyObject_CheckBuffer(src)) {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
"both destination and source must have the "\
|
"both destination and source must have the "\
|
||||||
"buffer interface");
|
"buffer interface");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PyObject_GetBuffer(dest, &view_dest, PyBUF_FULL) != 0) return -1;
|
if (PyObject_GetBuffer(dest, &view_dest, PyBUF_FULL) != 0) return -1;
|
||||||
if (PyObject_GetBuffer(src, &view_src, PyBUF_FULL_RO) != 0) {
|
if (PyObject_GetBuffer(src, &view_src, PyBUF_FULL_RO) != 0) {
|
||||||
PyObject_ReleaseBuffer(dest, &view_dest);
|
PyObject_ReleaseBuffer(dest, &view_dest);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (view_dest.len < view_src.len) {
|
if (view_dest.len < view_src.len) {
|
||||||
PyErr_SetString(PyExc_BufferError,
|
PyErr_SetString(PyExc_BufferError,
|
||||||
"destination is too small to receive data from source");
|
"destination is too small to receive data from source");
|
||||||
PyObject_ReleaseBuffer(dest, &view_dest);
|
PyObject_ReleaseBuffer(dest, &view_dest);
|
||||||
PyObject_ReleaseBuffer(src, &view_src);
|
PyObject_ReleaseBuffer(src, &view_src);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((PyBuffer_IsContiguous(&view_dest, 'C') &&
|
if ((PyBuffer_IsContiguous(&view_dest, 'C') &&
|
||||||
PyBuffer_IsContiguous(&view_src, 'C')) ||
|
PyBuffer_IsContiguous(&view_src, 'C')) ||
|
||||||
(PyBuffer_IsContiguous(&view_dest, 'F') &&
|
(PyBuffer_IsContiguous(&view_dest, 'F') &&
|
||||||
PyBuffer_IsContiguous(&view_src, 'F'))) {
|
PyBuffer_IsContiguous(&view_src, 'F'))) {
|
||||||
/* simplest copy is all that is needed */
|
/* simplest copy is all that is needed */
|
||||||
memcpy(view_dest.buf, view_src.buf, view_src.len);
|
memcpy(view_dest.buf, view_src.buf, view_src.len);
|
||||||
PyObject_ReleaseBuffer(dest, &view_dest);
|
PyObject_ReleaseBuffer(dest, &view_dest);
|
||||||
PyObject_ReleaseBuffer(src, &view_src);
|
PyObject_ReleaseBuffer(src, &view_src);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Otherwise a more elaborate copy scheme is needed */
|
/* Otherwise a more elaborate copy scheme is needed */
|
||||||
|
|
||||||
/* XXX(nnorwitz): need to check for overflow! */
|
/* XXX(nnorwitz): need to check for overflow! */
|
||||||
indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*view_src.ndim);
|
indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*view_src.ndim);
|
||||||
if (indices == NULL) {
|
if (indices == NULL) {
|
||||||
PyErr_NoMemory();
|
PyErr_NoMemory();
|
||||||
PyObject_ReleaseBuffer(dest, &view_dest);
|
PyObject_ReleaseBuffer(dest, &view_dest);
|
||||||
PyObject_ReleaseBuffer(src, &view_src);
|
PyObject_ReleaseBuffer(src, &view_src);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
for (k=0; k<view_src.ndim;k++) {
|
for (k=0; k<view_src.ndim;k++) {
|
||||||
indices[k] = 0;
|
indices[k] = 0;
|
||||||
}
|
}
|
||||||
elements = 1;
|
elements = 1;
|
||||||
for (k=0; k<view_src.ndim; k++) {
|
for (k=0; k<view_src.ndim; k++) {
|
||||||
/* XXX(nnorwitz): can this overflow? */
|
/* XXX(nnorwitz): can this overflow? */
|
||||||
elements *= view_src.shape[k];
|
elements *= view_src.shape[k];
|
||||||
}
|
}
|
||||||
while (elements--) {
|
while (elements--) {
|
||||||
_add_one_to_index_C(view_src.ndim, indices, view_src.shape);
|
_add_one_to_index_C(view_src.ndim, indices, view_src.shape);
|
||||||
dptr = PyBuffer_GetPointer(&view_dest, indices);
|
dptr = PyBuffer_GetPointer(&view_dest, indices);
|
||||||
sptr = PyBuffer_GetPointer(&view_src, indices);
|
sptr = PyBuffer_GetPointer(&view_src, indices);
|
||||||
memcpy(dptr, sptr, view_src.itemsize);
|
memcpy(dptr, sptr, view_src.itemsize);
|
||||||
}
|
}
|
||||||
PyMem_Free(indices);
|
PyMem_Free(indices);
|
||||||
PyObject_ReleaseBuffer(dest, &view_dest);
|
PyObject_ReleaseBuffer(dest, &view_dest);
|
||||||
PyObject_ReleaseBuffer(src, &view_src);
|
PyObject_ReleaseBuffer(src, &view_src);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
PyBuffer_FillContiguousStrides(int nd, Py_ssize_t *shape,
|
PyBuffer_FillContiguousStrides(int nd, Py_ssize_t *shape,
|
||||||
Py_ssize_t *strides, int itemsize,
|
Py_ssize_t *strides, int itemsize,
|
||||||
char fort)
|
char fort)
|
||||||
{
|
{
|
||||||
int k;
|
int k;
|
||||||
Py_ssize_t sd;
|
Py_ssize_t sd;
|
||||||
|
|
||||||
sd = itemsize;
|
sd = itemsize;
|
||||||
if (fort == 'F') {
|
if (fort == 'F') {
|
||||||
for (k=0; k<nd; k++) {
|
for (k=0; k<nd; k++) {
|
||||||
strides[k] = sd;
|
strides[k] = sd;
|
||||||
sd *= shape[k];
|
sd *= shape[k];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
for (k=nd-1; k>=0; k--) {
|
for (k=nd-1; k>=0; k--) {
|
||||||
strides[k] = sd;
|
strides[k] = sd;
|
||||||
sd *= shape[k];
|
sd *= shape[k];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
PyBuffer_FillInfo(Py_buffer *view, void *buf, Py_ssize_t len,
|
PyBuffer_FillInfo(Py_buffer *view, void *buf, Py_ssize_t len,
|
||||||
int readonly, int flags)
|
int readonly, int flags)
|
||||||
{
|
{
|
||||||
if (view == NULL) return 0;
|
if (view == NULL) return 0;
|
||||||
if (((flags & PyBUF_LOCKDATA) == PyBUF_LOCKDATA) &&
|
if (((flags & PyBUF_LOCK) == PyBUF_LOCK) &&
|
||||||
readonly != -1) {
|
readonly >= 0) {
|
||||||
PyErr_SetString(PyExc_BufferError,
|
PyErr_SetString(PyExc_BufferError,
|
||||||
"Cannot make this object read-only.");
|
"Cannot lock this object.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) &&
|
if (((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) &&
|
||||||
readonly == 1) {
|
(readonly == 1)) {
|
||||||
PyErr_SetString(PyExc_BufferError,
|
PyErr_SetString(PyExc_BufferError,
|
||||||
"Object is not writable.");
|
"Object is not writable.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
view->buf = buf;
|
view->buf = buf;
|
||||||
view->len = len;
|
view->len = len;
|
||||||
view->readonly = readonly;
|
view->readonly = readonly;
|
||||||
view->itemsize = 1;
|
view->itemsize = 1;
|
||||||
view->format = NULL;
|
view->format = NULL;
|
||||||
if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
|
if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
|
||||||
view->format = "B";
|
view->format = "B";
|
||||||
view->ndim = 1;
|
view->ndim = 1;
|
||||||
view->shape = NULL;
|
view->shape = NULL;
|
||||||
if ((flags & PyBUF_ND) == PyBUF_ND)
|
if ((flags & PyBUF_ND) == PyBUF_ND)
|
||||||
view->shape = &(view->len);
|
view->shape = &(view->len);
|
||||||
view->strides = NULL;
|
view->strides = NULL;
|
||||||
if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES)
|
if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES)
|
||||||
view->strides = &(view->itemsize);
|
view->strides = &(view->itemsize);
|
||||||
view->suboffsets = NULL;
|
view->suboffsets = NULL;
|
||||||
view->internal = NULL;
|
view->internal = NULL;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Operations on numbers */
|
/* Operations on numbers */
|
||||||
|
@ -1025,7 +1025,7 @@ PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
|
||||||
Py_DECREF(result);
|
Py_DECREF(result);
|
||||||
if (m != NULL) {
|
if (m != NULL) {
|
||||||
binaryfunc f = NULL;
|
binaryfunc f = NULL;
|
||||||
f = m->sq_inplace_concat;
|
f = m->sq_inplace_concat;
|
||||||
if (f == NULL)
|
if (f == NULL)
|
||||||
f = m->sq_concat;
|
f = m->sq_concat;
|
||||||
if (f != NULL)
|
if (f != NULL)
|
||||||
|
@ -1304,8 +1304,8 @@ PyNumber_Float(PyObject *o)
|
||||||
PyObject *res = m->nb_float(o);
|
PyObject *res = m->nb_float(o);
|
||||||
if (res && !PyFloat_Check(res)) {
|
if (res && !PyFloat_Check(res)) {
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"__float__ returned non-float (type %.200s)",
|
"__float__ returned non-float (type %.200s)",
|
||||||
res->ob_type->tp_name);
|
res->ob_type->tp_name);
|
||||||
Py_DECREF(res);
|
Py_DECREF(res);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -1384,7 +1384,7 @@ PySequence_Concat(PyObject *s, PyObject *o)
|
||||||
return m->sq_concat(s, o);
|
return m->sq_concat(s, o);
|
||||||
|
|
||||||
/* Instances of user classes defining an __add__() method only
|
/* Instances of user classes defining an __add__() method only
|
||||||
have an nb_add slot, not an sq_concat slot. So we fall back
|
have an nb_add slot, not an sq_concat slot. So we fall back
|
||||||
to nb_add if both arguments appear to be sequences. */
|
to nb_add if both arguments appear to be sequences. */
|
||||||
if (PySequence_Check(s) && PySequence_Check(o)) {
|
if (PySequence_Check(s) && PySequence_Check(o)) {
|
||||||
PyObject *result = binary_op1(s, o, NB_SLOT(nb_add));
|
PyObject *result = binary_op1(s, o, NB_SLOT(nb_add));
|
||||||
|
@ -1629,7 +1629,7 @@ PyObject *
|
||||||
PySequence_Tuple(PyObject *v)
|
PySequence_Tuple(PyObject *v)
|
||||||
{
|
{
|
||||||
PyObject *it; /* iter(v) */
|
PyObject *it; /* iter(v) */
|
||||||
Py_ssize_t n; /* guess for result tuple size */
|
Py_ssize_t n; /* guess for result tuple size */
|
||||||
PyObject *result;
|
PyObject *result;
|
||||||
Py_ssize_t j;
|
Py_ssize_t j;
|
||||||
|
|
||||||
|
@ -1662,7 +1662,7 @@ PySequence_Tuple(PyObject *v)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
n = 10; /* arbitrary */
|
n = 10; /* arbitrary */
|
||||||
}
|
}
|
||||||
result = PyTuple_New(n);
|
result = PyTuple_New(n);
|
||||||
if (result == NULL)
|
if (result == NULL)
|
||||||
|
@ -1718,7 +1718,7 @@ PyObject *
|
||||||
PySequence_List(PyObject *v)
|
PySequence_List(PyObject *v)
|
||||||
{
|
{
|
||||||
PyObject *result; /* result list */
|
PyObject *result; /* result list */
|
||||||
PyObject *rv; /* return value from PyList_Extend */
|
PyObject *rv; /* return value from PyList_Extend */
|
||||||
|
|
||||||
if (v == NULL)
|
if (v == NULL)
|
||||||
return null_error();
|
return null_error();
|
||||||
|
@ -1749,7 +1749,7 @@ PySequence_Fast(PyObject *v, const char *m)
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
it = PyObject_GetIter(v);
|
it = PyObject_GetIter(v);
|
||||||
if (it == NULL) {
|
if (it == NULL) {
|
||||||
if (PyErr_ExceptionMatches(PyExc_TypeError))
|
if (PyErr_ExceptionMatches(PyExc_TypeError))
|
||||||
PyErr_SetString(PyExc_TypeError, m);
|
PyErr_SetString(PyExc_TypeError, m);
|
||||||
|
@ -1763,9 +1763,9 @@ PySequence_Fast(PyObject *v, const char *m)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Iterate over seq. Result depends on the operation:
|
/* Iterate over seq. Result depends on the operation:
|
||||||
PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq.
|
PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq.
|
||||||
PY_ITERSEARCH_INDEX: 0-based index of first occurence of obj in seq;
|
PY_ITERSEARCH_INDEX: 0-based index of first occurence of obj in seq;
|
||||||
set ValueError and return -1 if none found; also return -1 on error.
|
set ValueError and return -1 if none found; also return -1 on error.
|
||||||
Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error.
|
Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error.
|
||||||
*/
|
*/
|
||||||
Py_ssize_t
|
Py_ssize_t
|
||||||
|
@ -1839,7 +1839,7 @@ _PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
|
||||||
goto Done;
|
goto Done;
|
||||||
|
|
||||||
PyErr_SetString(PyExc_ValueError,
|
PyErr_SetString(PyExc_ValueError,
|
||||||
"sequence.index(x): x not in sequence");
|
"sequence.index(x): x not in sequence");
|
||||||
/* fall into failure code */
|
/* fall into failure code */
|
||||||
Fail:
|
Fail:
|
||||||
n = -1;
|
n = -1;
|
||||||
|
@ -1865,7 +1865,7 @@ PySequence_Contains(PyObject *seq, PyObject *ob)
|
||||||
{
|
{
|
||||||
Py_ssize_t result;
|
Py_ssize_t result;
|
||||||
PySequenceMethods *sqm = seq->ob_type->tp_as_sequence;
|
PySequenceMethods *sqm = seq->ob_type->tp_as_sequence;
|
||||||
if (sqm != NULL && sqm->sq_contains != NULL)
|
if (sqm != NULL && sqm->sq_contains != NULL)
|
||||||
return (*sqm->sq_contains)(seq, ob);
|
return (*sqm->sq_contains)(seq, ob);
|
||||||
result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
|
result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
|
||||||
return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
|
return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
|
||||||
|
@ -1890,7 +1890,7 @@ PySequence_Index(PyObject *s, PyObject *o)
|
||||||
int
|
int
|
||||||
PyMapping_Check(PyObject *o)
|
PyMapping_Check(PyObject *o)
|
||||||
{
|
{
|
||||||
return o && o->ob_type->tp_as_mapping &&
|
return o && o->ob_type->tp_as_mapping &&
|
||||||
o->ob_type->tp_as_mapping->mp_subscript;
|
o->ob_type->tp_as_mapping->mp_subscript;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2044,7 +2044,7 @@ PyObject_CallObject(PyObject *o, PyObject *a)
|
||||||
PyObject *
|
PyObject *
|
||||||
PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw)
|
PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw)
|
||||||
{
|
{
|
||||||
ternaryfunc call;
|
ternaryfunc call;
|
||||||
|
|
||||||
if ((call = func->ob_type->tp_call) != NULL) {
|
if ((call = func->ob_type->tp_call) != NULL) {
|
||||||
PyObject *result;
|
PyObject *result;
|
||||||
|
@ -2308,7 +2308,7 @@ PyObject_CallFunctionObjArgs(PyObject *callable, ...)
|
||||||
* produce exactly the same results: NULL is returned and no error is set.
|
* produce exactly the same results: NULL is returned and no error is set.
|
||||||
*
|
*
|
||||||
* If some exception other than AttributeError is raised, then NULL is also
|
* If some exception other than AttributeError is raised, then NULL is also
|
||||||
* returned, but the exception is not cleared. That's because we want the
|
* returned, but the exception is not cleared. That's because we want the
|
||||||
* exception to be propagated along.
|
* exception to be propagated along.
|
||||||
*
|
*
|
||||||
* Callers are expected to test for PyErr_Occurred() when the return value
|
* Callers are expected to test for PyErr_Occurred() when the return value
|
||||||
|
@ -2336,7 +2336,7 @@ abstract_get_bases(PyObject *cls)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (!PyTuple_Check(bases)) {
|
if (!PyTuple_Check(bases)) {
|
||||||
Py_DECREF(bases);
|
Py_DECREF(bases);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return bases;
|
return bases;
|
||||||
|
@ -2428,18 +2428,18 @@ recursive_isinstance(PyObject *inst, PyObject *cls, int recursion_depth)
|
||||||
else if (PyTuple_Check(cls)) {
|
else if (PyTuple_Check(cls)) {
|
||||||
Py_ssize_t i, n;
|
Py_ssize_t i, n;
|
||||||
|
|
||||||
if (!recursion_depth) {
|
if (!recursion_depth) {
|
||||||
PyErr_SetString(PyExc_RuntimeError,
|
PyErr_SetString(PyExc_RuntimeError,
|
||||||
"nest level of tuple too deep");
|
"nest level of tuple too deep");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
n = PyTuple_GET_SIZE(cls);
|
n = PyTuple_GET_SIZE(cls);
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
retval = recursive_isinstance(
|
retval = recursive_isinstance(
|
||||||
inst,
|
inst,
|
||||||
PyTuple_GET_ITEM(cls, i),
|
PyTuple_GET_ITEM(cls, i),
|
||||||
recursion_depth-1);
|
recursion_depth-1);
|
||||||
if (retval != 0)
|
if (retval != 0)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -2490,12 +2490,12 @@ PyObject_IsInstance(PyObject *inst, PyObject *cls)
|
||||||
return recursive_isinstance(inst, cls, Py_GetRecursionLimit());
|
return recursive_isinstance(inst, cls, Py_GetRecursionLimit());
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
recursive_issubclass(PyObject *derived, PyObject *cls, int recursion_depth)
|
recursive_issubclass(PyObject *derived, PyObject *cls, int recursion_depth)
|
||||||
{
|
{
|
||||||
int retval;
|
int retval;
|
||||||
|
|
||||||
{
|
{
|
||||||
if (!check_class(derived,
|
if (!check_class(derived,
|
||||||
"issubclass() arg 1 must be a class"))
|
"issubclass() arg 1 must be a class"))
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -2504,16 +2504,16 @@ recursive_issubclass(PyObject *derived, PyObject *cls, int recursion_depth)
|
||||||
Py_ssize_t i;
|
Py_ssize_t i;
|
||||||
Py_ssize_t n = PyTuple_GET_SIZE(cls);
|
Py_ssize_t n = PyTuple_GET_SIZE(cls);
|
||||||
|
|
||||||
if (!recursion_depth) {
|
if (!recursion_depth) {
|
||||||
PyErr_SetString(PyExc_RuntimeError,
|
PyErr_SetString(PyExc_RuntimeError,
|
||||||
"nest level of tuple too deep");
|
"nest level of tuple too deep");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
for (i = 0; i < n; ++i) {
|
for (i = 0; i < n; ++i) {
|
||||||
retval = recursive_issubclass(
|
retval = recursive_issubclass(
|
||||||
derived,
|
derived,
|
||||||
PyTuple_GET_ITEM(cls, i),
|
PyTuple_GET_ITEM(cls, i),
|
||||||
recursion_depth-1);
|
recursion_depth-1);
|
||||||
if (retval != 0) {
|
if (retval != 0) {
|
||||||
/* either found it, or got an error */
|
/* either found it, or got an error */
|
||||||
return retval;
|
return retval;
|
||||||
|
@ -2590,7 +2590,7 @@ PyObject_GetIter(PyObject *o)
|
||||||
* If the iteration terminates normally, return NULL and clear the
|
* If the iteration terminates normally, return NULL and clear the
|
||||||
* PyExc_StopIteration exception (if it was set). PyErr_Occurred()
|
* PyExc_StopIteration exception (if it was set). PyErr_Occurred()
|
||||||
* will be false.
|
* will be false.
|
||||||
* Else return the next object. PyErr_Occurred() will be false.
|
* Else return the next object. PyErr_Occurred() will be false.
|
||||||
*/
|
*/
|
||||||
PyObject *
|
PyObject *
|
||||||
PyIter_Next(PyObject *iter)
|
PyIter_Next(PyObject *iter)
|
||||||
|
|
|
@ -196,7 +196,9 @@ _indirect_copy_nd(char *dest, Py_buffer *view, char fort)
|
||||||
a contiguous buffer if it is not. The view will point to
|
a contiguous buffer if it is not. The view will point to
|
||||||
the shadow buffer which can be written to and then
|
the shadow buffer which can be written to and then
|
||||||
will be copied back into the other buffer when the memory
|
will be copied back into the other buffer when the memory
|
||||||
view is de-allocated.
|
view is de-allocated. While the shadow buffer is
|
||||||
|
being used, it will have an exclusive write lock on
|
||||||
|
the original buffer.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
|
@ -224,7 +226,7 @@ PyMemoryView_GetContiguous(PyObject *obj, int buffertype, char fort)
|
||||||
flags = PyBUF_FULL;
|
flags = PyBUF_FULL;
|
||||||
break;
|
break;
|
||||||
case PyBUF_SHADOW:
|
case PyBUF_SHADOW:
|
||||||
flags = PyBUF_FULL_LCK;
|
flags = PyBUF_FULL_XLCK;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -431,11 +433,7 @@ memory_dealloc(PyMemoryViewObject *self)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
memory_repr(PyMemoryViewObject *self)
|
memory_repr(PyMemoryViewObject *self)
|
||||||
{
|
{
|
||||||
/* XXX(nnorwitz): the code should be different or remove condition. */
|
return PyUnicode_FromFormat("<memory at %p>", self);
|
||||||
if (self->base == NULL)
|
|
||||||
return PyUnicode_FromFormat("<memory at %p>", self);
|
|
||||||
else
|
|
||||||
return PyUnicode_FromFormat("<memory at %p>", self);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -502,6 +500,14 @@ memory_subscript(PyMemoryViewObject *self, PyObject *key)
|
||||||
/* Return a bytes object */
|
/* Return a bytes object */
|
||||||
char *ptr;
|
char *ptr;
|
||||||
ptr = (char *)view->buf;
|
ptr = (char *)view->buf;
|
||||||
|
if (result < 0) {
|
||||||
|
result += view->shape[0];
|
||||||
|
}
|
||||||
|
if ((result < 0) || (result > view->shape[0])) {
|
||||||
|
PyErr_SetString(PyExc_IndexError,
|
||||||
|
"index out of bounds");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
if (view->strides == NULL)
|
if (view->strides == NULL)
|
||||||
ptr += view->itemsize * result;
|
ptr += view->itemsize * result;
|
||||||
else
|
else
|
||||||
|
@ -517,14 +523,20 @@ memory_subscript(PyMemoryViewObject *self, PyObject *key)
|
||||||
/* Return a new memory-view object */
|
/* Return a new memory-view object */
|
||||||
Py_buffer newview;
|
Py_buffer newview;
|
||||||
memset(&newview, 0, sizeof(newview));
|
memset(&newview, 0, sizeof(newview));
|
||||||
|
/* XXX: This needs to be fixed so it
|
||||||
|
actually returns a sub-view
|
||||||
|
*/
|
||||||
return PyMemoryView_FromMemory(&newview);
|
return PyMemoryView_FromMemory(&newview);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Need to support getting a sliced view */
|
||||||
Py_INCREF(Py_NotImplemented);
|
Py_INCREF(Py_NotImplemented);
|
||||||
return Py_NotImplemented;
|
return Py_NotImplemented;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Need to support assigning memory if we can */
|
||||||
static int
|
static int
|
||||||
memory_ass_sub(PyMemoryViewObject *self, PyObject *key, PyObject *value)
|
memory_ass_sub(PyMemoryViewObject *self, PyObject *key, PyObject *value)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue