Commit
Unfortunately cannot use default args in pxd file; I also wouldn't know how the two array functions could be added as methods to the array class. Restored "inline" attribute for array creation function, it can be disabled with a \#DEF Removed some unused/redundant attributes. Moved selected comments to docstrings. Replaced tabs w/spaces.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
See array.pxd next to this file | ||
last changes: 2009-05-15 rk | ||
2012-05-02 andreasvc | ||
*/ | ||
|
||
|
@@ -21,24 +22,24 @@ struct arrayobject; /* Forward */ | |
* functions aren't visible yet. | ||
*/ | ||
typedef struct arraydescr { | ||
int typecode; | ||
int itemsize; | ||
PyObject * (*getitem)(struct arrayobject *, Py_ssize_t); | ||
int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *); | ||
int typecode; | ||
int itemsize; | ||
PyObject * (*getitem)(struct arrayobject *, Py_ssize_t); | ||
int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *); | ||
#if PY_VERSION_HEX >= 0x03000000 | ||
char *formats; | ||
#endif | ||
} arraydescr; | ||
|
||
|
||
typedef struct arrayobject { | ||
PyObject_HEAD | ||
PyObject_HEAD | ||
union { | ||
int ob_size; | ||
Py_ssize_t ob_size; | ||
unsigned length; | ||
}; | ||
union { | ||
char *ob_item; | ||
char *ob_item; | ||
float *_f; | ||
double *_d; | ||
int *_i; | ||
|
@@ -54,11 +55,11 @@ typedef struct arrayobject { | |
void *_v; | ||
}; | ||
#if PY_VERSION_HEX >= 0x02040000 | ||
Py_ssize_t allocated; | ||
Py_ssize_t allocated; | ||
#endif | ||
struct arraydescr *ob_descr; | ||
struct arraydescr *ob_descr; | ||
#if PY_VERSION_HEX >= 0x02040000 | ||
PyObject *weakreflist; /* List of weak references */ | ||
PyObject *weakreflist; /* List of weak references */ | ||
#if PY_VERSION_HEX >= 0x03000000 | ||
int ob_exports; /* Number of exported buffers */ | ||
#endif | ||
|
@@ -69,50 +70,50 @@ typedef struct arrayobject { | |
#ifndef NO_NEWARRAY_INLINE | ||
/* | ||
* | ||
* fast creation of a new array - init with zeros | ||
* fast creation of a new array | ||
*/ | ||
|
||
PyObject * newarrayobject | ||
(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr) { | ||
arrayobject *op; | ||
size_t nbytes; | ||
|
||
if (size < 0) { | ||
PyErr_BadInternalCall(); | ||
return NULL; | ||
} | ||
|
||
nbytes = size * descr->itemsize; | ||
/* Check for overflow */ | ||
if (nbytes / descr->itemsize != (size_t)size) { | ||
return PyErr_NoMemory(); | ||
} | ||
op = (arrayobject *) type->tp_alloc(type, 0); | ||
if (op == NULL) { | ||
return NULL; | ||
} | ||
op->ob_descr = descr; | ||
inline PyObject * newarrayobject(PyTypeObject *type, Py_ssize_t size, | ||
This comment has been minimized.
Sorry, something went wrong.
scoder
|
||
struct arraydescr *descr) { | ||
arrayobject *op; | ||
size_t nbytes; | ||
|
||
if (size < 0) { | ||
PyErr_BadInternalCall(); | ||
return NULL; | ||
} | ||
|
||
nbytes = size * descr->itemsize; | ||
/* Check for overflow */ | ||
if (nbytes / descr->itemsize != (size_t)size) { | ||
return PyErr_NoMemory(); | ||
} | ||
op = (arrayobject *) type->tp_alloc(type, 0); | ||
if (op == NULL) { | ||
return NULL; | ||
} | ||
op->ob_descr = descr; | ||
#if !( PY_VERSION_HEX < 0x02040000 ) | ||
op->allocated = size; | ||
op->weakreflist = NULL; | ||
op->allocated = size; | ||
op->weakreflist = NULL; | ||
#endif | ||
Py_SIZE(op) = size; | ||
if (size <= 0) { | ||
op->ob_item = NULL; | ||
} | ||
else { | ||
op->ob_item = PyMem_NEW(char, nbytes); | ||
if (op->ob_item == NULL) { | ||
Py_DECREF(op); | ||
return PyErr_NoMemory(); | ||
} | ||
} | ||
return (PyObject *) op; | ||
Py_SIZE(op) = size; | ||
if (size <= 0) { | ||
op->ob_item = NULL; | ||
} | ||
else { | ||
op->ob_item = PyMem_NEW(char, nbytes); | ||
if (op->ob_item == NULL) { | ||
Py_DECREF(op); | ||
return PyErr_NoMemory(); | ||
} | ||
} | ||
return (PyObject *) op; | ||
} | ||
#else | ||
PyObject * | ||
newarrayobject(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr); | ||
#endif | ||
PyObject* newarrayobject(PyTypeObject *type, Py_ssize_t size, | ||
struct arraydescr *descr); | ||
#endif /* ifndef NO_NEWARRAY_INLINE */ | ||
|
||
/* fast resize (reallocation to the point) | ||
not designed for filing small increments (but for fast opaque array apps) */ | ||
|
@@ -143,7 +144,7 @@ int resize_smart(arrayobject *self, Py_ssize_t n) { | |
return 0; | ||
} | ||
} | ||
newsize = n * 3 / 2 + 1; | ||
newsize = n * 3 / 2 + 1; | ||
PyMem_Resize(item, char, (size_t)(newsize * self->ob_descr->itemsize)); | ||
if (item == NULL) { | ||
PyErr_NoMemory(); | ||
|
3 comments
on commit 13d9929
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder whether getbuffer and releasebuffer are important. Right now they just generate warnings on every compile and I'm tempted to just remove them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the tests. I should say I haven't figured out how to run them. When I run the whole Cython test suite the array module doesn't get compiled correctly, but I wouldn't know how to debug it.
This line isn't needed - Py_UNICODE is a built-in type in Cython.