diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-07-02 02:59:54 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-07-02 02:59:54 +0000 |
commit | da42cda6ebd4666299c1287b8fdec82df15197fe (patch) | |
tree | 09b0657a2556c799e67e9e63da70019386a9c80f | |
parent | c00b40099043644cca23b6044c276ad72e9b8591 (diff) | |
download | numpy-da42cda6ebd4666299c1287b8fdec82df15197fe.tar.gz |
Remove dependency on _internal.py from pickles. Clean up Py_ssize_t usage. Add .ctypes attribute for use with the ctypes module if it's available.
-rw-r--r-- | benchmarks/casting.py | 8 | ||||
-rw-r--r-- | benchmarks/creating.py | 8 | ||||
-rw-r--r-- | numpy/core/_internal.py | 43 | ||||
-rw-r--r-- | numpy/core/include/numpy/arrayobject.h | 8 | ||||
-rw-r--r-- | numpy/core/numerictypes.py | 5 | ||||
-rw-r--r-- | numpy/core/src/arraymethods.c | 2 | ||||
-rw-r--r-- | numpy/core/src/arrayobject.c | 59 | ||||
-rw-r--r-- | numpy/core/src/multiarraymodule.c | 36 | ||||
-rw-r--r-- | numpy/core/src/scalartypes.inc.src | 32 |
9 files changed, 142 insertions, 59 deletions
diff --git a/benchmarks/casting.py b/benchmarks/casting.py index 84558a088..cb498db10 100644 --- a/benchmarks/casting.py +++ b/benchmarks/casting.py @@ -1,9 +1,9 @@ import timeit -N = [1000,100] +N = [10,10] t1 = timeit.Timer('b = a.astype(int)','import numpy;a=numpy.zeros(shape=%s,dtype=float)'%N) t2 = timeit.Timer('b = a.astype("l")','import Numeric;a=Numeric.zeros(shape=%s,typecode="d")'%N) t3 = timeit.Timer("b = a.astype('l')","import numarray; a=numarray.zeros(shape=%s,typecode='d')"%N) print "1-D length = ", N -print "NumPy: ", t1.repeat(3,100) -print "Numeric: ", t2.repeat(3,100) -print "Numarray: ", t3.repeat(3,100) +print "NumPy: ", t1.repeat(3,1000) +print "Numeric: ", t2.repeat(3,1000) +print "Numarray: ", t3.repeat(3,1000) diff --git a/benchmarks/creating.py b/benchmarks/creating.py index b8bb30f47..462d2cd19 100644 --- a/benchmarks/creating.py +++ b/benchmarks/creating.py @@ -1,9 +1,9 @@ import timeit -N = [1000,100] +N = [10,10] t1 = timeit.Timer('a=N.zeros(shape,type)','import numpy as N; shape=%s;type=float'%N) t2 = timeit.Timer('a=N.zeros(shape,type)','import Numeric as N; shape=%s;type=N.Float'%N) t3 = timeit.Timer('a=N.zeros(shape,type)',"import numarray as N; shape=%s;type=N.Float"%N) print "shape = ", N -print "NumPy: ", t1.repeat(3,100) -print "Numeric: ", t2.repeat(3,100) -print "Numarray: ", t3.repeat(3,100) +print "NumPy: ", t1.repeat(3,10000) +print "Numeric: ", t2.repeat(3,10000) +print "Numarray: ", t3.repeat(3,10000) diff --git a/numpy/core/_internal.py b/numpy/core/_internal.py index abd0342b6..f93087aec 100644 --- a/numpy/core/_internal.py +++ b/numpy/core/_internal.py @@ -102,7 +102,8 @@ def _array_descr(descriptor): # Build a new array from the information in a pickle. # Note that the name numpy.core._internal._reconstruct is embedded in -# the pickles of ndarrays, so don't remove the name here, or you'll +# pickles of ndarrays made with NumPy before release 1.0 +# so don't remove the name here, or you'll # break backward compatibilty. def _reconstruct(subtype, shape, dtype): return ndarray.__new__(subtype, shape, dtype) @@ -168,4 +169,42 @@ def _commastring(astr): return result - +def _getintp_ctype(): + if _getintp_ctype.cache: + return _getintp_ctype.cache + import ctypes + char = dtype('p').char + if (char == 'i'): + val = ctypes.c_int + elif char == 'l': + val = ctypes.c_long + elif char == 'q': + val = ctypes.c_longlong + else: + raise ValueError, "confused about intp->ctypes." + _getintp_ctype.cache = val + return val +_getintp_ctype.cache = None + +# Used for .ctypes attribute of ndarray +class _ctypes(object): + def __init__(self, array): + try: + import ctypes + self._ctypes = ctypes + except ImportError: + raise AttributeError, "ctypes not available" + self._arr = array + + def get_data(self): + return self._ctypes.c_void_p(self._arr.__array_interface__['data'][0]) + + def get_dims(self): + return (_getintp_ctype()*self._arr.ndim)(*self._arr.shape) + + def get_strides(self): + return (_getintp_ctype()*self._arr.ndim)(*self._arr.strides) + + data = property(get_data, None, doc="c-types data") + dims = property(get_dims, None, doc="c-types dims") + strides = property(get_strides, None, doc="c-types strides") diff --git a/numpy/core/include/numpy/arrayobject.h b/numpy/core/include/numpy/arrayobject.h index d6b41565c..438b2c5a3 100644 --- a/numpy/core/include/numpy/arrayobject.h +++ b/numpy/core/include/numpy/arrayobject.h @@ -730,10 +730,10 @@ typedef Py_uintptr_t uintp; #define SIZEOF_INTP SIZEOF_PY_INTPTR_T #define SIZEOF_UINTP SIZEOF_PY_INTPTR_T -#if PY_VERSION_HEX >= 0x02050000 -#define _int_or_ssize_t Py_ssize_t -#else -#define _int_or_ssize_t int +#if (PY_VERSION_HEX < 0x02050000) +typedef int Py_ssize_t; +#define PY_SSIZE_T_MAX INT_MAX +#define PY_SSIZE_T_MIN INT_MIN #endif #if SIZEOF_PY_INTPTR_T == SIZEOF_INT diff --git a/numpy/core/numerictypes.py b/numpy/core/numerictypes.py index ae9dc0a27..ecdc4c78c 100644 --- a/numpy/core/numerictypes.py +++ b/numpy/core/numerictypes.py @@ -76,8 +76,9 @@ $Id: numerictypes.py,v 1.17 2005/09/09 22:20:06 teoliphant Exp $ """ # we add more at the bottom -__all__ = ['typeDict', 'typeNA', 'sctypes', 'ScalarType', 'obj2sctype', 'cast', 'nbytes', - 'sctype2char', 'maximum_sctype', 'issctype', 'typecodes'] +__all__ = ['typeDict', 'typeNA', 'sctypes', 'ScalarType', 'obj2sctype', + 'cast', 'nbytes', 'sctype2char', 'maximum_sctype', 'issctype', + 'typecodes'] from multiarray import typeinfo, ndarray, array, empty import types as _types diff --git a/numpy/core/src/arraymethods.c b/numpy/core/src/arraymethods.c index d4567401b..352300779 100644 --- a/numpy/core/src/arraymethods.c +++ b/numpy/core/src/arraymethods.c @@ -877,7 +877,7 @@ array_reduce(PyArrayObject *self, PyObject *args) ret = PyTuple_New(3); if (ret == NULL) return NULL; - mod = PyImport_ImportModule("numpy.core._internal"); + mod = PyImport_ImportModule("numpy.core.multiarray"); if (mod == NULL) {Py_DECREF(ret); return NULL;} obj = PyObject_GetAttrString(mod, "_reconstruct"); Py_DECREF(mod); diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c index 3a3e0f4b1..ccc068692 100644 --- a/numpy/core/src/arrayobject.c +++ b/numpy/core/src/arrayobject.c @@ -1771,7 +1771,7 @@ array_dealloc(PyArrayObject *self) { **************** Implement Mapping Protocol *************************** *************************************************************************/ -static _int_or_ssize_t +static Py_ssize_t array_length(PyArrayObject *self) { if (self->nd != 0) { @@ -1812,7 +1812,7 @@ array_big_item(PyArrayObject *self, intp i) /* contains optimization for 1-d arrays */ static PyObject * -array_item_nice(PyArrayObject *self, _int_or_ssize_t i) +array_item_nice(PyArrayObject *self, Py_ssize_t i) { if (self->nd == 1) { char *item; @@ -1877,7 +1877,7 @@ array_ass_big_item(PyArrayObject *self, intp i, PyObject *v) #endif #ifndef array_ass_item static int -array_ass_item(PyArrayObject *self, _int_or_ssize_t i, PyObject *v) +array_ass_item(PyArrayObject *self, Py_ssize_t i, PyObject *v) { return array_ass_big_item(self, (intp) i, v); } @@ -2825,8 +2825,8 @@ static PyMappingMethods array_as_mapping = { /* removed multiple segment interface */ -static _int_or_ssize_t -array_getsegcount(PyArrayObject *self, _int_or_ssize_t *lenp) +static Py_ssize_t +array_getsegcount(PyArrayObject *self, Py_ssize_t *lenp) { if (lenp) *lenp = PyArray_NBYTES(self); @@ -2840,8 +2840,8 @@ array_getsegcount(PyArrayObject *self, _int_or_ssize_t *lenp) return 0; } -static _int_or_ssize_t -array_getreadbuf(PyArrayObject *self, _int_or_ssize_t segment, void **ptrptr) +static Py_ssize_t +array_getreadbuf(PyArrayObject *self, Py_ssize_t segment, void **ptrptr) { if (segment != 0) { PyErr_SetString(PyExc_ValueError, @@ -2859,8 +2859,8 @@ array_getreadbuf(PyArrayObject *self, _int_or_ssize_t segment, void **ptrptr) } -static _int_or_ssize_t -array_getwritebuf(PyArrayObject *self, _int_or_ssize_t segment, void **ptrptr) +static Py_ssize_t +array_getwritebuf(PyArrayObject *self, Py_ssize_t segment, void **ptrptr) { if (PyArray_CHKFLAGS(self, WRITEABLE)) return array_getreadbuf(self, segment, (void **) ptrptr); @@ -2871,8 +2871,8 @@ array_getwritebuf(PyArrayObject *self, _int_or_ssize_t segment, void **ptrptr) } } -static _int_or_ssize_t -array_getcharbuf(PyArrayObject *self, _int_or_ssize_t segment, const char **ptrptr) +static Py_ssize_t +array_getcharbuf(PyArrayObject *self, Py_ssize_t segment, const char **ptrptr) { if (self->descr->type_num == PyArray_STRING || \ self->descr->type_num == PyArray_UNICODE) @@ -3670,11 +3670,11 @@ static PyNumberMethods array_as_number = { static PyObject * -array_slice(PyArrayObject *self, _int_or_ssize_t ilow, - _int_or_ssize_t ihigh) +array_slice(PyArrayObject *self, Py_ssize_t ilow, + Py_ssize_t ihigh) { PyArrayObject *r; - _int_or_ssize_t l; + Py_ssize_t l; char *data; if (self->nd == 0) { @@ -3715,8 +3715,8 @@ array_slice(PyArrayObject *self, _int_or_ssize_t ilow, static int -array_ass_slice(PyArrayObject *self, _int_or_ssize_t ilow, - _int_or_ssize_t ihigh, PyObject *v) { +array_ass_slice(PyArrayObject *self, Py_ssize_t ilow, + Py_ssize_t ihigh, PyObject *v) { int ret; PyArrayObject *tmp; @@ -5546,6 +5546,13 @@ array_dataptr_get(PyArrayObject *self) } static PyObject * +array_ctypes_get(PyArrayObject *self) +{ + return PyObject_CallMethod(_numpy_internal, "_ctypes", + "O", self); +} + +static PyObject * array_interface_get(PyArrayObject *self) { PyObject *dict; @@ -6102,6 +6109,10 @@ static PyGetSetDef array_getsetlist[] = { (getter)array_flat_get, (setter)array_flat_set, "a 1-d view of a contiguous array"}, + {"ctypes", + (getter)array_ctypes_get, + NULL, + "Ctypes interface object"}, {"__array_interface__", (getter)array_interface_get, NULL, @@ -7997,7 +8008,7 @@ arrayiter_dealloc(PyArrayIterObject *it) _pya_free(it); } -static _int_or_ssize_t +static Py_ssize_t iter_length(PyArrayIterObject *self) { return self->size; @@ -10290,9 +10301,11 @@ arraydescr_richcompare(PyArray_Descr *self, PyObject *other, int cmp_op) **************** Implement Mapping Protocol *************************** *************************************************************************/ -static _int_or_ssize_t -descr_length(PyArray_Descr *self) +static Py_ssize_t +descr_length(PyObject *self0) { + + PyArray_Descr *self = (PyArray_Descr *)self0; if (self->fields && self->fields != Py_None) /* Remove the last entry (root) */ @@ -10335,13 +10348,9 @@ descr_subscript(PyArray_Descr *self, PyObject *op) } static PyMappingMethods descr_as_mapping = { -#if PY_VERSION_HEX >= 0x02050000 - (lenfunc)descr_length, /*mp_length*/ -#else - (inquiry)descr_length, /*mp_length*/ -#endif + descr_length, /*mp_length*/ (binaryfunc)descr_subscript, /*mp_subscript*/ - (objobjargproc)NULL, /*mp_ass_subscript*/ + (objobjargproc)NULL, /*mp_ass_subscript*/ }; /****************** End of Mapping Protocol ******************************/ diff --git a/numpy/core/src/multiarraymodule.c b/numpy/core/src/multiarraymodule.c index 6bf6bfa4c..d4c1513be 100644 --- a/numpy/core/src/multiarraymodule.c +++ b/numpy/core/src/multiarraymodule.c @@ -15,6 +15,7 @@ /* $Id: multiarraymodule.c,v 1.36 2005/09/14 00:14:00 teoliphant Exp $ */ +#define PY_SSIZE_T_CLEAN #include "Python.h" #include "structmember.h" /*#include <string.h> @@ -5031,7 +5032,7 @@ array_fromString(PyObject *ignored, PyObject *args, PyObject *keywds) char *data; longlong nin=-1; char *sep=NULL; - int s; + Py_ssize_t s; static char *kwlist[] = {"string", "dtype", "count", "sep", NULL}; PyArray_Descr *descr=NULL; @@ -5807,6 +5808,37 @@ array__get_ndarray_c_version(PyObject *dummy, PyObject *args, PyObject *kwds) return PyInt_FromLong( (long) PyArray_GetNDArrayCVersion() ); } +static char +doc__reconstruct[] = "_reconstruct(subtype, shape, dtype) constructs an empty array. Used by Pickles."; + +static PyObject * +array__reconstruct(PyObject *dummy, PyObject *args) +{ + + PyTypeObject *subtype; + PyArray_Dims shape = {NULL, 0}; + PyArray_Descr *dtype=NULL; + if (!PyArg_ParseTuple(args, "O!O&O&", &PyType_Type, &subtype, + PyArray_IntpConverter, &shape, + PyArray_DescrConverter, &dtype)) + goto fail; + + if (!PyType_IsSubtype(subtype, &PyArray_Type)) { + PyErr_SetString(PyExc_TypeError, + "_reconstruct: First argument must be " \ + "a sub-type of ndarray"); + goto fail; + } + + return PyArray_NewFromDescr(subtype, dtype, + (int)shape.len, shape.ptr, + NULL, NULL, 0, NULL); + fail: + Py_XDECREF(dtype); + if (shape.ptr) PyDimMem_FREE(shape.ptr); + return NULL; +} + static char doc_set_string_function[] = "set_string_function(f, repr=1) sets the python function f to be the function used to obtain a pretty printable string version of a array whenever a array is printed. f(M) should expect a array argument M, and should return a string consisting of the desired representation of M for printing."; @@ -6047,6 +6079,8 @@ format_longfloat(PyObject *dummy, PyObject *args, PyObject *kwds) static struct PyMethodDef array_module_methods[] = { {"_get_ndarray_c_version", (PyCFunction)array__get_ndarray_c_version, METH_VARARGS|METH_KEYWORDS, doc__get_ndarray_c_version}, + {"_reconstruct", (PyCFunction)array__reconstruct, + METH_VARARGS, doc__reconstruct}, {"set_string_function", (PyCFunction)array_set_string_function, METH_VARARGS|METH_KEYWORDS, doc_set_string_function}, {"set_numeric_ops", (PyCFunction)array_set_ops_function, diff --git a/numpy/core/src/scalartypes.inc.src b/numpy/core/src/scalartypes.inc.src index ae7c610e3..ed53778aa 100644 --- a/numpy/core/src/scalartypes.inc.src +++ b/numpy/core/src/scalartypes.inc.src @@ -1400,7 +1400,7 @@ static PyMethodDef voidtype_methods[] = { /************* As_mapping functions for void array scalar ************/ -static _int_or_ssize_t +static Py_ssize_t voidtype_length(PyVoidScalarObject *self) { if (!self->descr->fields || self->descr->fields == Py_None) { @@ -1418,7 +1418,7 @@ voidtype_length(PyVoidScalarObject *self) } static PyObject * -voidtype_item(PyVoidScalarObject *self, _int_or_ssize_t n) +voidtype_item(PyVoidScalarObject *self, Py_ssize_t n) { intp m; PyObject *flist=NULL, *key, *fieldinfo; @@ -1467,7 +1467,7 @@ voidtype_subscript(PyVoidScalarObject *self, PyObject *ind) n = PyArray_PyIntAsIntp(ind); if (error_converting(n)) goto fail; - return voidtype_item(self, (_int_or_ssize_t)n); + return voidtype_item(self, (Py_ssize_t)n); fail: PyErr_SetString(PyExc_IndexError, "invalid index"); @@ -1476,7 +1476,7 @@ voidtype_subscript(PyVoidScalarObject *self, PyObject *ind) } static int -voidtype_ass_item(PyVoidScalarObject *self, _int_or_ssize_t n, PyObject *val) +voidtype_ass_item(PyVoidScalarObject *self, Py_ssize_t n, PyObject *val) { intp m; PyObject *flist=NULL, *key, *fieldinfo, *newtup; @@ -1542,7 +1542,7 @@ voidtype_ass_subscript(PyVoidScalarObject *self, PyObject *ind, PyObject *val) /* try to convert it to a number */ n = PyArray_PyIntAsIntp(ind); if (error_converting(n)) goto fail; - return voidtype_ass_item(self, (_int_or_ssize_t)n, val); + return voidtype_ass_item(self, (Py_ssize_t)n, val); fail: PyErr_SetString(PyExc_IndexError, msg); @@ -2001,14 +2001,14 @@ object_arrtype_concat(PyObjectScalarObject *self, PyObject *other) return PySequence_Concat(self->obval, other); } -static _int_or_ssize_t +static Py_ssize_t object_arrtype_length(PyObjectScalarObject *self) { return PyObject_Length(self->obval); } static PyObject * -object_arrtype_repeat(PyObjectScalarObject *self, _int_or_ssize_t count) +object_arrtype_repeat(PyObjectScalarObject *self, Py_ssize_t count) { return PySequence_Repeat(self->obval, count); } @@ -2039,7 +2039,7 @@ object_arrtype_inplace_concat(PyObjectScalarObject *self, PyObject *o) } static PyObject * -object_arrtype_inplace_repeat(PyObjectScalarObject *self, _int_or_ssize_t count) +object_arrtype_inplace_repeat(PyObjectScalarObject *self, Py_ssize_t count) { return PySequence_InPlaceRepeat(self->obval, count); } @@ -2082,8 +2082,8 @@ static PyMappingMethods object_arrtype_as_mapping = { #endif }; -static _int_or_ssize_t -object_arrtype_getsegcount(PyObjectScalarObject *self, _int_or_ssize_t *lenp) +static Py_ssize_t +object_arrtype_getsegcount(PyObjectScalarObject *self, Py_ssize_t *lenp) { int newlen; int cnt; @@ -2100,8 +2100,8 @@ object_arrtype_getsegcount(PyObjectScalarObject *self, _int_or_ssize_t *lenp) return cnt; } -static _int_or_ssize_t -object_arrtype_getreadbuf(PyObjectScalarObject *self, _int_or_ssize_t segment, void **ptrptr) +static Py_ssize_t +object_arrtype_getreadbuf(PyObjectScalarObject *self, Py_ssize_t segment, void **ptrptr) { PyBufferProcs *pb = self->obval->ob_type->tp_as_buffer; @@ -2116,8 +2116,8 @@ object_arrtype_getreadbuf(PyObjectScalarObject *self, _int_or_ssize_t segment, v return (*pb->bf_getreadbuffer)(self->obval, segment, ptrptr); } -static _int_or_ssize_t -object_arrtype_getwritebuf(PyObjectScalarObject *self, _int_or_ssize_t segment, void **ptrptr) +static Py_ssize_t +object_arrtype_getwritebuf(PyObjectScalarObject *self, Py_ssize_t segment, void **ptrptr) { PyBufferProcs *pb = self->obval->ob_type->tp_as_buffer; @@ -2132,8 +2132,8 @@ object_arrtype_getwritebuf(PyObjectScalarObject *self, _int_or_ssize_t segment, return (*pb->bf_getwritebuffer)(self->obval, segment, ptrptr); } -static _int_or_ssize_t -object_arrtype_getcharbuf(PyObjectScalarObject *self, _int_or_ssize_t segment, +static Py_ssize_t +object_arrtype_getcharbuf(PyObjectScalarObject *self, Py_ssize_t segment, const char **ptrptr) { PyBufferProcs *pb = self->obval->ob_type->tp_as_buffer; |