[Cython] [cython-users] pointer problem

Stefan Behnel stefan_ml at behnel.de
Sun Mar 25 09:14:09 CEST 2012


Liguo Kong, 25.03.2012 04:04:
> I am not sure of the proper way to send a patch (never did it before), so I
> am sending patch file to you

Thanks. It's actually better to send short patches to the cython-devel
mailing list (so that the core developers can take a look - note that it's
subscribers-only), but the *best* way to do it is to set up a pull request
on github. You can even edit the file online for that (there's a button for
that when you look at the file), and once that's done, we can discuss the
change online as well. Another advantage of this is that you get your name
written into our source history. :)

There are a couple of problems with your change, though, so I'm resending
it to cython-devel for discussion here. For one, you are using Cython
specific syntax and .pyx files, so this does not belong into the "pure
mode" documentation. Also, there is a typo in your example code - a cimport
does not use the ".pxd" extension in the module name (Cython will add that
automatically when looking for the file).

Most importantly, however, It's not clear to me what you are trying to tell
the reader. Is it how to pass NumPy data into a C function? In that case,
it would belong into the NumPy specific part of the documentation (and your
C function would likely want to know something about the size of the array
your are passing). If the intention is to generally describe how to deal
with C functions that take pointers, it would rather belong into an earlier
part of the tutorial, and there's the "using C libraries" tutorial which
presents these things already.

Could you clarify that a bit?

Stefan


Your original patch for reference:

--- pure.rst	2012-03-13 21:08:19.000000000 -0700
+++ pure.patched.rst	2012-03-24 19:18:25.000000000 -0700
@@ -142,6 +142,30 @@
 arrays as ``cython.int[10]``. A limited attempt is made to emulate these more
 complex types, but only so much can be done from the Python language.

+Since use of pointers in C is ubiquitous, here we give a quick example of how
+to call C functions whose arguments contain pointers. Suppose you have a
+file in C 'C_func_file.c', which contains a function C_func with
+the following header::
+  void C_func(double * CPointer)
+
+where CPointer points a one-dimensional array of size N, whose data you want
+to access through a numpy array.
+
+Now assume that you write a corresponding .pxd file C_func_file.pxd to
+make the function cimport-able. Now you can access it in a :file: `.pyx`
+file :file: `NumpyCPointerExample.pyx`::
+
+    from C_func_file.pxd cimport C_func
+    import cython
+    cimport numpy as np
+
+    def f():
+        np.ndarray[np.double_t, ndim=1, mode="c"] numar = np.zeros((N))
+        C_func(<cython.double *> numar.data)
+        # followed by some manipulations of the numarray array
+        return
+
+
 Decorators
 --------------------------------



More information about the cython-devel mailing list