Passing back an array from an extension module?

Chris Barker chrishbarker at home.net
Thu Jun 14 16:17:11 EDT 2001


Bob Greschke wrote:


> All right, stop twisting my arm. :-)  I'll give it a try.  Actually,
> the initial reading of the file in C goes very quickly.

I didn't mean to be that forcefull!

> I can run everything on the same file and it eventually crashes on
> about the 6th or 7th reading.  I am free()ing up the C array, but I
> suspect that I have to do something with the reference count of the
> tuple that I create.  Maybe?  I'm still not clear on all of that.

Check your memory use after each reading to make sure this is what's
casueing your crash. I don't remember what OS you are running this on,
but on Linux, I can have a massive memory leak, and the process will get
large enough to swap like crazy and slow the system to crawl, but it
still doesn't crash. You may have another problem.

It is confusing to keep track of when you need to DECREF. It sounds like
this should not be required, as you are passing it back to Python. It
should get DECREFed by Python when it is no longer referenced there.

What I sometimes do is create my Arrays in Python and pass them into the
extension to be populated. That way you don't have to deal with the
reference counting at all.

> I'll still need to call the C stuff to read, and decode, the file.

How complex is the decoding? You might be surprised what you can do with
NumPy. Of course, if the C code is allready written,this should be easy!

Good luck,

-Chris

-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------



More information about the Python-list mailing list