[Numpy-svn] r2721 - in trunk: benchmarks numpy/core numpy/core/include/numpy numpy/core/src
numpy-svn at scipy.org
numpy-svn at scipy.org
Sat Jul 1 23:00:04 EDT 2006
Author: oliphant
Date: 2006-07-01 21:59:54 -0500 (Sat, 01 Jul 2006)
New Revision: 2721
Modified:
trunk/benchmarks/casting.py
trunk/benchmarks/creating.py
trunk/numpy/core/_internal.py
trunk/numpy/core/include/numpy/arrayobject.h
trunk/numpy/core/numerictypes.py
trunk/numpy/core/src/arraymethods.c
trunk/numpy/core/src/arrayobject.c
trunk/numpy/core/src/multiarraymodule.c
trunk/numpy/core/src/scalartypes.inc.src
Log:
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.
Modified: trunk/benchmarks/casting.py
===================================================================
--- trunk/benchmarks/casting.py 2006-07-01 20:54:24 UTC (rev 2720)
+++ trunk/benchmarks/casting.py 2006-07-02 02:59:54 UTC (rev 2721)
@@ -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)
Modified: trunk/benchmarks/creating.py
===================================================================
--- trunk/benchmarks/creating.py 2006-07-01 20:54:24 UTC (rev 2720)
+++ trunk/benchmarks/creating.py 2006-07-02 02:59:54 UTC (rev 2721)
@@ -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)
Modified: trunk/numpy/core/_internal.py
===================================================================
--- trunk/numpy/core/_internal.py 2006-07-01 20:54:24 UTC (rev 2720)
+++ trunk/numpy/core/_internal.py 2006-07-02 02:59:54 UTC (rev 2721)
@@ -102,7 +102,8 @@
# 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 @@
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")
Modified: trunk/numpy/core/include/numpy/arrayobject.h
===================================================================
--- trunk/numpy/core/include/numpy/arrayobject.h 2006-07-01 20:54:24 UTC (rev 2720)
+++ trunk/numpy/core/include/numpy/arrayobject.h 2006-07-02 02:59:54 UTC (rev 2721)
@@ -730,10 +730,10 @@
#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
Modified: trunk/numpy/core/numerictypes.py
===================================================================
--- trunk/numpy/core/numerictypes.py 2006-07-01 20:54:24 UTC (rev 2720)
+++ trunk/numpy/core/numerictypes.py 2006-07-02 02:59:54 UTC (rev 2721)
@@ -76,8 +76,9 @@
"""
# 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
Modified: trunk/numpy/core/src/arraymethods.c
===================================================================
--- trunk/numpy/core/src/arraymethods.c 2006-07-01 20:54:24 UTC (rev 2720)
+++ trunk/numpy/core/src/arraymethods.c 2006-07-02 02:59:54 UTC (rev 2721)
@@ -877,7 +877,7 @@
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);
Modified: trunk/numpy/core/src/arrayobject.c
===================================================================
--- trunk/numpy/core/src/arrayobject.c 2006-07-01 20:54:24 UTC (rev 2720)
+++ trunk/numpy/core/src/arrayobject.c 2006-07-02 02:59:54 UTC (rev 2721)
@@ -1771,7 +1771,7 @@
**************** Implement Mapping Protocol ***************************
*************************************************************************/
-static _int_or_ssize_t
+static Py_ssize_t
array_length(PyArrayObject *self)
{
if (self->nd != 0) {
@@ -1812,7 +1812,7 @@
/* 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 @@
#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 @@
/* 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 @@
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 @@
}
-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 @@
}
}
-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 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 @@
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 @@
}
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 @@
(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 @@
_pya_free(it);
}
-static _int_or_ssize_t
+static Py_ssize_t
iter_length(PyArrayIterObject *self)
{
return self->size;
@@ -10290,9 +10301,11 @@
**************** 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 @@
}
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 ******************************/
Modified: trunk/numpy/core/src/multiarraymodule.c
===================================================================
--- trunk/numpy/core/src/multiarraymodule.c 2006-07-01 20:54:24 UTC (rev 2720)
+++ trunk/numpy/core/src/multiarraymodule.c 2006-07-02 02:59:54 UTC (rev 2721)
@@ -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 @@
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 @@
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 @@
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,
Modified: trunk/numpy/core/src/scalartypes.inc.src
===================================================================
--- trunk/numpy/core/src/scalartypes.inc.src 2006-07-01 20:54:24 UTC (rev 2720)
+++ trunk/numpy/core/src/scalartypes.inc.src 2006-07-02 02:59:54 UTC (rev 2721)
@@ -1400,7 +1400,7 @@
/************* 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 @@
}
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 @@
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 @@
}
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 @@
/* 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 @@
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 @@
}
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 @@
#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 @@
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 @@
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 @@
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;
More information about the Numpy-svn
mailing list