[Numpy-svn] r5298 - trunk/numpy/doc/cython

numpy-svn@scip... numpy-svn@scip...
Thu Jun 19 01:16:53 CDT 2008


Author: fperez
Date: 2008-06-19 01:16:48 -0500 (Thu, 19 Jun 2008)
New Revision: 5298

Added:
   trunk/numpy/doc/cython/c_numpy.pxd
   trunk/numpy/doc/cython/c_python.pxd
Removed:
   trunk/numpy/doc/cython/Python.pxi
   trunk/numpy/doc/cython/numpy.pxi
Modified:
   trunk/numpy/doc/cython/Makefile
   trunk/numpy/doc/cython/numpyx.pyx
Log:
Updated Cython code to use .pxd files with cimport instead of .pxi/include.

Using cimport/pxd is the currently recommended approach by the Cython
team.


Modified: trunk/numpy/doc/cython/Makefile
===================================================================
--- trunk/numpy/doc/cython/Makefile	2008-06-18 22:55:30 UTC (rev 5297)
+++ trunk/numpy/doc/cython/Makefile	2008-06-19 06:16:48 UTC (rev 5298)
@@ -24,7 +24,7 @@
 
 numpyx.pyx.html: numpyx.pyx
 	cython -a numpyx.pyx
-	@echo "Annotated HTML of the C code generated in numpy.pyx.html"
+	@echo "Annotated HTML of the C code generated in numpyx.html"
 
 # Phony targets for cleanup and similar uses
 

Deleted: trunk/numpy/doc/cython/Python.pxi
===================================================================
--- trunk/numpy/doc/cython/Python.pxi	2008-06-18 22:55:30 UTC (rev 5297)
+++ trunk/numpy/doc/cython/Python.pxi	2008-06-19 06:16:48 UTC (rev 5298)
@@ -1,62 +0,0 @@
-# :Author:    Robert Kern
-# :Copyright: 2004, Enthought, Inc.
-# :License:   BSD Style
-
-
-cdef extern from "Python.h":
-    # Not part of the Python API, but we might as well define it here.
-    # Note that the exact type doesn't actually matter for Pyrex.
-    ctypedef int size_t
-
-    # Some type declarations we need
-    ctypedef int Py_intptr_t
-
-
-    # String API
-    char* PyString_AsString(object string)
-    char* PyString_AS_STRING(object string)
-    object PyString_FromString(char* c_string)
-    object PyString_FromStringAndSize(char* c_string, int length)
-    object PyString_InternFromString(char *v)
-
-    # Float API
-    object PyFloat_FromDouble(double v)
-    double PyFloat_AsDouble(object ob)
-    long PyInt_AsLong(object ob)
-
-
-    # Memory API
-    void* PyMem_Malloc(size_t n)
-    void* PyMem_Realloc(void* buf, size_t n)
-    void PyMem_Free(void* buf)
-
-    void Py_DECREF(object obj)
-    void Py_XDECREF(object obj)
-    void Py_INCREF(object obj)
-    void Py_XINCREF(object obj)
-
-    # CObject API
-    ctypedef void (*destructor1)(void* cobj)
-    ctypedef void (*destructor2)(void* cobj, void* desc)
-    int PyCObject_Check(object p)
-    object PyCObject_FromVoidPtr(void* cobj, destructor1 destr)
-    object PyCObject_FromVoidPtrAndDesc(void* cobj, void* desc, 
-        destructor2 destr)
-    void* PyCObject_AsVoidPtr(object self)
-    void* PyCObject_GetDesc(object self)
-    int PyCObject_SetVoidPtr(object self, void* cobj)  
-
-    # TypeCheck API
-    int PyFloat_Check(object obj)
-    int PyInt_Check(object obj)
-
-    # Error API
-    int PyErr_Occurred()
-    void PyErr_Clear()
-    int  PyErr_CheckSignals()
-
-cdef extern from "string.h":
-    void *memcpy(void *s1, void *s2, int n)
-
-cdef extern from "math.h":
-    double fabs(double x)

Copied: trunk/numpy/doc/cython/c_numpy.pxd (from rev 5297, trunk/numpy/doc/cython/numpy.pxi)

Copied: trunk/numpy/doc/cython/c_python.pxd (from rev 5297, trunk/numpy/doc/cython/Python.pxi)

Deleted: trunk/numpy/doc/cython/numpy.pxi
===================================================================
--- trunk/numpy/doc/cython/numpy.pxi	2008-06-18 22:55:30 UTC (rev 5297)
+++ trunk/numpy/doc/cython/numpy.pxi	2008-06-19 06:16:48 UTC (rev 5298)
@@ -1,133 +0,0 @@
-# :Author:    Travis Oliphant
-
-cdef extern from "numpy/arrayobject.h":
-
-    cdef enum NPY_TYPES:
-        NPY_BOOL
-        NPY_BYTE
-        NPY_UBYTE
-        NPY_SHORT
-        NPY_USHORT 
-        NPY_INT
-        NPY_UINT 
-        NPY_LONG
-        NPY_ULONG
-        NPY_LONGLONG
-        NPY_ULONGLONG
-        NPY_FLOAT
-        NPY_DOUBLE 
-        NPY_LONGDOUBLE
-        NPY_CFLOAT
-        NPY_CDOUBLE
-        NPY_CLONGDOUBLE
-        NPY_OBJECT
-        NPY_STRING
-        NPY_UNICODE
-        NPY_VOID
-        NPY_NTYPES
-        NPY_NOTYPE
-
-    cdef enum requirements:
-        NPY_CONTIGUOUS
-        NPY_FORTRAN
-        NPY_OWNDATA
-        NPY_FORCECAST
-        NPY_ENSURECOPY
-        NPY_ENSUREARRAY
-        NPY_ELEMENTSTRIDES
-        NPY_ALIGNED
-        NPY_NOTSWAPPED
-        NPY_WRITEABLE
-        NPY_UPDATEIFCOPY
-        NPY_ARR_HAS_DESCR
-
-        NPY_BEHAVED
-        NPY_BEHAVED_NS
-        NPY_CARRAY
-        NPY_CARRAY_RO
-        NPY_FARRAY
-        NPY_FARRAY_RO
-        NPY_DEFAULT
-
-        NPY_IN_ARRAY
-        NPY_OUT_ARRAY
-        NPY_INOUT_ARRAY
-        NPY_IN_FARRAY
-        NPY_OUT_FARRAY
-        NPY_INOUT_FARRAY
-
-        NPY_UPDATE_ALL 
-
-    cdef enum defines:
-        NPY_MAXDIMS
-
-    ctypedef struct npy_cdouble:
-        double real
-        double imag
-
-    ctypedef struct npy_cfloat:
-        double real
-        double imag
-
-    ctypedef int npy_intp 
-
-    ctypedef extern class numpy.dtype [object PyArray_Descr]:
-        cdef int type_num, elsize, alignment
-        cdef char type, kind, byteorder, hasobject
-        cdef object fields, typeobj
-
-    ctypedef extern class numpy.ndarray [object PyArrayObject]:
-        cdef char *data
-        cdef int nd
-        cdef npy_intp *dimensions
-        cdef npy_intp *strides
-        cdef object base
-        cdef dtype descr
-        cdef int flags
-
-    ctypedef extern class numpy.flatiter [object PyArrayIterObject]:
-        cdef int  nd_m1
-        cdef npy_intp index, size
-        cdef ndarray ao
-        cdef char *dataptr
-        
-    ctypedef extern class numpy.broadcast [object PyArrayMultiIterObject]:
-        cdef int numiter
-        cdef npy_intp size, index
-        cdef int nd
-        cdef npy_intp *dimensions
-        cdef void **iters
-
-    object PyArray_ZEROS(int ndims, npy_intp* dims, NPY_TYPES type_num, int fortran)
-    object PyArray_EMPTY(int ndims, npy_intp* dims, NPY_TYPES type_num, int fortran)
-    dtype PyArray_DescrFromTypeNum(NPY_TYPES type_num)
-    object PyArray_SimpleNew(int ndims, npy_intp* dims, NPY_TYPES type_num)
-    int PyArray_Check(object obj)
-    object PyArray_ContiguousFromAny(object obj, NPY_TYPES type, 
-        int mindim, int maxdim)
-    object PyArray_ContiguousFromObject(object obj, NPY_TYPES type, 
-        int mindim, int maxdim)
-    npy_intp PyArray_SIZE(ndarray arr)
-    npy_intp PyArray_NBYTES(ndarray arr)
-    void *PyArray_DATA(ndarray arr)
-    object PyArray_FromAny(object obj, dtype newtype, int mindim, int maxdim,
-		    int requirements, object context)
-    object PyArray_FROMANY(object obj, NPY_TYPES type_num, int min,
-                           int max, int requirements)
-    object PyArray_NewFromDescr(object subtype, dtype newtype, int nd,
-                                npy_intp* dims, npy_intp* strides, void* data,
-                                int flags, object parent)
-
-    object PyArray_FROM_OTF(object obj, NPY_TYPES type, int flags)
-    object PyArray_EnsureArray(object)
-
-    object PyArray_MultiIterNew(int n, ...)
-
-    char *PyArray_MultiIter_DATA(broadcast multi, int i)
-    void PyArray_MultiIter_NEXTi(broadcast multi, int i) 
-    void PyArray_MultiIter_NEXT(broadcast multi)
-
-    object PyArray_IterNew(object arr)
-    void PyArray_ITER_NEXT(flatiter it)
-
-    void import_array()

Modified: trunk/numpy/doc/cython/numpyx.pyx
===================================================================
--- trunk/numpy/doc/cython/numpyx.pyx	2008-06-18 22:55:30 UTC (rev 5297)
+++ trunk/numpy/doc/cython/numpyx.pyx	2008-06-19 06:16:48 UTC (rev 5298)
@@ -2,21 +2,21 @@
 """Cython access to Numpy arrays - simple example.
 """
 
-# Includes from the python headers
-include "Python.pxi"
-# Include the Numpy C API for use via Cython extension code
-include "numpy.pxi"
+# Import the pieces of the Python C API we need to use (from c_python.pxd):
+cimport c_python as py
 
+# Import the NumPy C API (from c_numpy.pxd)
+cimport c_numpy as cnp
+
 ################################################
 # Initialize numpy - this MUST be done before any other code is executed.
-import_array()
+cnp.import_array()
 
-# Import the Numpy module for access to its usual Python API
+# Import the NumPy module for access to its usual Python API
 import numpy as np
 
-
 # A 'def' function is visible in the Python-imported module
-def print_array_info(ndarray arr):
+def print_array_info(cnp.ndarray arr):
     """Simple information printer about an array.
 
     Code meant to illustrate Cython/NumPy integration only."""
@@ -24,19 +24,19 @@
     cdef int i
 
     print '-='*10
-    # Note: the double cast here (void * first, then Py_intptr_t) is needed in
-    # Cython but not in Pyrex, since the casting behavior of cython is slightly
-    # different (and generally safer) than that of Pyrex.  In this case, we
-    # just want the memory address of the actual Array object, so we cast it to
-    # void before doing the Py_intptr_t cast:
+    # Note: the double cast here (void * first, then py.Py_intptr_t) is needed
+    # in Cython but not in Pyrex, since the casting behavior of cython is
+    # slightly different (and generally safer) than that of Pyrex.  In this
+    # case, we just want the memory address of the actual Array object, so we
+    # cast it to void before doing the py.Py_intptr_t cast:
     print 'Printing array info for ndarray at 0x%0lx'% \
-          (<Py_intptr_t><void *>arr,)
+          (<py.Py_intptr_t><void *>arr,)
     print 'number of dimensions:',arr.nd
-    print 'address of strides: 0x%0lx'%(<Py_intptr_t>arr.strides,)
+    print 'address of strides: 0x%0lx'%(<py.Py_intptr_t>arr.strides,)
     print 'strides:'
     for i from 0<=i<arr.nd:
         # print each stride
-        print '  stride %d:'%i,<Py_intptr_t>arr.strides[i]
+        print '  stride %d:'%i,<py.Py_intptr_t>arr.strides[i]
     print 'memory dump:'
     print_elements( arr.data, arr.strides, arr.dimensions,
                     arr.nd, sizeof(double), arr.dtype )
@@ -46,12 +46,12 @@
 # A 'cdef' function is NOT visible to the python side, but it is accessible to
 # the rest of this Cython module
 cdef print_elements(char *data,
-                    Py_intptr_t* strides,
-                    Py_intptr_t* dimensions,
+                    py.Py_intptr_t* strides,
+                    py.Py_intptr_t* dimensions,
                     int nd,
                     int elsize,
                     object dtype):
-    cdef Py_intptr_t i,j
+    cdef py.Py_intptr_t i,j
     cdef void* elptr
 
     if dtype not in [np.dtype(np.object_),
@@ -78,7 +78,7 @@
             print_elements(data, strides+1, dimensions+1, nd-1, elsize, dtype)
             data = data + strides[0]
 
-def test_methods(ndarray arr):
+def test_methods(cnp.ndarray arr):
     """Test a few attribute accesses for an array.
     
     This illustrates how the pyrex-visible object is in practice a strange



More information about the Numpy-svn mailing list