[Numpy-discussion] newbie for writing numpy/scipy extensions
Travis Oliphant
oliphant at ee.byu.edu
Thu Apr 27 11:36:06 EDT 2006
Gennan Chen wrote:
> Hi! All,
>
> I just start writing my own python extension based on numpy. Couple
> of questions here:
>
> 1. I have some utility functions, such as wrappers for
> PyArray_GETPTR* needed be access by different extension modules. So,
> I put them in utlis.h and utlis.c. In utils.h, I need to include
> "numpy/arrayobject.h". But the compilation failed when I include it
> again in my extension module function, wrap.c:
>
> #include "numpy/arrayobject.h"
> #include "utils.h"
>
> When I remove it and use
>
> #include "utils.h"
>
> the compilation works. So, is it true that I can only include
> arrayobject.h once?
No, you can include arrayobject.h more than once. However, if you make
use of C-API functions (not just macros that access elements of the
array) in more than one file for the same extension module, you need to
do a couple of things to make it work.
In the original file you must define PY_ARRAY_UNIQUE_SYMBOL to something
unique to your extension module before you include the arrayobject.h file.
In the helper c file you must define PY_ARRAY_UNIQUE_SYMBOL and define
NO_IMPORT_ARRAY prior to including the arrayobject.h
Thus, in wrap.c you do (feel free to change the name from
_chen_extension to something else)
#define PY_ARRAY_UNIQUE_SYMBOL _chen_extension
#include "numpy/arrayobject.h"
and in
utils.c you do
#define PY_ARRAY_UNIQUE_SYMBOL _chen_extension
#define NO_IMPORT_ARRAY
#include "numpy/arrayobject.h"
>
> 2. which import I should use in my initial function:
>
> import_array()
import_array()
-Travis
More information about the NumPy-Discussion
mailing list