[pypy-commit] pypy pypy-pyarray: - cpyext/ndarrayobject.py: Rename PyArray_* routines to _PyArray_* to
Author: Stefan H. Muller Branch: pypy-pyarray Changeset: r65897:bf28f2841084 Date: 2013-07-28 21:49 +0200 http://bitbucket.org/pypy/pypy/changeset/bf28f2841084/ Log:- cpyext/ndarrayobject.py: Rename PyArray_* routines to _PyArray_* to avoid name clashes with other implementations of numpy in the future. diff --git a/pypy/module/cpyext/include/numpy/arrayobject.h b/pypy/module/cpyext/include/numpy/arrayobject.h --- a/pypy/module/cpyext/include/numpy/arrayobject.h +++ b/pypy/module/cpyext/include/numpy/arrayobject.h @@ -17,6 +17,23 @@ #define import_array() #endif +#ifndef PyArray_NDIM + +#define PyArray_NDIM _PyArray_NDIM +#define PyArray_DIM _PyArray_DIM +#define PyArray_SIZE _PyArray_SIZE +#define PyArray_ITEMSIZE _PyArray_ITEMSIZE +#define PyArray_NBYTES _PyArray_NBYTES +#define PyArray_TYPE _PyArray_TYPE +#define PyArray_DATA _PyArray_DATA +#define PyArray_FromAny _PyArray_FromAny + +#define PyArray_SimpleNew _PyArray_SimpleNew +#define PyArray_SimpleNewFromData _PyArray_SimpleNewFromData +#define PyArray_SimpleNewFromDataOwning _PyArray_SimpleNewFromDataOwning + +#endif + /* copied from numpy/ndarraytypes.h * keep numbers in sync with micronumpy.interp_dtype.DTypeCache */ diff --git a/pypy/module/cpyext/ndarrayobject.py b/pypy/module/cpyext/ndarrayobject.py --- a/pypy/module/cpyext/ndarrayobject.py +++ b/pypy/module/cpyext/ndarrayobject.py @@ -13,38 +13,38 @@ # the asserts are needed, otherwise the translation fails @cpython_api([PyObject], Py_ssize_t, error=CANNOT_FAIL) -def PyArray_NDIM(space, w_array): +def _PyArray_NDIM(space, w_array): assert isinstance(w_array, W_NDimArray) return len(w_array.get_shape()) @cpython_api([PyObject, Py_ssize_t], Py_ssize_t, error=CANNOT_FAIL) -def PyArray_DIM(space, w_array, n): +def _PyArray_DIM(space, w_array, n): assert isinstance(w_array, W_NDimArray) return w_array.get_shape()[n] @cpython_api([PyObject], Py_ssize_t, error=CANNOT_FAIL) -def PyArray_SIZE(space, w_array): +def _PyArray_SIZE(space, w_array): assert isinstance(w_array, W_NDimArray) return w_array.get_size() @cpython_api([PyObject], Py_ssize_t, error=CANNOT_FAIL) -def PyArray_ITEMSIZE(space, w_array): +def _PyArray_ITEMSIZE(space, w_array): assert isinstance(w_array, W_NDimArray) return w_array.get_dtype().get_size() @cpython_api([PyObject], Py_ssize_t, error=CANNOT_FAIL) -def PyArray_NBYTES(space, w_array): +def _PyArray_NBYTES(space, w_array): assert isinstance(w_array, W_NDimArray) return w_array.get_size() * w_array.get_dtype().get_size() @cpython_api([PyObject], Py_ssize_t, error=CANNOT_FAIL) -def PyArray_TYPE(space, w_array): +def _PyArray_TYPE(space, w_array): assert isinstance(w_array, W_NDimArray) return w_array.get_dtype().num @cpython_api([PyObject], rffi.VOIDP, error=CANNOT_FAIL) -def PyArray_DATA(space, w_array): +def _PyArray_DATA(space, w_array): # fails on scalars - see PyArray_FromAny() assert isinstance(w_array, W_NDimArray) return rffi.cast(rffi.VOIDP, w_array.implementation.storage) @@ -52,7 +52,7 @@ @cpython_api([PyObject, rffi.VOIDP, Py_ssize_t, Py_ssize_t, Py_ssize_t, rffi.VOIDP], PyObject) -def PyArray_FromAny(space, w_obj, dtype, min_depth, max_depth, requirements, context): +def _PyArray_FromAny(space, w_obj, dtype, min_depth, max_depth, requirements, context): # ignore all additional arguments for now w_array = convert_to_array(space, w_obj) if w_array.is_scalar(): @@ -69,7 +69,7 @@ @cpython_api([Py_ssize_t, rffi.LONGP, Py_ssize_t], PyObject) -def PyArray_SimpleNew(space, nd, dims, typenum): +def _PyArray_SimpleNew(space, nd, dims, typenum): dtype = get_dtype_cache(space).dtypes_by_num[typenum] shape = [] for i in range(nd): @@ -94,11 +94,11 @@ return W_NDimArray.from_shape_and_storage(space, shape, storage, dtype, owning=owning) @cpython_api([Py_ssize_t, rffi.LONGP, Py_ssize_t, rffi.VOIDP], PyObject) -def PyArray_SimpleNewFromData(space, nd, dims, typenum, data): +def _PyArray_SimpleNewFromData(space, nd, dims, typenum, data): return simple_new_from_data(space, nd, dims, typenum, data, owning=False) @cpython_api([Py_ssize_t, rffi.LONGP, Py_ssize_t, rffi.VOIDP], PyObject) -def PyArray_SimpleNewFromDataOwning(space, nd, dims, typenum, data): +def _PyArray_SimpleNewFromDataOwning(space, nd, dims, typenum, data): # Variant to take over ownership of the memory, equivalent to: # PyObject *arr = PyArray_SimpleNewFromData(nd, dims, typenum, data); # ((PyArrayObject*)arr)->flags |= NPY_OWNDATA; diff --git a/pypy/module/cpyext/test/test_ndarrayobject.py b/pypy/module/cpyext/test/test_ndarrayobject.py --- a/pypy/module/cpyext/test/test_ndarrayobject.py +++ b/pypy/module/cpyext/test/test_ndarrayobject.py @@ -26,31 +26,31 @@ def test_NDIM(self, space, api): a = array(space, [10, 5, 3]) -assert api.PyArray_NDI
[pypy-commit] pypy pypy-pyarray: - cpyext/ndarrayobject.py: Add support for PyArray_STRIDE() and
Author: Stefan H. Muller Branch: pypy-pyarray Changeset: r65898:e5e7c8d419cc Date: 2013-07-30 11:59 +0200 http://bitbucket.org/pypy/pypy/changeset/e5e7c8d419cc/ Log:- cpyext/ndarrayobject.py: Add support for PyArray_STRIDE() and PyArray_FromObject(). - cpyext/include/numpy: Add constants needed by matplotlib. - cpyext/include/complexobject.h: Replace macro with function for const correctness. - lib_pypy/numpy.py: Add __version__. I felt like it's 1.6.2. diff --git a/lib_pypy/numpy.py b/lib_pypy/numpy.py --- a/lib_pypy/numpy.py +++ b/lib_pypy/numpy.py @@ -8,6 +8,8 @@ import os +__version__ = '1.6.2' + def get_include(): head, tail = os.path.split(os.path.dirname(os.path.abspath(__file__))) return os.path.join(head, 'include') diff --git a/pypy/module/cpyext/include/complexobject.h b/pypy/module/cpyext/include/complexobject.h --- a/pypy/module/cpyext/include/complexobject.h +++ b/pypy/module/cpyext/include/complexobject.h @@ -16,6 +16,7 @@ /* generated function */ PyAPI_FUNC(void) _PyComplex_AsCComplex(PyObject *, Py_complex *); +PyAPI_FUNC(PyObject *) _PyComplex_FromCComplex(Py_complex *); Py_LOCAL_INLINE(Py_complex) PyComplex_AsCComplex(PyObject *obj) { @@ -24,7 +25,12 @@ return result; } -#define PyComplex_FromCComplex(c) _PyComplex_FromCComplex(&c) +// shmuller 2013/07/30: Make a function, since macro will fail in C++ due to +// const correctness if called with "const Py_complex" +//#define PyComplex_FromCComplex(c) _PyComplex_FromCComplex(&c) +PyObject *PyComplex_FromCComplex(Py_complex c) { +return _PyComplex_FromCComplex(&c); +} #ifdef __cplusplus } diff --git a/pypy/module/cpyext/include/numpy/arrayobject.h b/pypy/module/cpyext/include/numpy/arrayobject.h --- a/pypy/module/cpyext/include/numpy/arrayobject.h +++ b/pypy/module/cpyext/include/numpy/arrayobject.h @@ -7,6 +7,10 @@ extern "C" { #endif +#include "old_defines.h" + +#define NPY_INLINE + /* fake PyArrayObject so that code that doesn't do direct field access works */ #define PyArrayObject PyObject @@ -19,14 +23,20 @@ #ifndef PyArray_NDIM +#define PyArray_ISCONTIGUOUS(arr) (1) + #define PyArray_NDIM _PyArray_NDIM #define PyArray_DIM _PyArray_DIM +#define PyArray_STRIDE _PyArray_STRIDE #define PyArray_SIZE _PyArray_SIZE #define PyArray_ITEMSIZE _PyArray_ITEMSIZE #define PyArray_NBYTES _PyArray_NBYTES #define PyArray_TYPE _PyArray_TYPE #define PyArray_DATA _PyArray_DATA -#define PyArray_FromAny _PyArray_FromAny + +#define PyArray_FromAny _PyArray_FromAny +#define PyArray_FromObject _PyArray_FromObject +#define PyArray_ContiguousFromObject PyArray_FromObject #define PyArray_SimpleNew _PyArray_SimpleNew #define PyArray_SimpleNewFromData _PyArray_SimpleNewFromData @@ -63,6 +73,19 @@ NPY_NTYPES_ABI_COMPATIBLE=21 }; +#define NPY_INT8 NPY_BYTE +#define NPY_UINT8 NPY_UBYTE +#define NPY_INT16 NPY_SHORT +#define NPY_UINT16NPY_USHORT +#define NPY_INT32 NPY_INT +#define NPY_UINT32NPY_UINT +#define NPY_INT64 NPY_LONG +#define NPY_UINT64NPY_ULONG +#define NPY_FLOAT32 NPY_FLOAT +#define NPY_FLOAT64 NPY_DOUBLE +#define NPY_COMPLEX32 NPY_CFLOAT +#define NPY_COMPLEX64 NPY_CDOUBLE + #ifdef __cplusplus } #endif diff --git a/pypy/module/cpyext/include/numpy/npy_3kcompat.h b/pypy/module/cpyext/include/numpy/npy_3kcompat.h new file mode 100644 diff --git a/pypy/module/cpyext/include/numpy/old_defines.h b/pypy/module/cpyext/include/numpy/old_defines.h new file mode 100644 --- /dev/null +++ b/pypy/module/cpyext/include/numpy/old_defines.h @@ -0,0 +1,187 @@ +/* This header is deprecated as of NumPy 1.7 */ +#ifndef OLD_DEFINES_H +#define OLD_DEFINES_H + +#if defined(NPY_NO_DEPRECATED_API) && NPY_NO_DEPRECATED_API >= NPY_1_7_API_VERSION +#error The header "old_defines.h" is deprecated as of NumPy 1.7. +#endif + +#define NDARRAY_VERSION NPY_VERSION + +#define PyArray_MIN_BUFSIZE NPY_MIN_BUFSIZE +#define PyArray_MAX_BUFSIZE NPY_MAX_BUFSIZE +#define PyArray_BUFSIZE NPY_BUFSIZE + +#define PyArray_PRIORITY NPY_PRIORITY +#define PyArray_SUBTYPE_PRIORITY NPY_PRIORITY +#define PyArray_NUM_FLOATTYPE NPY_NUM_FLOATTYPE + +#define NPY_MAX PyArray_MAX +#define NPY_MIN PyArray_MIN + +#define PyArray_TYPES NPY_TYPES +#define PyArray_BOOLNPY_BOOL +#define PyArray_BYTENPY_BYTE +#define PyArray_UBYTE NPY_UBYTE +#define PyArray_SHORT NPY_SHORT +#define PyArray_USHORT NPY_USHORT +#define PyArray_INT NPY_INT +#define PyArray_UINTNPY_UINT +#define PyArray_LONGNPY_LONG +#define PyArray_ULONG NPY_ULONG +#define PyArray_LONGLONGNPY_LONGLONG +#define PyArray_ULONGLONG NPY_ULONGLONG +#define PyArray_HALFNPY_HALF +#define PyArray_FLOAT NPY_FLOAT +#define PyArray_DOUBLE NPY_DOUBLE
[pypy-commit] pypy pypy-pyarray: - Add cpyext implementation of Numpy PyArray_* C-API
e().value == 10. + +def test_SimpleNewFromData_scalar(self, space, api): +a = array(space, [1]) +num = api.PyArray_TYPE(a) +ptr_a = api.PyArray_DATA(a) + +x = rffi.cast(rffi.DOUBLEP, ptr_a) +x[0] = float(10.) + +ptr_s = lltype.nullptr(rffi.LONGP.TO) + +res = api.PyArray_SimpleNewFromData(0, ptr_s, num, ptr_a) +assert res.is_scalar() +assert res.get_scalar_value().value == 10. + +def test_SimpleNew(self, space, api): +shape = [10, 5, 3] +nd = len(shape) + +s = iarray(space, [nd]) +ptr_s = rffi.cast(rffi.LONGP, api.PyArray_DATA(s)) +ptr_s[0] = 10 +ptr_s[1] = 5 +ptr_s[2] = 3 + +a = api.PyArray_SimpleNew(nd, ptr_s, 12) + +#assert list(api.PyArray_DIMS(a))[:3] == shape + +ptr_a = api.PyArray_DATA(a) + +x = rffi.cast(rffi.DOUBLEP, ptr_a) +for i in range(150): +x[i] = float(i) + +for i in range(150): +assert x[i] == float(i) + +def test_SimpleNewFromData(self, space, api): +shape = [10, 5, 3] +nd = len(shape) + +s = iarray(space, [nd]) +ptr_s = rffi.cast(rffi.LONGP, api.PyArray_DATA(s)) +ptr_s[0] = 10 +ptr_s[1] = 5 +ptr_s[2] = 3 + +a = array(space, shape) +num = api.PyArray_TYPE(a) +ptr_a = api.PyArray_DATA(a) + +x = rffi.cast(rffi.DOUBLEP, ptr_a) +for i in range(150): +x[i] = float(i) + +res = api.PyArray_SimpleNewFromData(nd, ptr_s, num, ptr_a) +assert api.PyArray_TYPE(res) == num +assert api.PyArray_DATA(res) == ptr_a +for i in range(nd): +assert api.PyArray_DIM(res, i) == shape[i] +ptr_r = rffi.cast(rffi.DOUBLEP, api.PyArray_DATA(res)) +for i in range(150): +assert ptr_r[i] == float(i) + +def test_SimpleNewFromData_complex(self, space, api): +a = array(space, [2]) +ptr_a = api.PyArray_DATA(a) + +x = rffi.cast(rffi.DOUBLEP, ptr_a) +x[0] = 3. +x[1] = 4. + +ptr_s = lltype.nullptr(rffi.LONGP.TO) + +res = api.PyArray_SimpleNewFromData(0, ptr_s, 15, ptr_a) +assert res.get_scalar_value().real == 3. +assert res.get_scalar_value().imag == 4. + diff --git a/pypy/module/micronumpy/interp_dtype.py b/pypy/module/micronumpy/interp_dtype.py --- a/pypy/module/micronumpy/interp_dtype.py +++ b/pypy/module/micronumpy/interp_dtype.py @@ -804,10 +804,12 @@ self.dtypes_by_name[alias] = dtype self.dtypes_by_name[dtype.char] = dtype -self.dtypes_by_num = [dtype for dtype in -sorted(self.dtypes_by_num.values(), key=lambda dtype: dtype.num) -if dtype.num <= self.w_float64dtype.num] -assert len(self.dtypes_by_num) == self.w_float64dtype.num + 1 +# shmuller 2013/07/22: Cannot find complex data types after conversion to +# list! Solution: Keep in dictionary form. +#self.dtypes_by_num = [dtype for dtype in +#sorted(self.dtypes_by_num.values(), key=lambda dtype: dtype.num) +#if dtype.num <= self.w_float64dtype.num] +#assert len(self.dtypes_by_num) == self.w_float64dtype.num + 1 typeinfo_full = { 'LONGLONG': self.w_int64dtype, ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy pypy-pyarray: - cpyext/include: Add a few definitions needed by PyCXX to headers (for
Author: Stefan H. Muller Branch: pypy-pyarray Changeset: r66337:aabf1cc286a7 Date: 2013-07-31 00:42 +0200 http://bitbucket.org/pypy/pypy/changeset/aabf1cc286a7/ Log:- cpyext/include: Add a few definitions needed by PyCXX to headers (for matplotlib build). - NEW: cpyext/include/missing.h: Temporary place to put definitions from missing header files. diff --git a/pypy/module/cpyext/include/Python.h b/pypy/module/cpyext/include/Python.h --- a/pypy/module/cpyext/include/Python.h +++ b/pypy/module/cpyext/include/Python.h @@ -126,6 +126,9 @@ #include "pysignals.h" #include "pythread.h" +/* Missing definitions */ +#include "missing.h" + // XXX This shouldn't be included here #include "structmember.h" diff --git a/pypy/module/cpyext/include/funcobject.h b/pypy/module/cpyext/include/funcobject.h --- a/pypy/module/cpyext/include/funcobject.h +++ b/pypy/module/cpyext/include/funcobject.h @@ -12,6 +12,8 @@ PyObject *func_name; /* The __name__ attribute, a string object */ } PyFunctionObject; +PyAPI_DATA(PyTypeObject) PyFunction_Type; + #define PyFunction_GET_CODE(obj) PyFunction_GetCode((PyObject*)(obj)) #define PyMethod_GET_FUNCTION(obj) PyMethod_Function((PyObject*)(obj)) diff --git a/pypy/module/cpyext/include/missing.h b/pypy/module/cpyext/include/missing.h new file mode 100644 --- /dev/null +++ b/pypy/module/cpyext/include/missing.h @@ -0,0 +1,15 @@ + +/* Definitions from missing header files */ + +#ifndef Py_MISSING_H +#define Py_MISSING_H +#ifdef __cplusplus +extern "C" { +#endif + +PyAPI_DATA(PyTypeObject) PyMethod_Type, PyRange_Type, PyTraceBack_Type; + +#ifdef __cplusplus +} +#endif +#endif /* !Py_MISSING_H */ diff --git a/pypy/module/cpyext/include/modsupport.h b/pypy/module/cpyext/include/modsupport.h --- a/pypy/module/cpyext/include/modsupport.h +++ b/pypy/module/cpyext/include/modsupport.h @@ -56,6 +56,7 @@ #define PyMODINIT_FUNC void #endif +PyAPI_DATA(char *) _Py_PackageContext; #ifdef __cplusplus } diff --git a/pypy/module/cpyext/include/pythonrun.h b/pypy/module/cpyext/include/pythonrun.h --- a/pypy/module/cpyext/include/pythonrun.h +++ b/pypy/module/cpyext/include/pythonrun.h @@ -6,14 +6,41 @@ extern "C" { #endif - void Py_FatalError(const char *msg); +void Py_FatalError(const char *msg); /* the -3 option will probably not be implemented */ +/* #define Py_Py3kWarningFlag 0 #define Py_FrozenFlag 0 #define Py_VerboseFlag 0 #define Py_DebugFlag 1 +*/ + +/* taken from Python-2.7.3/Include/pydebug.h */ +PyAPI_DATA(int) Py_DebugFlag; +PyAPI_DATA(int) Py_VerboseFlag; +PyAPI_DATA(int) Py_InteractiveFlag; +PyAPI_DATA(int) Py_InspectFlag; +PyAPI_DATA(int) Py_OptimizeFlag; +PyAPI_DATA(int) Py_NoSiteFlag; +PyAPI_DATA(int) Py_BytesWarningFlag; +PyAPI_DATA(int) Py_UseClassExceptionsFlag; +PyAPI_DATA(int) Py_FrozenFlag; +PyAPI_DATA(int) Py_TabcheckFlag; +PyAPI_DATA(int) Py_UnicodeFlag; +PyAPI_DATA(int) Py_IgnoreEnvironmentFlag; +PyAPI_DATA(int) Py_DivisionWarningFlag; +PyAPI_DATA(int) Py_DontWriteBytecodeFlag; +PyAPI_DATA(int) Py_NoUserSiteDirectory; +/* _XXX Py_QnewFlag should go away in 3.0. It's true iff -Qnew is passed, + * on the command line, and is used in 2.2 by ceval.c to make all "/" divisions + * true divisions (which they will be in 3.0). */ +PyAPI_DATA(int) _Py_QnewFlag; +/* Warn about 3.x issues */ +PyAPI_DATA(int) Py_Py3kWarningFlag; +PyAPI_DATA(int) Py_HashRandomizationFlag; + typedef struct { int cf_flags; /* bitmask of CO_xxx flags relevant to future */ ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy pypy-pyarray: Declare PyArray_Type as extern.
Author: Stefan H. Muller Branch: pypy-pyarray Changeset: r66345:8a17098648b8 Date: 2013-08-08 18:16 +0200 http://bitbucket.org/pypy/pypy/changeset/8a17098648b8/ Log:Declare PyArray_Type as extern. diff --git a/pypy/module/cpyext/include/numpy/arrayobject.h b/pypy/module/cpyext/include/numpy/arrayobject.h --- a/pypy/module/cpyext/include/numpy/arrayobject.h +++ b/pypy/module/cpyext/include/numpy/arrayobject.h @@ -18,7 +18,7 @@ #define PyArrayObject PyObject #define PyArray_Descr PyObject -//PyTypeObject PyArray_Type; +extern PyTypeObject PyArray_Type; typedef unsigned char npy_bool; typedef unsigned char npy_uint8; ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy pypy-pyarray: Implement PyNumber_Float() as space.float(w_obj).
Author: Stefan H. Muller Branch: pypy-pyarray Changeset: r66346:56fa5d73e5ec Date: 2013-08-10 00:33 +0200 http://bitbucket.org/pypy/pypy/changeset/56fa5d73e5ec/ Log:Implement PyNumber_Float() as space.float(w_obj). diff --git a/pypy/module/cpyext/floatobject.py b/pypy/module/cpyext/floatobject.py --- a/pypy/module/cpyext/floatobject.py +++ b/pypy/module/cpyext/floatobject.py @@ -25,7 +25,7 @@ """ Returns the o converted to a float object on success, or NULL on failure. This is the equivalent of the Python expression float(o).""" -return space.call_function(space.w_float, w_obj) +return space.float(w_obj) @cpython_api([PyObject, rffi.CCHARPP], PyObject) def PyFloat_FromString(space, w_obj, _): ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy pypy-pyarray: - cpyext/number.py, cpyext/test/test_number.py: Implement
Author: Stefan H. Muller Branch: pypy-pyarray Changeset: r66336:eba88c40cdfa Date: 2013-07-30 20:24 +0200 http://bitbucket.org/pypy/pypy/changeset/eba88c40cdfa/ Log:- cpyext/number.py, cpyext/test/test_number.py: Implement PyNumber_CoerceEx() and PyNumber_Coerce(). diff --git a/pypy/module/cpyext/number.py b/pypy/module/cpyext/number.py --- a/pypy/module/cpyext/number.py +++ b/pypy/module/cpyext/number.py @@ -1,6 +1,6 @@ from pypy.interpreter.error import OperationError from pypy.module.cpyext.api import cpython_api, CANNOT_FAIL, Py_ssize_t -from pypy.module.cpyext.pyobject import PyObject +from pypy.module.cpyext.pyobject import PyObject, PyObjectP, from_ref, make_ref, Py_DecRef from rpython.rtyper.lltypesystem import rffi, lltype from rpython.tool.sourcetools import func_with_new_name @@ -56,6 +56,38 @@ """ return space.index(w_obj) +@cpython_api([PyObjectP, PyObjectP], rffi.INT_real, error=-1) +def PyNumber_CoerceEx(space, pp1, pp2): +""" +""" +w_obj1 = from_ref(space, pp1[0]) +w_obj2 = from_ref(space, pp2[0]) +w_res = space.try_coerce(w_obj1, w_obj2) +if w_res is None: +return 1 +else: +Py_DecRef(space, pp1[0]) +Py_DecRef(space, pp2[0]) +pp1[0] = make_ref(space, space.getitem(w_res, space.wrap(0))) +pp2[0] = make_ref(space, space.getitem(w_res, space.wrap(1))) +return 0 + +@cpython_api([PyObjectP, PyObjectP], rffi.INT_real, error=-1) +def PyNumber_Coerce(space, pp1, pp2): +""" +""" +w_obj1 = from_ref(space, pp1[0]) +w_obj2 = from_ref(space, pp2[0]) +w_res = space.coerce(w_obj1, w_obj2) +if w_res is None: +return 1 +else: +Py_DecRef(space, pp1[0]) +Py_DecRef(space, pp2[0]) +pp1[0] = make_ref(space, space.getitem(w_res, space.wrap(0))) +pp2[0] = make_ref(space, space.getitem(w_res, space.wrap(1))) +return 0 + def func_rename(newname): return lambda func: func_with_new_name(func, newname) diff --git a/pypy/module/cpyext/test/test_number.py b/pypy/module/cpyext/test/test_number.py --- a/pypy/module/cpyext/test/test_number.py +++ b/pypy/module/cpyext/test/test_number.py @@ -2,6 +2,7 @@ from pypy.interpreter.error import OperationError from pypy.module.cpyext.test.test_api import BaseApiTest from pypy.module.cpyext import sequence +from pypy.module.cpyext.pyobject import PyObject, PyObjectP, from_ref, make_ref, Py_DecRef class TestIterator(BaseApiTest): def test_check(self, space, api): @@ -35,6 +36,26 @@ assert w_l is None api.PyErr_Clear() +def test_number_coerce_ex(self, space, api): +pl = make_ref(space, space.wrap(123)) +pf = make_ref(space, space.wrap(42.)) +ppl = lltype.malloc(PyObjectP.TO, 1, flavor='raw') +ppf = lltype.malloc(PyObjectP.TO, 1, flavor='raw') +ppl[0] = pl +ppf[0] = pf + +ret = api.PyNumber_CoerceEx(ppl, ppf) +assert ret == 0 + +w_res = from_ref(space, ppl[0]) + +assert api.PyFloat_Check(w_res) +assert space.unwrap(w_res) == 123. +Py_DecRef(space, ppl[0]) +Py_DecRef(space, ppf[0]) +lltype.free(ppl, flavor='raw') +lltype.free(ppf, flavor='raw') + def test_numbermethods(self, space, api): assert "ab" == space.unwrap( api.PyNumber_Add(space.wrap("a"), space.wrap("b"))) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy pypy-pyarray: - cpyext/ndarrayobject.py: Rename PyArray_* routines to _PyArray_* to
Author: Stefan H. Muller Branch: pypy-pyarray Changeset: r66333:ada4abc58e15 Date: 2013-07-28 21:49 +0200 http://bitbucket.org/pypy/pypy/changeset/ada4abc58e15/ Log:- cpyext/ndarrayobject.py: Rename PyArray_* routines to _PyArray_* to avoid name clashes with other implementations of numpy in the future. diff --git a/pypy/module/cpyext/include/numpy/arrayobject.h b/pypy/module/cpyext/include/numpy/arrayobject.h --- a/pypy/module/cpyext/include/numpy/arrayobject.h +++ b/pypy/module/cpyext/include/numpy/arrayobject.h @@ -7,10 +7,6 @@ extern "C" { #endif -#include "old_defines.h" - -#define NPY_INLINE - /* fake PyArrayObject so that code that doesn't do direct field access works */ #define PyArrayObject PyObject @@ -23,20 +19,14 @@ #ifndef PyArray_NDIM -#define PyArray_ISCONTIGUOUS(arr) (1) - #define PyArray_NDIM _PyArray_NDIM #define PyArray_DIM _PyArray_DIM -#define PyArray_STRIDE _PyArray_STRIDE #define PyArray_SIZE _PyArray_SIZE #define PyArray_ITEMSIZE _PyArray_ITEMSIZE #define PyArray_NBYTES _PyArray_NBYTES #define PyArray_TYPE _PyArray_TYPE #define PyArray_DATA _PyArray_DATA - -#define PyArray_FromAny _PyArray_FromAny -#define PyArray_FromObject _PyArray_FromObject -#define PyArray_ContiguousFromObject PyArray_FromObject +#define PyArray_FromAny _PyArray_FromAny #define PyArray_SimpleNew _PyArray_SimpleNew #define PyArray_SimpleNewFromData _PyArray_SimpleNewFromData @@ -73,19 +63,6 @@ NPY_NTYPES_ABI_COMPATIBLE=21 }; -#define NPY_INT8 NPY_BYTE -#define NPY_UINT8 NPY_UBYTE -#define NPY_INT16 NPY_SHORT -#define NPY_UINT16NPY_USHORT -#define NPY_INT32 NPY_INT -#define NPY_UINT32NPY_UINT -#define NPY_INT64 NPY_LONG -#define NPY_UINT64NPY_ULONG -#define NPY_FLOAT32 NPY_FLOAT -#define NPY_FLOAT64 NPY_DOUBLE -#define NPY_COMPLEX32 NPY_CFLOAT -#define NPY_COMPLEX64 NPY_CDOUBLE - #ifdef __cplusplus } #endif ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy pypy-pyarray: Add PyArray_New() and many macros for f2py.
Author: Stefan H. Muller Branch: pypy-pyarray Changeset: r66343:bb2aafab5a92 Date: 2013-08-08 00:41 +0200 http://bitbucket.org/pypy/pypy/changeset/bb2aafab5a92/ Log:Add PyArray_New() and many macros for f2py. The modified version of f2py from the numpy pypy-hack branch generates code that compiles with this, but the generated modules don't work due to bypassing of Py_InitModule(). diff --git a/pypy/module/cpyext/api.py b/pypy/module/cpyext/api.py --- a/pypy/module/cpyext/api.py +++ b/pypy/module/cpyext/api.py @@ -404,7 +404,7 @@ 'PyFunction_Type', 'PyMethod_Type', 'PyRange_Type', 'PyTraceBack_Type', -'PyArray_ZEROS', +'PyArray_Type', '_PyArray_FILLWBYTE', '_PyArray_ZEROS', '_PyArray_CopyInto', 'Py_DebugFlag', 'Py_VerboseFlag', 'Py_InteractiveFlag', 'Py_InspectFlag', 'Py_OptimizeFlag', 'Py_NoSiteFlag', 'Py_BytesWarningFlag', 'Py_UseClassExceptionsFlag', diff --git a/pypy/module/cpyext/include/numpy/arrayobject.h b/pypy/module/cpyext/include/numpy/arrayobject.h --- a/pypy/module/cpyext/include/numpy/arrayobject.h +++ b/pypy/module/cpyext/include/numpy/arrayobject.h @@ -13,13 +13,20 @@ /* fake PyArrayObject so that code that doesn't do direct field access works */ #define PyArrayObject PyObject +#define PyArray_Descr PyObject + +PyTypeObject PyArray_Type; typedef unsigned char npy_bool; typedef unsigned char npy_uint8; +typedef int npy_int; #ifndef npy_intp #define npy_intp long #endif +#ifndef NPY_INTP_FMT +#define NPY_INTP_FMT "ld" +#endif #ifndef import_array #define import_array() #endif @@ -73,21 +80,51 @@ #define NPY_COMPLEX32 NPY_CFLOAT #define NPY_COMPLEX64 NPY_CDOUBLE +#define PyTypeNum_ISBOOL(type) ((type) == NPY_BOOL) +#define PyTypeNum_ISINTEGER(type) (((type) >= NPY_BYTE) && \ +((type) <= NPY_ULONGLONG)) +#define PyTypeNum_ISFLOAT(type) type) >= NPY_FLOAT) && \ +((type) <= NPY_LONGDOUBLE)) || \ +((type) == NPY_HALF)) +#define PyTypeNum_ISCOMPLEX(type) (((type) >= NPY_CFLOAT) && \ +((type) <= NPY_CLONGDOUBLE)) + +#define PyArray_ISBOOL(arr)(PyTypeNum_ISBOOL(PyArray_TYPE(arr))) +#define PyArray_ISINTEGER(arr) (PyTypeNum_ISINTEGER(PyArray_TYPE(arr))) +#define PyArray_ISFLOAT(arr) (PyTypeNum_ISFLOAT(PyArray_TYPE(arr))) +#define PyArray_ISCOMPLEX(arr) (PyTypeNum_ISCOMPLEX(PyArray_TYPE(arr))) + /* selection of flags */ -#define NPY_C_CONTIGUOUS0x0001 +#define NPY_CONTIGUOUS 0x0001 +#define NPY_FORTRAN 0x0002 #define NPY_OWNDATA 0x0004 +#define NPY_FORCECAST 0x0010 #define NPY_ALIGNED 0x0100 -#define NPY_IN_ARRAY (NPY_C_CONTIGUOUS | NPY_ALIGNED) - +#define NPY_NOTSWAPPED 0x0200 +#define NPY_WRITEABLE 0x0400 +#define NPY_C_CONTIGUOUSNPY_CONTIGUOUS +#define NPY_F_CONTIGUOUSNPY_FORTRAN +#define NPY_IN_ARRAY (NPY_C_CONTIGUOUS | NPY_ALIGNED) +#define NPY_BEHAVED(NPY_ALIGNED | NPY_WRITEABLE) +#define NPY_CARRAY (NPY_CONTIGUOUS | NPY_BEHAVED) +#define NPY_FARRAY (NPY_FORTRAN | NPY_BEHAVED) +#define NPY_DEFAULT NPY_CARRAY /* functions */ #ifndef PyArray_NDIM -#define PyArray_ISCONTIGUOUS(arr) (1) - #define PyArray_Check _PyArray_Check #define PyArray_CheckExact _PyArray_CheckExact + +#define PyArray_ISONESEGMENT(arr) (1) +#define PyArray_FLAGS(arr)(0) + +#define PyArray_ISCONTIGUOUS _PyArray_ISCONTIGUOUS + +#define PyArray_ISCARRAY(arr) PyArray_ISCONTIGUOUS(arr) +#define PyArray_ISFARRAY(arr) (!PyArray_ISCONTIGUOUS(arr)) + #define PyArray_NDIM _PyArray_NDIM #define PyArray_DIM_PyArray_DIM #define PyArray_STRIDE _PyArray_STRIDE @@ -106,8 +143,10 @@ #define PyArray_ContiguousFromAny PyArray_FromObject #define PyArray_FROMANY(obj, typenum, min, max, requirements) (obj) -#define PyArray_FROM_OTF(obj, typenum, requirements) (obj) +#define PyArray_FROM_OTF(obj, typenum, requirements) \ +PyArray_FromObject(obj, typenum, 0, 0) +#define PyArray_New _PyArray_New #define PyArray_SimpleNew _PyArray_SimpleNew #define PyArray_SimpleNewFromData _PyArray_SimpleNewFromData #define PyArray_SimpleNewFromDataOwning _PyArray_SimpleNewFromDataOwning @@ -115,7 +154,13 @@ #define PyArray_EMPTY(nd, dims, type_num, fortran) \ PyArray_SimpleNew(nd, dims, type_num) -PyObject* PyArray_ZEROS(int nd, npy_intp* dims, int type_num, int fortran); +void _PyArray_FILLWBYTE(PyObject* obj, int val); +PyObject* _PyArray_ZEROS(int nd, npy_intp* dims, int type_num, int fortran); +int _PyArray_CopyInto(PyArrayObject* dest, PyArrayObject* src); + +#define PyArray_FILLWBYTE _PyArray_FILLWBYTE +#define PyArray_ZEROS _PyArray_ZEROS +#define PyArray_CopyInto _PyArray_CopyInto #define PyArray_Resize(self, newshape, refcheck, fortran) (NULL) diff --git a/pypy/module/cpyext/ndarrayobject.py b/pypy/module/cpyext/ndarrayobject.py --- a/py
[pypy-commit] pypy pypy-pyarray: - Add cpyext implementation of Numpy PyArray_* C-API
Author: Stefan H. Muller Branch: pypy-pyarray Changeset: r66332:01fc9f0596f2 Date: 2013-07-28 15:12 +0200 http://bitbucket.org/pypy/pypy/changeset/01fc9f0596f2/ Log:- Add cpyext implementation of Numpy PyArray_* C-API * pypy/module/cpyext/include/numpy/arrayobject.h * pypy/module/cpyext/ndarrayobject.py * pypy/module/cpyext/test/test_ndarrayobject.py - pypy/module/cpyext/api.py: copy_header_files() now copies the numpy subdirectory as well. - pypy/module/micronumpy/interp_dtype.py: DtypeCache.dtypes_by_num: * Keep in dictionary form, since otherwise not all dtypes can be reached. - lib_pypy/numpy.py, lib_pypy/numpypy/__init__.py: * "import numpy" now displays a warning but falls back to "import numpypy as numpy" *without* raising an ImportError. - pypy/module/cpyext/include/boolobject.h and complexobject.h: * Add #define's for PyIntObject and PyComplexObject. diff --git a/lib_pypy/numpy.py b/lib_pypy/numpy.py --- a/lib_pypy/numpy.py +++ b/lib_pypy/numpy.py @@ -8,8 +8,6 @@ import os -__version__ = '1.7' - def get_include(): head, tail = os.path.split(os.path.dirname(os.path.abspath(__file__))) return os.path.join(head, 'include') ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy pypy-pyarray: ndarray.nonzero(): Deal with scalars, add test.
Author: Stefan H. Muller Branch: pypy-pyarray Changeset: r66349:01ebe4a52002 Date: 2013-08-11 21:09 +0200 http://bitbucket.org/pypy/pypy/changeset/01ebe4a52002/ Log:ndarray.nonzero(): Deal with scalars, add test. - lib_pypy/numpypy/core/fromnumeric.py: Put original numpy implementation back for nonzero(). diff --git a/lib_pypy/numpypy/core/fromnumeric.py b/lib_pypy/numpypy/core/fromnumeric.py --- a/lib_pypy/numpypy/core/fromnumeric.py +++ b/lib_pypy/numpypy/core/fromnumeric.py @@ -1133,7 +1133,13 @@ (array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2])) """ -raise NotImplementedError('Waiting on interp level method') +try: +nonzero = a.nonzero +except AttributeError: +res = _wrapit(a, 'nonzero') +else: +res = nonzero() +return res def shape(a): diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py --- a/pypy/module/micronumpy/interp_numarray.py +++ b/pypy/module/micronumpy/interp_numarray.py @@ -352,15 +352,21 @@ return self.descr_reshape(space, [space.wrap(-1)]) def descr_nonzero(self, space): +s = loop.count_all_true(self) +index_type = interp_dtype.get_dtype_cache(space).w_int64dtype +box = index_type.itemtype.box + +if self.is_scalar(): +w_res = W_NDimArray.from_shape(space, [s], index_type) +if s == 1: +w_res.implementation.setitem(0, box(0)) +return space.newtuple([w_res]) + impl = self.implementation arr_iter = iter.MultiDimViewIterator(impl, impl.dtype, 0, impl.strides, impl.backstrides, impl.shape) -index_type = interp_dtype.get_dtype_cache(space).w_int64dtype -box = index_type.itemtype.box - nd = len(impl.shape) -s = loop.count_all_true(self) w_res = W_NDimArray.from_shape(space, [s, nd], index_type) res_iter = w_res.create_iter() diff --git a/pypy/module/micronumpy/test/test_numarray.py b/pypy/module/micronumpy/test/test_numarray.py --- a/pypy/module/micronumpy/test/test_numarray.py +++ b/pypy/module/micronumpy/test/test_numarray.py @@ -2302,8 +2302,13 @@ def test_nonzero(self): from numpypy import array -a = array([[1, 0, 3], [2, 0, 4]]) -nz = a.nonzero() +nz = array(0).nonzero() +assert nz[0].size == 0 + +nz = array(2).nonzero() +assert (nz[0] == array([0])).all() + +nz = array([[1, 0, 3], [2, 0, 4]]).nonzero() assert (nz[0] == array([0, 0, 1, 1])).all() assert (nz[1] == array([0, 2, 0, 2])).all() ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy pypy-pyarray: - Add missing symbols for importing matplotlib.
Author: Stefan H. Muller Branch: pypy-pyarray Changeset: r66340:ca566c12805d Date: 2013-08-01 08:54 +0200 http://bitbucket.org/pypy/pypy/changeset/ca566c12805d/ Log:- Add missing symbols for importing matplotlib. diff --git a/pypy/module/cpyext/api.py b/pypy/module/cpyext/api.py --- a/pypy/module/cpyext/api.py +++ b/pypy/module/cpyext/api.py @@ -401,6 +401,16 @@ 'PyThread_ReInitTLS', 'PyStructSequence_InitType', 'PyStructSequence_New', + +'PyFunction_Type', 'PyMethod_Type', 'PyRange_Type', 'PyTraceBack_Type', + +'PyArray_ZEROS', + +'Py_DebugFlag', 'Py_VerboseFlag', 'Py_InteractiveFlag', 'Py_InspectFlag', +'Py_OptimizeFlag', 'Py_NoSiteFlag', 'Py_BytesWarningFlag', 'Py_UseClassExceptionsFlag', +'Py_FrozenFlag', 'Py_TabcheckFlag', 'Py_UnicodeFlag', 'Py_IgnoreEnvironmentFlag', +'Py_DivisionWarningFlag', 'Py_DontWriteBytecodeFlag', 'Py_NoUserSiteDirectory', +'_Py_QnewFlag', 'Py_Py3kWarningFlag', 'Py_HashRandomizationFlag', '_Py_PackageContext', ] TYPES = {} GLOBALS = { # this needs to include all prebuilt pto, otherwise segfaults occur @@ -990,6 +1000,8 @@ source_dir / "capsule.c", source_dir / "pysignals.c", source_dir / "pythread.c", + source_dir / "ndarrayobject.c", + source_dir / "missing.c", ], separate_module_sources=separate_module_sources, export_symbols=export_symbols_eci, diff --git a/pypy/module/cpyext/include/missing.h b/pypy/module/cpyext/include/missing.h --- a/pypy/module/cpyext/include/missing.h +++ b/pypy/module/cpyext/include/missing.h @@ -7,7 +7,9 @@ extern "C" { #endif -PyAPI_DATA(PyTypeObject) PyMethod_Type, PyRange_Type, PyTraceBack_Type; +PyAPI_DATA(PyTypeObject) PyMethod_Type; +PyAPI_DATA(PyTypeObject) PyRange_Type; +PyAPI_DATA(PyTypeObject) PyTraceBack_Type; #ifdef __cplusplus } diff --git a/pypy/module/cpyext/include/numpy/arrayobject.h b/pypy/module/cpyext/include/numpy/arrayobject.h --- a/pypy/module/cpyext/include/numpy/arrayobject.h +++ b/pypy/module/cpyext/include/numpy/arrayobject.h @@ -7,8 +7,6 @@ extern "C" { #endif -#include/* memset */ - #include "old_defines.h" #define NPY_INLINE @@ -116,16 +114,7 @@ #define PyArray_EMPTY(nd, dims, type_num, fortran) \ PyArray_SimpleNew(nd, dims, type_num) -#define PyArray_ZEROS PyArray_EMPTY - -/* -PyObject* PyArray_ZEROS(int nd, npy_intp* dims, int type_num, int fortran) -{ -PyObject *arr = PyArray_EMPTY(nd, dims, type_num, fortran); -memset(PyArray_DATA(arr), 0, PyArray_NBYTES(arr)); -return arr; -}; -*/ +PyObject* PyArray_ZEROS(int nd, npy_intp* dims, int type_num, int fortran); #define PyArray_Resize(self, newshape, refcheck, fortran) (NULL) diff --git a/pypy/module/cpyext/include/pythonrun.h b/pypy/module/cpyext/include/pythonrun.h --- a/pypy/module/cpyext/include/pythonrun.h +++ b/pypy/module/cpyext/include/pythonrun.h @@ -8,15 +8,6 @@ void Py_FatalError(const char *msg); -/* the -3 option will probably not be implemented */ -/* -#define Py_Py3kWarningFlag 0 - -#define Py_FrozenFlag 0 -#define Py_VerboseFlag 0 -#define Py_DebugFlag 1 -*/ - /* taken from Python-2.7.3/Include/pydebug.h */ PyAPI_DATA(int) Py_DebugFlag; PyAPI_DATA(int) Py_VerboseFlag; diff --git a/pypy/module/cpyext/src/missing.c b/pypy/module/cpyext/src/missing.c new file mode 100644 --- /dev/null +++ b/pypy/module/cpyext/src/missing.c @@ -0,0 +1,29 @@ +/* Definitions of missing symbols go here */ + +#include "Python.h" + +PyTypeObject PyFunction_Type; + +PyTypeObject PyMethod_Type; +PyTypeObject PyRange_Type; +PyTypeObject PyTraceBack_Type; + +int Py_DebugFlag = 1; +int Py_VerboseFlag = 0; +int Py_InteractiveFlag = 0; +int Py_InspectFlag = 0; +int Py_OptimizeFlag = 0; +int Py_NoSiteFlag = 0; +int Py_BytesWarningFlag = 0; +int Py_UseClassExceptionsFlag = 0; +int Py_FrozenFlag = 0; +int Py_TabcheckFlag = 0; +int Py_UnicodeFlag = 0; +int Py_IgnoreEnvironmentFlag = 0; +int Py_DivisionWarningFlag = 0; +int Py_DontWriteBytecodeFlag = 0; +int Py_NoUserSiteDirectory = 0; +int _Py_QnewFlag = 0; +int Py_Py3kWarningFlag = 0; +int Py_HashRandomizationFlag = 0; + diff --git a/pypy/module/cpyext/src/ndarrayobject.c b/pypy/module/cpyext/src/ndarrayobject.c new file mode 100644 --- /dev/null +++ b/pypy/module/cpyext/src/ndarrayobject.c @@ -0,0 +1,13 @@ + +#include "Python.h" +#include "numpy/arrayobject.h" +#include/* memset */ + +PyObject* +PyArray_ZEROS(int nd, npy_intp* dims, int type_num, int fortran) +{ +PyObject *arr = PyArray_EMPTY(nd, dims, type_num, fortran); +memset(PyArray_DATA(arr), 0, PyArray_NBYTES(arr)); +return arr; +} + ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy pypy-pyarray: Implement PyArray_FLAGS()
Author: Stefan H. Muller Branch: pypy-pyarray Changeset: r66344:232f215b317d Date: 2013-08-08 16:16 +0200 http://bitbucket.org/pypy/pypy/changeset/232f215b317d/ Log:Implement PyArray_FLAGS() - ndarrayobject.py: Implement PyArray_FLAGS() instead of PyArray_ISCONTIGUOUS(). This should yield correct results w.r.t. CPython. - arrayobject.h: Literally copy many more macros from numpy, which can be all traced back to PyArray_FLAGS(). diff --git a/pypy/module/cpyext/include/numpy/arrayobject.h b/pypy/module/cpyext/include/numpy/arrayobject.h --- a/pypy/module/cpyext/include/numpy/arrayobject.h +++ b/pypy/module/cpyext/include/numpy/arrayobject.h @@ -10,12 +10,15 @@ #include "old_defines.h" #define NPY_INLINE +#define NPY_UNUSED(x) x +#define PyArray_MAX(a,b) (((a)>(b))?(a):(b)) +#define PyArray_MIN(a,b) (((a)<(b))?(a):(b)) /* fake PyArrayObject so that code that doesn't do direct field access works */ #define PyArrayObject PyObject #define PyArray_Descr PyObject -PyTypeObject PyArray_Type; +//PyTypeObject PyArray_Type; typedef unsigned char npy_bool; typedef unsigned char npy_uint8; @@ -95,35 +98,79 @@ #define PyArray_ISCOMPLEX(arr) (PyTypeNum_ISCOMPLEX(PyArray_TYPE(arr))) -/* selection of flags */ -#define NPY_CONTIGUOUS 0x0001 -#define NPY_FORTRAN 0x0002 -#define NPY_OWNDATA 0x0004 -#define NPY_FORCECAST 0x0010 -#define NPY_ALIGNED 0x0100 -#define NPY_NOTSWAPPED 0x0200 -#define NPY_WRITEABLE 0x0400 -#define NPY_C_CONTIGUOUSNPY_CONTIGUOUS -#define NPY_F_CONTIGUOUSNPY_FORTRAN -#define NPY_IN_ARRAY (NPY_C_CONTIGUOUS | NPY_ALIGNED) -#define NPY_BEHAVED(NPY_ALIGNED | NPY_WRITEABLE) -#define NPY_CARRAY (NPY_CONTIGUOUS | NPY_BEHAVED) -#define NPY_FARRAY (NPY_FORTRAN | NPY_BEHAVED) -#define NPY_DEFAULT NPY_CARRAY +/* flags */ +#define NPY_ARRAY_C_CONTIGUOUS0x0001 +#define NPY_ARRAY_F_CONTIGUOUS0x0002 +#define NPY_ARRAY_OWNDATA 0x0004 +#define NPY_ARRAY_FORCECAST 0x0010 +#define NPY_ARRAY_ENSURECOPY 0x0020 +#define NPY_ARRAY_ENSUREARRAY 0x0040 +#define NPY_ARRAY_ELEMENTSTRIDES 0x0080 +#define NPY_ARRAY_ALIGNED 0x0100 +#define NPY_ARRAY_NOTSWAPPED 0x0200 +#define NPY_ARRAY_WRITEABLE 0x0400 +#define NPY_ARRAY_UPDATEIFCOPY0x1000 + +#define NPY_ARRAY_BEHAVED (NPY_ARRAY_ALIGNED | \ +NPY_ARRAY_WRITEABLE) +#define NPY_ARRAY_BEHAVED_NS (NPY_ARRAY_ALIGNED | \ +NPY_ARRAY_WRITEABLE | \ +NPY_ARRAY_NOTSWAPPED) +#define NPY_ARRAY_CARRAY (NPY_ARRAY_C_CONTIGUOUS | \ +NPY_ARRAY_BEHAVED) +#define NPY_ARRAY_CARRAY_RO(NPY_ARRAY_C_CONTIGUOUS | \ +NPY_ARRAY_ALIGNED) +#define NPY_ARRAY_FARRAY (NPY_ARRAY_F_CONTIGUOUS | \ +NPY_ARRAY_BEHAVED) +#define NPY_ARRAY_FARRAY_RO(NPY_ARRAY_F_CONTIGUOUS | \ +NPY_ARRAY_ALIGNED) +#define NPY_ARRAY_DEFAULT (NPY_ARRAY_CARRAY) +#define NPY_ARRAY_IN_ARRAY (NPY_ARRAY_CARRAY_RO) +#define NPY_ARRAY_OUT_ARRAY(NPY_ARRAY_CARRAY) +#define NPY_ARRAY_INOUT_ARRAY (NPY_ARRAY_CARRAY | \ +NPY_ARRAY_UPDATEIFCOPY) +#define NPY_ARRAY_IN_FARRAY(NPY_ARRAY_FARRAY_RO) +#define NPY_ARRAY_OUT_FARRAY (NPY_ARRAY_FARRAY) +#define NPY_ARRAY_INOUT_FARRAY (NPY_ARRAY_FARRAY | \ +NPY_ARRAY_UPDATEIFCOPY) + +#define NPY_ARRAY_UPDATE_ALL (NPY_ARRAY_C_CONTIGUOUS | \ +NPY_ARRAY_F_CONTIGUOUS | \ +NPY_ARRAY_ALIGNED) + +#define NPY_FARRAY NPY_ARRAY_FARRAY +#define NPY_CARRAY NPY_ARRAY_CARRAY + +#define PyArray_CHKFLAGS(m, flags) (PyArray_FLAGS(m) & (flags)) + +#define PyArray_ISCONTIGUOUS(m) PyArray_CHKFLAGS(m, NPY_ARRAY_C_CONTIGUOUS) +#define PyArray_ISWRITEABLE(m) PyArray_CHKFLAGS(m, NPY_ARRAY_WRITEABLE) +#define PyArray_ISALIGNED(m) PyArray_CHKFLAGS(m, NPY_ARRAY_ALIGNED) + +#define PyArray_IS_C_CONTIGUOUS(m) PyArray_CHKFLAGS(m, NPY_ARRAY_C_CONTIGUOUS) +#define PyArray_IS_F_CONTIGUOUS(m) PyArray_CHKFLAGS(m, NPY_ARRAY_F_CONTIGUOUS) + +#define PyArray_FLAGSWAP(m, flags) (PyArray_CHKFLAGS(m, flags) && \ +PyArray_ISNOTSWAPPED(m)) + +#define PyArray_ISCARRAY(m) PyArray_FLAGSWAP(m, NPY_ARRAY_CARRAY) +#define PyArray_ISCARRAY_RO(m) PyArray_FLAGSWAP(m, NPY_ARRAY_CARRAY_RO) +#define PyArray_ISFARRAY(m) PyArray_FLAGSWAP(m, NPY_ARRAY_FARRAY) +#define PyArray_ISFARRAY_RO(m) PyArray_FLAGSWAP(m, NPY_ARRAY_FARRAY_RO) +#define PyArray_ISBEHAVED(m) PyArray_FLAGSWAP(m, NPY_ARRAY_BEHAVED) +#define PyArray_ISBEHAVED_RO(m) PyArray_FLAGSWAP(m, NPY_ARRAY_ALIGNED) + +#define PyArray_ISONESEGMENT(arr) (1) +#define PyArray_ISNOTSWAPPED(arr) (1) +#define PyArray_ISBYTESWAPPED(arr) (0) + /* functions */ #ifndef PyArray_ND
[pypy-commit] pypy pypy-pyarray: - cpyext/include/numpy/arrayobject.h: Many more definitions. Succeeded
Author: Stefan H. Muller Branch: pypy-pyarray Changeset: r66339:4a0adafeea01 Date: 2013-07-31 17:55 +0200 http://bitbucket.org/pypy/pypy/changeset/4a0adafeea01/ Log:- cpyext/include/numpy/arrayobject.h: Many more definitions. Succeeded to compile a patched version of matplotlib with this. diff --git a/pypy/module/cpyext/include/numpy/arrayobject.h b/pypy/module/cpyext/include/numpy/arrayobject.h --- a/pypy/module/cpyext/include/numpy/arrayobject.h +++ b/pypy/module/cpyext/include/numpy/arrayobject.h @@ -26,6 +26,12 @@ #define import_array() #endif +#define NPY_MAXDIMS 32 + +typedef struct { +npy_intp *ptr; +int len; +} PyArray_Dims; /* data types copied from numpy/ndarraytypes.h * keep numbers in sync with micronumpy.interp_dtype.DTypeCache @@ -70,10 +76,18 @@ #define NPY_COMPLEX64 NPY_CDOUBLE +/* selection of flags */ +#define NPY_C_CONTIGUOUS0x0001 +#define NPY_OWNDATA 0x0004 +#define NPY_ALIGNED 0x0100 +#define NPY_IN_ARRAY (NPY_C_CONTIGUOUS | NPY_ALIGNED) + + /* functions */ #ifndef PyArray_NDIM #define PyArray_ISCONTIGUOUS(arr) (1) +#define PyArray_Check(arr) (1) #define PyArray_NDIM _PyArray_NDIM #define PyArray_DIM _PyArray_DIM @@ -84,11 +98,16 @@ #define PyArray_TYPE _PyArray_TYPE #define PyArray_DATA _PyArray_DATA -#define PyArray_BYTES(obj) ((char *)PyArray_DATA(obj)) +#define PyArray_Size PyArray_SIZE +#define PyArray_BYTES(arr) ((char *)PyArray_DATA(arr)) #define PyArray_FromAny _PyArray_FromAny #define PyArray_FromObject _PyArray_FromObject #define PyArray_ContiguousFromObject PyArray_FromObject +#define PyArray_ContiguousFromAny PyArray_FromObject + +#define PyArray_FROMANY(obj, typenum, min, max, requirements) (obj) +#define PyArray_FROM_OTF(obj, typenum, requirements) (obj) #define PyArray_SimpleNew _PyArray_SimpleNew #define PyArray_SimpleNewFromData _PyArray_SimpleNewFromData @@ -105,9 +124,11 @@ PyObject *arr = PyArray_EMPTY(nd, dims, type_num, fortran); memset(PyArray_DATA(arr), 0, PyArray_NBYTES(arr)); return arr; -} +}; */ +#define PyArray_Resize(self, newshape, refcheck, fortran) (NULL) + /* Don't use these in loops! */ #define PyArray_GETPTR1(obj, i) ((void *)(PyArray_BYTES(obj) + \ ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy pypy-pyarray: Implement ndarray.nonzero()
Author: Stefan H. Muller Branch: pypy-pyarray Changeset: r66348:f0b2849dfe98 Date: 2013-08-11 18:59 +0200 http://bitbucket.org/pypy/pypy/changeset/f0b2849dfe98/ Log:Implement ndarray.nonzero() diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py --- a/pypy/module/micronumpy/interp_numarray.py +++ b/pypy/module/micronumpy/interp_numarray.py @@ -5,7 +5,7 @@ from pypy.module.micronumpy.base import W_NDimArray, convert_to_array,\ ArrayArgumentException, issequence_w, wrap_impl from pypy.module.micronumpy import interp_dtype, interp_ufuncs, interp_boxes,\ - interp_arrayops + interp_arrayops, iter from pypy.module.micronumpy.strides import find_shape_and_elems,\ get_shape_from_iterable, to_coords, shape_agreement, \ shape_agreement_multiple @@ -351,6 +351,31 @@ "order not implemented")) return self.descr_reshape(space, [space.wrap(-1)]) +def descr_nonzero(self, space): +impl = self.implementation +arr_iter = iter.MultiDimViewIterator(impl, impl.dtype, 0, +impl.strides, impl.backstrides, impl.shape) + +index_type = interp_dtype.get_dtype_cache(space).w_int64dtype +box = index_type.itemtype.box + +nd = len(impl.shape) +s = loop.count_all_true(self) +w_res = W_NDimArray.from_shape(space, [s, nd], index_type) +res_iter = w_res.create_iter() + +dims = range(nd) +while not arr_iter.done(): +if arr_iter.getitem_bool(): +for d in dims: +res_iter.setitem(box(arr_iter.indexes[d])) +res_iter.next() +arr_iter.next() + +w_res = w_res.implementation.swapaxes(space, w_res, 0, 1) +l_w = [w_res.descr_getitem(space, space.wrap(d)) for d in dims] +return space.newtuple(l_w) + def descr_take(self, space, w_obj, w_axis=None, w_out=None): # if w_axis is None and w_out is Nont this is an equivalent to # fancy indexing @@ -707,7 +732,7 @@ descr_conj = _unaryop_impl('conjugate') -def descr_nonzero(self, space): +def descr___nonzero__(self, space): if self.get_size() > 1: raise OperationError(space.w_ValueError, space.wrap( "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()")) @@ -995,7 +1020,7 @@ __neg__ = interp2app(W_NDimArray.descr_neg), __abs__ = interp2app(W_NDimArray.descr_abs), __invert__ = interp2app(W_NDimArray.descr_invert), -__nonzero__ = interp2app(W_NDimArray.descr_nonzero), +__nonzero__ = interp2app(W_NDimArray.descr___nonzero__), __add__ = interp2app(W_NDimArray.descr_add), __sub__ = interp2app(W_NDimArray.descr_sub), @@ -1070,6 +1095,7 @@ tolist = interp2app(W_NDimArray.descr_tolist), flatten = interp2app(W_NDimArray.descr_flatten), ravel = interp2app(W_NDimArray.descr_ravel), +nonzero = interp2app(W_NDimArray.descr_nonzero), take = interp2app(W_NDimArray.descr_take), compress = interp2app(W_NDimArray.descr_compress), repeat = interp2app(W_NDimArray.descr_repeat), diff --git a/pypy/module/micronumpy/test/test_numarray.py b/pypy/module/micronumpy/test/test_numarray.py --- a/pypy/module/micronumpy/test/test_numarray.py +++ b/pypy/module/micronumpy/test/test_numarray.py @@ -2300,6 +2300,13 @@ assert (arange(6).reshape(2, 3).ravel() == arange(6)).all() assert (arange(6).reshape(2, 3).T.ravel() == [0, 3, 1, 4, 2, 5]).all() +def test_nonzero(self): +from numpypy import array +a = array([[1, 0, 3], [2, 0, 4]]) +nz = a.nonzero() +assert (nz[0] == array([0, 0, 1, 1])).all() +assert (nz[1] == array([0, 2, 0, 2])).all() + def test_take(self): from numpypy import arange try: ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy pypy-pyarray: - lib_pypy/numpypy/__init__.py: __version__ and get_include() now
Author: Stefan H. Muller Branch: pypy-pyarray Changeset: r66341:3c29d99be539 Date: 2013-08-05 05:03 +0200 http://bitbucket.org/pypy/pypy/changeset/3c29d99be539/ Log:- lib_pypy/numpypy/__init__.py: __version__ and get_include() now defined here, so that site-packages/numpy can get it. - lib_pypy/numpy.py: Contains only the warning. This file should be shadowed if site-packages/numpy is installed. It presently isn't so it must be renamed. - cpyext/include/complexobject.h: Fix error in macro usage. - micronumpy/base.py: convert_to_array(): Add call to __array__() method, if it exists. diff --git a/lib_pypy/numpy.py b/lib_pypy/numpy.py --- a/lib_pypy/numpy.py +++ b/lib_pypy/numpy.py @@ -6,11 +6,4 @@ from numpypy import * -import os -__version__ = '1.6.2' - -def get_include(): -head, tail = os.path.split(os.path.dirname(os.path.abspath(__file__))) -return os.path.join(head, 'include') - diff --git a/lib_pypy/numpypy/__init__.py b/lib_pypy/numpypy/__init__.py --- a/lib_pypy/numpypy/__init__.py +++ b/lib_pypy/numpypy/__init__.py @@ -6,9 +6,19 @@ from __builtin__ import bool, int, long, float, complex, object, unicode, str from core import abs, max, min -__all__ = [] +__version__ = '1.7.0' + +import os +def get_include(): +head, tail = os.path.split(os.path.dirname(os.path.abspath(__file__))) +return os.path.join(head, '../include') + + +__all__ = ['__version__', 'get_include'] __all__ += core.__all__ __all__ += lib.__all__ #import sys #sys.modules.setdefault('numpy', sys.modules['numpypy']) + + diff --git a/pypy/module/cpyext/include/complexobject.h b/pypy/module/cpyext/include/complexobject.h --- a/pypy/module/cpyext/include/complexobject.h +++ b/pypy/module/cpyext/include/complexobject.h @@ -28,7 +28,7 @@ // shmuller 2013/07/30: Make a function, since macro will fail in C++ due to // const correctness if called with "const Py_complex" //#define PyComplex_FromCComplex(c) _PyComplex_FromCComplex(&c) -Py_LOCAL_INLINE(PyObject) *PyComplex_FromCComplex(Py_complex c) { +Py_LOCAL_INLINE(PyObject *) PyComplex_FromCComplex(Py_complex c) { return _PyComplex_FromCComplex(&c); } diff --git a/pypy/module/micronumpy/base.py b/pypy/module/micronumpy/base.py --- a/pypy/module/micronumpy/base.py +++ b/pypy/module/micronumpy/base.py @@ -1,4 +1,5 @@ +from pypy.interpreter.error import OperationError from pypy.interpreter.baseobjspace import W_Root from rpython.tool.pairtype import extendabletype from pypy.module.micronumpy.support import calc_strides @@ -91,10 +92,20 @@ if isinstance(w_obj, W_NDimArray): return w_obj -elif issequence_w(space, w_obj): -# Convert to array. -return array(space, w_obj, w_order=None) else: -# If it's a scalar -dtype = interp_ufuncs.find_dtype_for_scalar(space, w_obj) -return W_NDimArray.new_scalar(space, dtype, w_obj) +# Use __array__() method if it exists +w_array = space.lookup(w_obj, "__array__") +if w_array is not None: +w_result = space.get_and_call_function(w_array, w_obj) +if isinstance(w_result, W_NDimArray): +return w_result +else: +raise OperationError(space.w_ValueError, +space.wrap("object __array__ method not producing an array")) +elif issequence_w(space, w_obj): +# Convert to array. +return array(space, w_obj, w_order=None) +else: +# If it's a scalar +dtype = interp_ufuncs.find_dtype_for_scalar(space, w_obj) +return W_NDimArray.new_scalar(space, dtype, w_obj) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy pypy-pyarray: - cpyext/include/complexobject.h: Add Py_LOCAL_INLINE() to translate.
Author: Stefan H. Muller Branch: pypy-pyarray Changeset: r66335:92a2115f0785 Date: 2013-07-30 16:29 +0200 http://bitbucket.org/pypy/pypy/changeset/92a2115f0785/ Log:- cpyext/include/complexobject.h: Add Py_LOCAL_INLINE() to translate. - cpyext/stringobject.py: Replace 4x rffi.CCHARP -> CONST_STRING for const correctness. diff --git a/pypy/module/cpyext/include/complexobject.h b/pypy/module/cpyext/include/complexobject.h --- a/pypy/module/cpyext/include/complexobject.h +++ b/pypy/module/cpyext/include/complexobject.h @@ -28,7 +28,7 @@ // shmuller 2013/07/30: Make a function, since macro will fail in C++ due to // const correctness if called with "const Py_complex" //#define PyComplex_FromCComplex(c) _PyComplex_FromCComplex(&c) -PyObject *PyComplex_FromCComplex(Py_complex c) { +Py_LOCAL_INLINE(PyObject) *PyComplex_FromCComplex(Py_complex c) { return _PyComplex_FromCComplex(&c); } diff --git a/pypy/module/cpyext/stringobject.py b/pypy/module/cpyext/stringobject.py --- a/pypy/module/cpyext/stringobject.py +++ b/pypy/module/cpyext/stringobject.py @@ -275,7 +275,7 @@ Py_DecRef(space, string[0]) string[0] = make_ref(space, w_str) -@cpython_api([PyObject, rffi.CCHARP, rffi.CCHARP], PyObject) +@cpython_api([PyObject, CONST_STRING, CONST_STRING], PyObject) def PyString_AsEncodedObject(space, w_str, encoding, errors): """Encode a string object using the codec registered for encoding and return the result as Python object. encoding and errors have the same meaning as @@ -294,7 +294,7 @@ w_errors = space.wrap(rffi.charp2str(errors)) return space.call_method(w_str, 'encode', w_encoding, w_errors) -@cpython_api([PyObject, rffi.CCHARP, rffi.CCHARP], PyObject) +@cpython_api([PyObject, CONST_STRING, CONST_STRING], PyObject) def PyString_AsDecodedObject(space, w_str, encoding, errors): """Decode a string object by passing it to the codec registered for encoding and return the result as Python object. encoding and ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy pypy-pyarray: array() constructor now uses for __array__() method.
Author: Stefan H. Muller Branch: pypy-pyarray Changeset: r66347:82c65a5a6f82 Date: 2013-08-10 23:20 +0200 http://bitbucket.org/pypy/pypy/changeset/82c65a5a6f82/ Log:array() constructor now uses for __array__() method. diff --git a/pypy/module/micronumpy/base.py b/pypy/module/micronumpy/base.py --- a/pypy/module/micronumpy/base.py +++ b/pypy/module/micronumpy/base.py @@ -87,6 +87,7 @@ def convert_to_array(space, w_obj): +#XXX: This whole routine should very likely simply be array() from pypy.module.micronumpy.interp_numarray import array from pypy.module.micronumpy import interp_ufuncs diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py --- a/pypy/module/micronumpy/interp_numarray.py +++ b/pypy/module/micronumpy/interp_numarray.py @@ -418,7 +418,7 @@ raise OperationError(space.w_NotImplementedError, space.wrap( "non-int arg not supported")) -def descr___array__(self, space): +def descr___array__(self, space, w_dtype=None): # stub implementation of __array__() return self @@ -1113,13 +1113,27 @@ @unwrap_spec(ndmin=int, copy=bool, subok=bool) def array(space, w_object, w_dtype=None, copy=True, w_order=None, subok=False, ndmin=0): +# for anything that isn't already an array, try __array__ method first +if not isinstance(w_object, W_NDimArray): +w___array__ = space.lookup(w_object, "__array__") +if w___array__ is not None: +w_array = space.get_and_call_function(w___array__, w_object, w_dtype) +if isinstance(w_array, W_NDimArray): +# feed w_array back into array() for other properties +return array(space, w_array, w_dtype, False, w_order, subok, ndmin) +else: +raise operationerrfmt(space.w_ValueError, +"object __array__ method not producing an array") + +# scalars and strings w/o __array__ method isstr = space.isinstance_w(w_object, space.w_str) if not issequence_w(space, w_object) or isstr: if space.is_none(w_dtype) or isstr: w_dtype = interp_ufuncs.find_dtype_for_scalar(space, w_object) -dtype = space.interp_w(interp_dtype.W_Dtype, - space.call_function(space.gettypefor(interp_dtype.W_Dtype), w_dtype)) +dtype = space.interp_w(interp_dtype.W_Dtype, +space.call_function(space.gettypefor(interp_dtype.W_Dtype), w_dtype)) return W_NDimArray.new_scalar(space, dtype, w_object) + if space.is_none(w_order): order = 'C' else: @@ -1128,6 +1142,7 @@ raise operationerrfmt(space.w_ValueError, "Unknown order: %s", order) +# arrays with correct dtype dtype = interp_dtype.decode_w_dtype(space, w_dtype) if isinstance(w_object, W_NDimArray) and \ (space.is_none(w_dtype) or w_object.get_dtype() is dtype): @@ -1144,6 +1159,8 @@ w_ret.implementation = w_ret.implementation.set_shape(space, w_ret, shape) return w_ret + +# not an array or incorrect dtype shape, elems_w = find_shape_and_elems(space, w_object, dtype) if dtype is None or ( dtype.is_str_or_unicode() and dtype.itemtype.get_size() < 1): ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy pypy-pyarray: - cpyext/include/numpy/arrayobject.h: Many more definitions.
Author: Stefan H. Muller Branch: pypy-pyarray Changeset: r66338:53d9c2233349 Date: 2013-07-31 02:59 +0200 http://bitbucket.org/pypy/pypy/changeset/53d9c2233349/ Log:- cpyext/include/numpy/arrayobject.h: Many more definitions. diff --git a/pypy/module/cpyext/include/numpy/arrayobject.h b/pypy/module/cpyext/include/numpy/arrayobject.h --- a/pypy/module/cpyext/include/numpy/arrayobject.h +++ b/pypy/module/cpyext/include/numpy/arrayobject.h @@ -7,6 +7,8 @@ extern "C" { #endif +#include/* memset */ + #include "old_defines.h" #define NPY_INLINE @@ -14,6 +16,9 @@ /* fake PyArrayObject so that code that doesn't do direct field access works */ #define PyArrayObject PyObject +typedef unsigned char npy_bool; +typedef unsigned char npy_uint8; + #ifndef npy_intp #define npy_intp long #endif @@ -21,30 +26,8 @@ #define import_array() #endif -#ifndef PyArray_NDIM -#define PyArray_ISCONTIGUOUS(arr) (1) - -#define PyArray_NDIM _PyArray_NDIM -#define PyArray_DIM _PyArray_DIM -#define PyArray_STRIDE _PyArray_STRIDE -#define PyArray_SIZE _PyArray_SIZE -#define PyArray_ITEMSIZE _PyArray_ITEMSIZE -#define PyArray_NBYTES _PyArray_NBYTES -#define PyArray_TYPE _PyArray_TYPE -#define PyArray_DATA _PyArray_DATA - -#define PyArray_FromAny _PyArray_FromAny -#define PyArray_FromObject _PyArray_FromObject -#define PyArray_ContiguousFromObject PyArray_FromObject - -#define PyArray_SimpleNew _PyArray_SimpleNew -#define PyArray_SimpleNewFromData _PyArray_SimpleNewFromData -#define PyArray_SimpleNewFromDataOwning _PyArray_SimpleNewFromDataOwning - -#endif - -/* copied from numpy/ndarraytypes.h +/* data types copied from numpy/ndarraytypes.h * keep numbers in sync with micronumpy.interp_dtype.DTypeCache */ enum NPY_TYPES {NPY_BOOL=0, @@ -86,6 +69,67 @@ #define NPY_COMPLEX32 NPY_CFLOAT #define NPY_COMPLEX64 NPY_CDOUBLE + +/* functions */ +#ifndef PyArray_NDIM + +#define PyArray_ISCONTIGUOUS(arr) (1) + +#define PyArray_NDIM _PyArray_NDIM +#define PyArray_DIM _PyArray_DIM +#define PyArray_STRIDE _PyArray_STRIDE +#define PyArray_SIZE _PyArray_SIZE +#define PyArray_ITEMSIZE _PyArray_ITEMSIZE +#define PyArray_NBYTES _PyArray_NBYTES +#define PyArray_TYPE _PyArray_TYPE +#define PyArray_DATA _PyArray_DATA + +#define PyArray_BYTES(obj) ((char *)PyArray_DATA(obj)) + +#define PyArray_FromAny _PyArray_FromAny +#define PyArray_FromObject _PyArray_FromObject +#define PyArray_ContiguousFromObject PyArray_FromObject + +#define PyArray_SimpleNew _PyArray_SimpleNew +#define PyArray_SimpleNewFromData _PyArray_SimpleNewFromData +#define PyArray_SimpleNewFromDataOwning _PyArray_SimpleNewFromDataOwning + +#define PyArray_EMPTY(nd, dims, type_num, fortran) \ +PyArray_SimpleNew(nd, dims, type_num) + +#define PyArray_ZEROS PyArray_EMPTY + +/* +PyObject* PyArray_ZEROS(int nd, npy_intp* dims, int type_num, int fortran) +{ +PyObject *arr = PyArray_EMPTY(nd, dims, type_num, fortran); +memset(PyArray_DATA(arr), 0, PyArray_NBYTES(arr)); +return arr; +} +*/ + +/* Don't use these in loops! */ + +#define PyArray_GETPTR1(obj, i) ((void *)(PyArray_BYTES(obj) + \ + (i)*PyArray_STRIDE(obj,0))) + +#define PyArray_GETPTR2(obj, i, j) ((void *)(PyArray_BYTES(obj) + \ +(i)*PyArray_STRIDE(obj,0) + \ +(j)*PyArray_STRIDE(obj,1))) + +#define PyArray_GETPTR3(obj, i, j, k) ((void *)(PyArray_BYTES(obj) + \ +(i)*PyArray_STRIDE(obj,0) + \ +(j)*PyArray_STRIDE(obj,1) + \ +(k)*PyArray_STRIDE(obj,2))) + +#define PyArray_GETPTR4(obj, i, j, k, l) ((void *)(PyArray_BYTES(obj) + \ +(i)*PyArray_STRIDE(obj,0) + \ +(j)*PyArray_STRIDE(obj,1) + \ +(k)*PyArray_STRIDE(obj,2) + \ +(l)*PyArray_STRIDE(obj,3))) + +#endif + #ifdef __cplusplus } #endif ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy pypy-pyarray: Implement fijal's approach for nonzero()
Author: Stefan H. Muller Branch: pypy-pyarray Changeset: r66351:81c7341f996b Date: 2013-08-13 01:06 +0200 http://bitbucket.org/pypy/pypy/changeset/81c7341f996b/ Log:Implement fijal's approach for nonzero() - Iterators get get_index() method. - create_iter() get additional keyword argument 'require_index'. diff --git a/pypy/module/micronumpy/arrayimpl/base.py b/pypy/module/micronumpy/arrayimpl/base.py --- a/pypy/module/micronumpy/arrayimpl/base.py +++ b/pypy/module/micronumpy/arrayimpl/base.py @@ -6,7 +6,7 @@ def base(self): raise NotImplementedError -def create_iter(self, shape=None, backward_broadcast=False): +def create_iter(self, shape=None, backward_broadcast=False, require_index=False): raise NotImplementedError class BaseArrayIterator(object): diff --git a/pypy/module/micronumpy/arrayimpl/concrete.py b/pypy/module/micronumpy/arrayimpl/concrete.py --- a/pypy/module/micronumpy/arrayimpl/concrete.py +++ b/pypy/module/micronumpy/arrayimpl/concrete.py @@ -282,18 +282,12 @@ def nonzero(self, space, index_type): s = loop.count_all_true_concrete(self) box = index_type.itemtype.box -nd = len(self.shape) - -if nd == 1: -w_res = W_NDimArray.from_shape(space, [s], index_type) -loop.nonzero_onedim(w_res, self, box) -return space.newtuple([w_res]) -else: -w_res = W_NDimArray.from_shape(space, [s, nd], index_type) -loop.nonzero_multidim(w_res, self, box) -w_res = w_res.implementation.swapaxes(space, w_res, 0, 1) -l_w = [w_res.descr_getitem(space, space.wrap(d)) for d in range(nd)] -return space.newtuple(l_w) +nd = len(self.get_shape()) +w_res = W_NDimArray.from_shape(space, [s, nd], index_type) +loop.nonzero(w_res, self, box) +w_res = w_res.implementation.swapaxes(space, w_res, 0, 1) +l_w = [w_res.descr_getitem(space, space.wrap(d)) for d in range(nd)] +return space.newtuple(l_w) def get_storage_as_int(self, space): return rffi.cast(lltype.Signed, self.storage) + self.start @@ -331,13 +325,22 @@ self.backstrides = backstrides self.storage = storage -def create_iter(self, shape=None, backward_broadcast=False): -if shape is None or shape == self.get_shape(): +def create_iter(self, shape=None, backward_broadcast=False, require_index=False): +if shape is not None and shape != self.get_shape(): +r = calculate_broadcast_strides(self.get_strides(), +self.get_backstrides(), +self.get_shape(), shape, backward_broadcast) +return iter.MultiDimViewIterator(self, self.dtype, self.start, r[0], r[1], shape) + +if not require_index: return iter.ConcreteArrayIterator(self) -r = calculate_broadcast_strides(self.get_strides(), -self.get_backstrides(), -self.get_shape(), shape, backward_broadcast) -return iter.MultiDimViewIterator(self, self.dtype, 0, r[0], r[1], shape) +else: +if len(self.get_shape()) == 1: +return iter.OneDimViewIterator(self, self.dtype, self.start, +self.get_strides(), self.get_shape()) +else: +return iter.MultiDimViewIterator(self, self.dtype, self.start, +self.get_strides(), self.get_backstrides(), self.get_shape()) def fill(self, box): self.dtype.fill(self.storage, box, 0, self.size) @@ -400,7 +403,7 @@ def fill(self, box): loop.fill(self, box.convert_to(self.dtype)) -def create_iter(self, shape=None, backward_broadcast=False): +def create_iter(self, shape=None, backward_broadcast=False, require_index=False): if shape is not None and shape != self.get_shape(): r = calculate_broadcast_strides(self.get_strides(), self.get_backstrides(), diff --git a/pypy/module/micronumpy/arrayimpl/scalar.py b/pypy/module/micronumpy/arrayimpl/scalar.py --- a/pypy/module/micronumpy/arrayimpl/scalar.py +++ b/pypy/module/micronumpy/arrayimpl/scalar.py @@ -45,7 +45,7 @@ def get_backstrides(self): return [] -def create_iter(self, shape=None, backward_broadcast=False): +def create_iter(self, shape=None, backward_broadcast=False, require_index=False): return ScalarIterator(self) def get_scalar_value(self): diff --git a/pypy/module/micronumpy/interp_flatiter.py b/pypy/module/micronumpy/interp_flatiter.py --- a/pypy/module/micronumpy/interp_flatiter.py +++ b/pypy/module/micronumpy/interp_flatiter.py @@ -19,7 +19,7 @@ def get_shape(self): return self.shape -def create_iter(self, sha
[pypy-commit] pypy pypy-pyarray: Put split nonzero() between scalar.py, concrete.py and loops.py.
Author: Stefan H. Muller Branch: pypy-pyarray Changeset: r66350:b70301c90922 Date: 2013-08-12 23:49 +0200 http://bitbucket.org/pypy/pypy/changeset/b70301c90922/ Log:Put split nonzero() between scalar.py, concrete.py and loops.py. - Separate implementations for 1D and ND case. Try to reunify in next commit. diff --git a/pypy/module/micronumpy/arrayimpl/concrete.py b/pypy/module/micronumpy/arrayimpl/concrete.py --- a/pypy/module/micronumpy/arrayimpl/concrete.py +++ b/pypy/module/micronumpy/arrayimpl/concrete.py @@ -279,6 +279,22 @@ return W_NDimArray.new_slice(space, self.start, strides, backstrides, shape, self, orig_arr) +def nonzero(self, space, index_type): +s = loop.count_all_true_concrete(self) +box = index_type.itemtype.box +nd = len(self.shape) + +if nd == 1: +w_res = W_NDimArray.from_shape(space, [s], index_type) +loop.nonzero_onedim(w_res, self, box) +return space.newtuple([w_res]) +else: +w_res = W_NDimArray.from_shape(space, [s, nd], index_type) +loop.nonzero_multidim(w_res, self, box) +w_res = w_res.implementation.swapaxes(space, w_res, 0, 1) +l_w = [w_res.descr_getitem(space, space.wrap(d)) for d in range(nd)] +return space.newtuple(l_w) + def get_storage_as_int(self, space): return rffi.cast(lltype.Signed, self.storage) + self.start diff --git a/pypy/module/micronumpy/arrayimpl/scalar.py b/pypy/module/micronumpy/arrayimpl/scalar.py --- a/pypy/module/micronumpy/arrayimpl/scalar.py +++ b/pypy/module/micronumpy/arrayimpl/scalar.py @@ -155,6 +155,13 @@ def swapaxes(self, space, orig_array, axis1, axis2): raise Exception("should not be called") +def nonzero(self, space, index_type): +s = self.dtype.itemtype.bool(self.value) +w_res = W_NDimArray.from_shape(space, [s], index_type) +if s == 1: +w_res.implementation.setitem(0, index_type.itemtype.box(0)) +return space.newtuple([w_res]) + def fill(self, w_value): self.value = w_value diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py --- a/pypy/module/micronumpy/interp_numarray.py +++ b/pypy/module/micronumpy/interp_numarray.py @@ -332,6 +332,10 @@ return self return self.implementation.swapaxes(space, self, axis1, axis2) +def descr_nonzero(self, space): +index_type = interp_dtype.get_dtype_cache(space).w_int64dtype +return self.implementation.nonzero(space, index_type) + def descr_tolist(self, space): if len(self.get_shape()) == 0: return self.get_scalar_value().item(space) @@ -351,37 +355,6 @@ "order not implemented")) return self.descr_reshape(space, [space.wrap(-1)]) -def descr_nonzero(self, space): -s = loop.count_all_true(self) -index_type = interp_dtype.get_dtype_cache(space).w_int64dtype -box = index_type.itemtype.box - -if self.is_scalar(): -w_res = W_NDimArray.from_shape(space, [s], index_type) -if s == 1: -w_res.implementation.setitem(0, box(0)) -return space.newtuple([w_res]) - -impl = self.implementation -arr_iter = iter.MultiDimViewIterator(impl, impl.dtype, 0, -impl.strides, impl.backstrides, impl.shape) - -nd = len(impl.shape) -w_res = W_NDimArray.from_shape(space, [s, nd], index_type) -res_iter = w_res.create_iter() - -dims = range(nd) -while not arr_iter.done(): -if arr_iter.getitem_bool(): -for d in dims: -res_iter.setitem(box(arr_iter.indexes[d])) -res_iter.next() -arr_iter.next() - -w_res = w_res.implementation.swapaxes(space, w_res, 0, 1) -l_w = [w_res.descr_getitem(space, space.wrap(d)) for d in dims] -return space.newtuple(l_w) - def descr_take(self, space, w_obj, w_axis=None, w_out=None): # if w_axis is None and w_out is Nont this is an equivalent to # fancy indexing @@ -1101,11 +1074,11 @@ tolist = interp2app(W_NDimArray.descr_tolist), flatten = interp2app(W_NDimArray.descr_flatten), ravel = interp2app(W_NDimArray.descr_ravel), -nonzero = interp2app(W_NDimArray.descr_nonzero), take = interp2app(W_NDimArray.descr_take), compress = interp2app(W_NDimArray.descr_compress), repeat = interp2app(W_NDimArray.descr_repeat), swapaxes = interp2app(W_NDimArray.descr_swapaxes), +nonzero = interp2app(W_NDimArray.descr_nonzero), flat = GetSetProperty(W_NDimArray.descr_get_flatiter), item = interp2app(W_NDimArray.descr_item), real = GetSetProperty(W_NDimArray.descr_get_real, diff --git a/pypy/modu
[pypy-commit] pypy pypy-pyarray: Add some missing numpypy features
Author: Stefan H. Muller Branch: pypy-pyarray Changeset: r66342:09db6e0e5c1d Date: 2013-08-06 00:12 +0200 http://bitbucket.org/pypy/pypy/changeset/09db6e0e5c1d/ Log:Add some missing numpypy features - ndarrayobject.py: PyArray_Check(), PyArray_CheckExact() - interp_numarray.py: Stub implementations for __array__(), __array_prepare__(), __array_wrap__(). diff --git a/pypy/module/cpyext/include/numpy/arrayobject.h b/pypy/module/cpyext/include/numpy/arrayobject.h --- a/pypy/module/cpyext/include/numpy/arrayobject.h +++ b/pypy/module/cpyext/include/numpy/arrayobject.h @@ -85,16 +85,17 @@ #ifndef PyArray_NDIM #define PyArray_ISCONTIGUOUS(arr) (1) -#define PyArray_Check(arr) (1) -#define PyArray_NDIM _PyArray_NDIM -#define PyArray_DIM _PyArray_DIM -#define PyArray_STRIDE _PyArray_STRIDE -#define PyArray_SIZE _PyArray_SIZE -#define PyArray_ITEMSIZE _PyArray_ITEMSIZE -#define PyArray_NBYTES _PyArray_NBYTES -#define PyArray_TYPE _PyArray_TYPE -#define PyArray_DATA _PyArray_DATA +#define PyArray_Check _PyArray_Check +#define PyArray_CheckExact _PyArray_CheckExact +#define PyArray_NDIM _PyArray_NDIM +#define PyArray_DIM_PyArray_DIM +#define PyArray_STRIDE _PyArray_STRIDE +#define PyArray_SIZE _PyArray_SIZE +#define PyArray_ITEMSIZE _PyArray_ITEMSIZE +#define PyArray_NBYTES _PyArray_NBYTES +#define PyArray_TYPE _PyArray_TYPE +#define PyArray_DATA _PyArray_DATA #define PyArray_Size PyArray_SIZE #define PyArray_BYTES(arr) ((char *)PyArray_DATA(arr)) diff --git a/pypy/module/cpyext/ndarrayobject.py b/pypy/module/cpyext/ndarrayobject.py --- a/pypy/module/cpyext/ndarrayobject.py +++ b/pypy/module/cpyext/ndarrayobject.py @@ -12,6 +12,20 @@ # the asserts are needed, otherwise the translation fails +@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL) +def _PyArray_Check(space, w_obj): +w_obj_type = space.type(w_obj) +w_type = space.gettypeobject(W_NDimArray.typedef) +return (space.is_w(w_obj_type, w_type) or +space.is_true(space.issubtype(w_obj_type, w_type))) + +@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL) +def _PyArray_CheckExact(space, w_obj): +w_obj_type = space.type(w_obj) +w_type = space.gettypeobject(W_NDimArray.typedef) +return space.is_w(w_obj_type, w_type) + + @cpython_api([PyObject], Py_ssize_t, error=CANNOT_FAIL) def _PyArray_NDIM(space, w_array): assert isinstance(w_array, W_NDimArray) diff --git a/pypy/module/cpyext/test/test_ndarrayobject.py b/pypy/module/cpyext/test/test_ndarrayobject.py --- a/pypy/module/cpyext/test/test_ndarrayobject.py +++ b/pypy/module/cpyext/test/test_ndarrayobject.py @@ -24,6 +24,14 @@ class TestNDArrayObject(BaseApiTest): +def test_Check(self, space, api): +a = array(space, [10, 5, 3]) +x = space.wrap(10.) +assert api._PyArray_Check(a) +assert api._PyArray_CheckExact(a) +assert not api._PyArray_Check(x) +assert not api._PyArray_CheckExact(x) + def test_NDIM(self, space, api): a = array(space, [10, 5, 3]) assert api._PyArray_NDIM(a) == 3 diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py --- a/pypy/module/micronumpy/interp_numarray.py +++ b/pypy/module/micronumpy/interp_numarray.py @@ -418,6 +418,26 @@ raise OperationError(space.w_NotImplementedError, space.wrap( "non-int arg not supported")) +def descr___array__(self, space): +# stub implementation of __array__() +return self + +def descr___array_prepare__(self, space, w_array): +# stub implementation of __array_prepare__() +if isinstance(w_array, W_NDimArray): +return w_array +else: +raise OperationError(space.w_TypeError, + space.wrap("can only be called with ndarray object")) + +def descr___array_wrap__(self, space, w_array): +# stub implementation of __array_wrap__() +if isinstance(w_array, W_NDimArray): +return w_array +else: +raise OperationError(space.w_TypeError, + space.wrap("can only be called with ndarray object")) + def descr_array_iface(self, space): addr = self.implementation.get_storage_as_int(space) # will explode if it can't @@ -1084,6 +1104,10 @@ __reduce__ = interp2app(W_NDimArray.descr_reduce), __setstate__ = interp2app(W_NDimArray.descr_setstate), __array_finalize__ = interp2app(W_NDimArray.descr___array_finalize__), + +__array__ = interp2app(W_NDimArray.descr___array__), +__array_prepare__ = interp2app(W_NDimArray.descr___array_prepare__), +__array_wrap__= interp2app(W_NDimArray.descr___array_wrap__), ) @unwrap_spec(ndmin=int, copy=bool, subok=bool) ___ pypy-commit mailing list pypy-commi
[pypy-commit] pypy pypy-pyarray: - cpyext/ndarrayobject.py: Add support for PyArray_STRIDE() and
Author: Stefan H. Muller Branch: pypy-pyarray Changeset: r66334:3f9be1d43a61 Date: 2013-07-30 11:59 +0200 http://bitbucket.org/pypy/pypy/changeset/3f9be1d43a61/ Log:- cpyext/ndarrayobject.py: Add support for PyArray_STRIDE() and PyArray_FromObject(). - cpyext/include/numpy: Add constants needed by matplotlib. - cpyext/include/complexobject.h: Replace macro with function for const correctness. - lib_pypy/numpy.py: Add __version__. I felt like it's 1.6.2. diff --git a/lib_pypy/numpy.py b/lib_pypy/numpy.py --- a/lib_pypy/numpy.py +++ b/lib_pypy/numpy.py @@ -8,6 +8,8 @@ import os +__version__ = '1.6.2' + def get_include(): head, tail = os.path.split(os.path.dirname(os.path.abspath(__file__))) return os.path.join(head, 'include') diff --git a/pypy/module/cpyext/include/numpy/arrayobject.h b/pypy/module/cpyext/include/numpy/arrayobject.h --- a/pypy/module/cpyext/include/numpy/arrayobject.h +++ b/pypy/module/cpyext/include/numpy/arrayobject.h @@ -7,6 +7,10 @@ extern "C" { #endif +#include "old_defines.h" + +#define NPY_INLINE + /* fake PyArrayObject so that code that doesn't do direct field access works */ #define PyArrayObject PyObject @@ -19,14 +23,20 @@ #ifndef PyArray_NDIM +#define PyArray_ISCONTIGUOUS(arr) (1) + #define PyArray_NDIM _PyArray_NDIM #define PyArray_DIM _PyArray_DIM +#define PyArray_STRIDE _PyArray_STRIDE #define PyArray_SIZE _PyArray_SIZE #define PyArray_ITEMSIZE _PyArray_ITEMSIZE #define PyArray_NBYTES _PyArray_NBYTES #define PyArray_TYPE _PyArray_TYPE #define PyArray_DATA _PyArray_DATA -#define PyArray_FromAny _PyArray_FromAny + +#define PyArray_FromAny _PyArray_FromAny +#define PyArray_FromObject _PyArray_FromObject +#define PyArray_ContiguousFromObject PyArray_FromObject #define PyArray_SimpleNew _PyArray_SimpleNew #define PyArray_SimpleNewFromData _PyArray_SimpleNewFromData @@ -63,6 +73,19 @@ NPY_NTYPES_ABI_COMPATIBLE=21 }; +#define NPY_INT8 NPY_BYTE +#define NPY_UINT8 NPY_UBYTE +#define NPY_INT16 NPY_SHORT +#define NPY_UINT16NPY_USHORT +#define NPY_INT32 NPY_INT +#define NPY_UINT32NPY_UINT +#define NPY_INT64 NPY_LONG +#define NPY_UINT64NPY_ULONG +#define NPY_FLOAT32 NPY_FLOAT +#define NPY_FLOAT64 NPY_DOUBLE +#define NPY_COMPLEX32 NPY_CFLOAT +#define NPY_COMPLEX64 NPY_CDOUBLE + #ifdef __cplusplus } #endif ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit