Burlen, Have you actually verified that the first instance of import_array() actually gets called? Because the behavior suggests that it does not.
In PyTrilinos, I wrap the import_array() call inside a C++ singleton, and put that singleton in a dynamic library that all my packages link to, thus insuring that it gets called once and only once. -Bill On Nov 14, 2013, at 11:10 AM, Burlen Loring wrote: > Hi David, > > Yes, that the situation. using your naming convention, in addtion to > foo_wrap.c I have another file , say foo_convert.cxx, for massaging > python data structures into our internal data structures. This source is > compiled with a c++ compiler so that I can use templates to simplify > handling the numerous types a user could throw at us. All of the sources > are linked into the module's .so. I need to use various numpy functions > in my data conversions functions that live in foo_convert.cxx. For > example I'm using PyArray_Check because I need to discerne between numpy > arrays, and python lists and tuples. > > What I'm confused about is that calls to numpy funtions from this file > are segv'ing unless I add another call to import_array() made from > foo_convert.cxx. I'd like to understand why that's necessary, and why > the import_array() call in the module's init section in foo_wrap.c > doesn't have any affect even though it's called first? > > Thanks > Burlen > > On 11/13/2013 10:48 PM, David Froger wrote: >> Hi Burlen, >> >> SWIG will generate a file named for example foo_wrap.c, which will >> contains a call to import_array() inserted by SWIG because of the >> %init %{ >> import_array(); >> %} >> in the SWIG script. >> So in the file foo_wrap.c (which will be compiled to a Python module >> _foo.so), you should be able to used PyArray_Check without segfault. >> Typically, PyArray_Check will be inserted in foo_wrap.c by a typemap, >> for example a typemap from numpy.i . >> >> Do you use PyArray_Check in the foo_wrap.c or in another file? Is >> PyArray_Check in called in another C library, that _foo.so is linked >> with? >> >> David >> >> Quoting Burlen Loring (2013-11-14 02:21:19) >>> Hi, >>> >>> I'd like to add numpy support to an existing code that uses swig. I've >>> changed the source file that has code to convert python lists into >>> native data from c to c++ so I can use templates to handle data >>> conversions. The problem I'm having is a segfault on PyArray_Check >>> called from my c++ source. Looking at things in the debugger the >>> argument passed to it is indeed an intialized python list, my swig file >>> has import_array() in it's init, and I've verified that it is getting >>> called. adding a function in my c++ source to call import_array() a >>> second time prevents the segfault. Could anyone explain why I need the >>> import_array() in both places? If that's the correct way to handle it? >>> >>> Thanks >>> Burlen >>> _______________________________________________ >>> NumPy-Discussion mailing list >>> NumPy-Discussion@scipy.org >>> http://mail.scipy.org/mailman/listinfo/numpy-discussion >> _______________________________________________ >> NumPy-Discussion mailing list >> NumPy-Discussion@scipy.org >> http://mail.scipy.org/mailman/listinfo/numpy-discussion > > _______________________________________________ > NumPy-Discussion mailing list > NumPy-Discussion@scipy.org > http://mail.scipy.org/mailman/listinfo/numpy-discussion ** Bill Spotz ** ** Sandia National Laboratories Voice: (505)845-0170 ** ** P.O. Box 5800 Fax: (505)284-0154 ** ** Albuquerque, NM 87185-0370 Email: wfsp...@sandia.gov ** _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion