Author: mattip <matti.pi...@gmail.com> Branch: cleanup-includes Changeset: r83606:45cb9a7c7b7d Date: 2016-04-11 23:19 +0300 http://bitbucket.org/pypy/pypy/changeset/45cb9a7c7b7d/
Log: move ndarray declarations to seperate header, adjust header creation 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 @@ -811,6 +811,11 @@ prologue = ("#include <Python.h>\n" "#include <structmember.h>\n" "#include <src/thread.c>\n") + if use_micronumpy: + prologue = ("#include <Python.h>\n" + "#include <structmember.h>\n" + "#include <pypy_numpy.h>\n" + "#include <src/thread.c>\n") code = (prologue + struct_declaration_code + global_code + @@ -1012,6 +1017,16 @@ for header_name, header_functions in FUNCTIONS_BY_HEADER.iteritems(): if header_name not in decls: header = decls[header_name] = [] + header.append("#ifndef _PYPY_%s\n" % + header_name.upper().replace('.','_')) + header.append("#define _PYPY_%s\n" % + header_name.upper().replace('.','_')) + header.append("#ifndef PYPY_STANDALONE\n") + header.append("#ifdef __cplusplus") + header.append("extern \"C\" {") + header.append("#endif\n") + header.append('#define Signed long /* xxx temporary fix */\n') + header.append('#define Unsigned unsigned long /* xxx temporary fix */\n') else: header = decls[header_name] @@ -1048,13 +1063,16 @@ typ = 'PyObject*' pypy_decls.append('PyAPI_DATA(%s) %s;' % (typ, name)) - pypy_decls.append('#undef Signed /* xxx temporary fix */\n') - pypy_decls.append('#undef Unsigned /* xxx temporary fix */\n') - pypy_decls.append("#ifdef __cplusplus") - pypy_decls.append("}") - pypy_decls.append("#endif") - pypy_decls.append("#endif /*PYPY_STANDALONE*/\n") - pypy_decls.append("#endif /*_PYPY_PYPY_DECL_H*/\n") + for header_name in FUNCTIONS_BY_HEADER.keys(): + header = decls[header_name] + header.append('#undef Signed /* xxx temporary fix */\n') + header.append('#undef Unsigned /* xxx temporary fix */\n') + header.append("#ifdef __cplusplus") + header.append("}") + header.append("#endif") + header.append("#endif /*PYPY_STANDALONE*/\n") + header.append("#endif /*_PYPY_%s_H*/\n" % + header_name.upper().replace('.','_')) for header_name, header_decls in decls.iteritems(): decl_h = udir.join(header_name) @@ -1204,10 +1222,12 @@ PyObjectP, 'pypy_static_pyobjs', eci2, c_type='PyObject **', getter_only=True, declare_as_extern=False) - for name, func in FUNCTIONS.iteritems(): - newname = mangle_name('PyPy', name) or name - deco = entrypoint_lowlevel("cpyext", func.argtypes, newname, relax=True) - deco(func.get_wrapper(space)) + for name, func in FUNCTIONS_BY_HEADER.iteritems(): + for name, func in header_functions.iteritems(): + newname = mangle_name('PyPy', name) or name + deco = entrypoint_lowlevel("cpyext", func.argtypes, newname, + relax=True) + deco(func.get_wrapper(space)) setup_init_functions(eci, translating=True) trunk_include = pypydir.dirpath() / 'include' 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 @@ -27,62 +27,63 @@ ARRAY_DEFAULT = ARRAY_CARRAY -@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL) +HEADER = 'pypy_numpy.h' +@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL, header=HEADER) 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) +@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL, header=HEADER) 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], rffi.INT_real, error=CANNOT_FAIL) +@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL, header=HEADER) def _PyArray_FLAGS(space, w_array): assert isinstance(w_array, W_NDimArray) flags = ARRAY_BEHAVED_NS | w_array.get_flags() return flags -@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL) +@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL, header=HEADER) 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) +@cpython_api([PyObject, Py_ssize_t], Py_ssize_t, error=CANNOT_FAIL, header=HEADER) 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], Py_ssize_t, error=CANNOT_FAIL) +@cpython_api([PyObject, Py_ssize_t], Py_ssize_t, error=CANNOT_FAIL, header=HEADER) def _PyArray_STRIDE(space, w_array, n): assert isinstance(w_array, W_NDimArray) return w_array.implementation.get_strides()[n] -@cpython_api([PyObject], Py_ssize_t, error=CANNOT_FAIL) +@cpython_api([PyObject], Py_ssize_t, error=CANNOT_FAIL, header=HEADER) def _PyArray_SIZE(space, w_array): assert isinstance(w_array, W_NDimArray) return w_array.get_size() -@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL) +@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL, header=HEADER) def _PyArray_ITEMSIZE(space, w_array): assert isinstance(w_array, W_NDimArray) return w_array.get_dtype().elsize -@cpython_api([PyObject], Py_ssize_t, error=CANNOT_FAIL) +@cpython_api([PyObject], Py_ssize_t, error=CANNOT_FAIL, header=HEADER) def _PyArray_NBYTES(space, w_array): assert isinstance(w_array, W_NDimArray) return w_array.get_size() * w_array.get_dtype().elsize -@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL) +@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL, header=HEADER) 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) +@cpython_api([PyObject], rffi.VOIDP, error=CANNOT_FAIL, header=HEADER) def _PyArray_DATA(space, w_array): # fails on scalars - see PyArray_FromAny() assert isinstance(w_array, W_NDimArray) @@ -92,7 +93,7 @@ NULL = lltype.nullptr(rffi.VOIDP.TO) @cpython_api([PyObject, PyArray_Descr, Py_ssize_t, Py_ssize_t, Py_ssize_t, rffi.VOIDP], - PyObject) + PyObject, header=HEADER) def _PyArray_FromAny(space, w_obj, w_dtype, min_depth, max_depth, requirements, context): """ This is the main function used to obtain an array from any nested sequence, or object that exposes the array interface, op. The @@ -146,7 +147,7 @@ w_array.implementation.shape = [] return w_array -@cpython_api([Py_ssize_t], PyObject) +@cpython_api([Py_ssize_t], PyObject, header=HEADER) def _PyArray_DescrFromType(space, typenum): try: dtype = get_dtype_cache(space).dtypes_by_num[typenum] @@ -155,7 +156,7 @@ raise OperationError(space.w_ValueError, space.wrap( '_PyArray_DescrFromType called with invalid dtype %d' % typenum)) -@cpython_api([PyObject, Py_ssize_t, Py_ssize_t, Py_ssize_t], PyObject) +@cpython_api([PyObject, Py_ssize_t, Py_ssize_t, Py_ssize_t], PyObject, header=HEADER) def _PyArray_FromObject(space, w_obj, typenum, min_depth, max_depth): try: dtype = get_dtype_cache(space).dtypes_by_num[typenum] @@ -193,15 +194,15 @@ order=order, owning=owning, w_subtype=w_subtype) -@cpython_api([Py_ssize_t, rffi.LONGP, Py_ssize_t], PyObject) +@cpython_api([Py_ssize_t, rffi.LONGP, Py_ssize_t], PyObject, header=HEADER) def _PyArray_SimpleNew(space, nd, dims, typenum): return simple_new(space, nd, dims, typenum) -@cpython_api([Py_ssize_t, rffi.LONGP, Py_ssize_t, rffi.VOIDP], PyObject) +@cpython_api([Py_ssize_t, rffi.LONGP, Py_ssize_t, rffi.VOIDP], PyObject, header=HEADER) 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) +@cpython_api([Py_ssize_t, rffi.LONGP, Py_ssize_t, rffi.VOIDP], PyObject, header=HEADER) 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); @@ -210,7 +211,7 @@ @cpython_api([rffi.VOIDP, Py_ssize_t, rffi.LONGP, Py_ssize_t, rffi.LONGP, - rffi.VOIDP, Py_ssize_t, Py_ssize_t, PyObject], PyObject) + rffi.VOIDP, Py_ssize_t, Py_ssize_t, PyObject], PyObject, header=HEADER) def _PyArray_New(space, subtype, nd, dims, typenum, strides, data, itemsize, flags, obj): if strides: raise OperationError(space.w_NotImplementedError, @@ -232,7 +233,7 @@ # a problem with casting function pointers? @cpython_api([rffi.CArrayPtr(rffi.CArrayPtr(gufunctype)), rffi.VOIDP, rffi.CCHARP, Py_ssize_t, Py_ssize_t, Py_ssize_t, Py_ssize_t, rffi.CCHARP, rffi.CCHARP, Py_ssize_t, - rffi.CCHARP], PyObject) + rffi.CCHARP], PyObject, header=HEADER) def PyUFunc_FromFuncAndDataAndSignature(space, funcs, data, types, ntypes, nin, nout, identity, name, doc, check_return, signature): w_signature = rffi.charp2str(signature) @@ -258,7 +259,7 @@ return ufunc_generic @cpython_api([rffi.CArrayPtr(rffi.CArrayPtr(gufunctype)), rffi.VOIDP, rffi.CCHARP, Py_ssize_t, Py_ssize_t, - Py_ssize_t, Py_ssize_t, rffi.CCHARP, rffi.CCHARP, Py_ssize_t], PyObject) + Py_ssize_t, Py_ssize_t, rffi.CCHARP, rffi.CCHARP, Py_ssize_t], PyObject, header=HEADER) def PyUFunc_FromFuncAndData(space, funcs, data, types, ntypes, nin, nout, identity, name, doc, check_return): w_signature = ','.join(['()'] * nin) + '->' + ','.join(['()'] * nout) diff --git a/pypy/module/cpyext/src/ndarrayobject.c b/pypy/module/cpyext/src/ndarrayobject.c --- a/pypy/module/cpyext/src/ndarrayobject.c +++ b/pypy/module/cpyext/src/ndarrayobject.c @@ -1,5 +1,6 @@ #include "Python.h" +#include "pypy_numpy.h" #include "numpy/arrayobject.h" #include <string.h> /* memset, memcpy */ 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 @@ -281,7 +281,8 @@ return _PyArray_DescrFromType(typenum); """ ), - ], prologue='#include <numpy/arrayobject.h>') + ], prologue='''#include <numpy/arrayobject.h> +#include <pypy_numpy.h>''') arr = mod.test_simplenew() assert arr.shape == (2, 3) assert arr.dtype.num == 11 #float32 dtype @@ -309,7 +310,8 @@ Py_INCREF(obj); return obj; '''), - ], prologue='#include <numpy/arrayobject.h>') + ], prologue='''#include <numpy/arrayobject.h> +#include <pypy_numpy.h>''') array = ndarray((3, 4), dtype='d') assert mod.check_array(array) is array raises(TypeError, "mod.check_array(42)") @@ -353,6 +355,7 @@ """), ], prologue=''' #include "numpy/ndarraytypes.h" + #include "pypy_numpy.h" /*#include <numpy/ufuncobject.h> generated by numpy setup.py*/ typedef void (*PyUFuncGenericFunction) (char **args, _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit