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? 2. which import I should use in my initial function: import_array() or import_libnumarray() Gen-Nan Chen, PhD Chief Scientist Research and Development Group CorTechs Labs Inc (www.cortechs.net) 1020 Prospect St., #304, La Jolla, CA, 92037 Tel: 1-858-459-9700 ext 16 Fax: 1-858-459-9705 Email: gnchen@cortechs.net
Gennan Chen
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?
What is the compiler error message?
2. which import I should use in my initial function:
import_array()
This one. It's the one to use for Numeric, numarray, and numpy.
or import_libnumarray()
This is for numarray, the other Numeric derivative. It pulls in the numarray-specific stuff IIRC. -- |>|\/|< /--------------------------------------------------------------------------\ |David M. Cooke http://arbutus.physics.mcmaster.ca/dmc/ |cookedm@physics.mcmaster.ca
Got it. Looks like ndimage still used the old one. Gen-Nan Chen, PhD Chief Scientist Research and Development Group CorTechs Labs Inc (www.cortechs.net) 1020 Prospect St., #304, La Jolla, CA, 92037 Tel: 1-858-459-9700 ext 16 Fax: 1-858-459-9705 Email: gnchen@cortechs.net On Apr 27, 2006, at 11:31 AM, David M. Cooke wrote:
Gennan Chen
writes: 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?
What is the compiler error message?
2. which import I should use in my initial function:
import_array()
This one. It's the one to use for Numeric, numarray, and numpy.
or import_libnumarray()
This is for numarray, the other Numeric derivative. It pulls in the numarray-specific stuff IIRC.
-- |>|\/|< /--------------------------------------------------------------------- -----\ |David M. Cooke http:// arbutus.physics.mcmaster.ca/dmc/ |cookedm@physics.mcmaster.ca
Hello all I've collected the information from this thread along with links to some recent threads on writing C extensions on the wiki at: http://www.scipy.org/Cookbook/C_Extensions Feel free to contribute! Regards, Albert
-----Original Message----- From: numpy-discussion-admin@lists.sourceforge.net [mailto:numpy- discussion-admin@lists.sourceforge.net] On Behalf Of Gennan Chen Sent: 27 April 2006 21:23 To: David M.Cooke Cc: Numpy-discussion@lists.sourceforge.net Subject: Re: [Numpy-discussion] newbie for writing numpy/scipy extensions
Got it. Looks like ndimage still used the old one.
Gen-Nan Chen, PhD Chief Scientist Research and Development Group CorTechs Labs Inc (www.cortechs.net) 1020 Prospect St., #304, La Jolla, CA, 92037 Tel: 1-858-459-9700 ext 16 Fax: 1-858-459-9705 Email: gnchen@cortechs.net
On Apr 27, 2006, at 11:31 AM, David M. Cooke wrote:
Gennan Chen
writes: 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?
What is the compiler error message?
2. which import I should use in my initial function:
import_array()
This one. It's the one to use for Numeric, numarray, and numpy.
or import_libnumarray()
This is for numarray, the other Numeric derivative. It pulls in the numarray-specific stuff IIRC.
-- |>|\/|< /--------------------------------------------------------------------- -----\ |David M. Cooke http:// arbutus.physics.mcmaster.ca/dmc/ |cookedm@physics.mcmaster.ca
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
Thanks! That solve the problem. May I ask what does those #define really means?? Gen On Apr 27, 2006, at 11:35 AM, Travis Oliphant wrote:
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
participants (4)
-
Albert Strasheim
-
cookedm@physics.mcmaster.ca
-
Gennan Chen
-
Travis Oliphant