Hi all,

after I updated the pyrex code to use cython, a bit of confusion led
me to using .pxi includes instead of .pxd files and cimport.  That
turned out to be mistaken, as later clarified by the Cython team:

http://codespeak.net/pipermail/cython-dev/2008-April/000628.html

Would anyone object to my fixing the code to the new convention?  The
changes are minimal, the .pxi files can just be renamed and then a few
trivial modifications are done.  But I figured since this gets shipped
as canonical example code, vetting from the core developers is a good
idea.

In particular, I used:

# 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

# Import the NumPy module for access to its usual Python API
import numpy as np

I don't know if those are the conventions people want to use.  I went
with 'cnp' for symmetry, and plain 'py' for simplicity.

The patch is otherwise an inconsequential adding of those prefixes,
since the old include mechanism was equivalent to an 'import *' while
this gives us a namespace.  I've attached the diff for the numpyx file
here for review (the actual diff is lager because of the renames, but
there's no code changes there).

Stefan, core team?

Cheers,

f
Index: numpyx.pyx
===================================================================
--- numpyx.pyx	(revision 5297)
+++ numpyx.pyx	(working copy)
@@ -3,20 +3,22 @@
 """
 
 # Includes from the python headers
-include "Python.pxi"
+#include "Python.pxi"
+cimport c_python as py
+
 # Include the Numpy C API for use via Cython extension code
-include "numpy.pxi"
+#include "numpy.pxi"
+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 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 +26,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 +48,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 +80,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
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to