On 12/03/2012 08:01 PM, Chris Barker - NOAA Federal wrote: > On Mon, Dec 3, 2012 at 4:16 PM, Nathaniel Smith <n...@pobox.com> wrote: > >> Yeah, this is a general problem with the Python file API, trying to >> hook it up to stdio is not at all an easy thing. A better version of >> this code would skip that altogether like: >> >> cdef void write_to_pyfile(png_structp s, png_bytep data, png_size_t count): >> fobj = <object>png_get_io_ptr(s) >> pydata = PyString_FromStringAndSize(data, count) >> fobj.write(pydata) > Good point -- not at all Cython-specific, but do you need libpng (or > whatever) to write to the file? can you just get a buffer with the > encoded data and write it on the Python side? Particularly if the user > wants to pass in an open file object. This might be a better API for > folks that might want stream an image right through a web app, too. You need to support both: raw C FILE objects for speed, and writing to a Python file-like object for flexibility. The code in master already does this (albeit with PyCXX), and the code on my "No CXX" branch does this as well with Cython. > > As a lot of Python APIs take either a file name or a file-like object, > perhaps it would make sense to push that distinction down to the > Cython level: > -- if it's a filename, open it with raw C
Unfortunately, as stated in detail in my last e-mail, that doesn't work with Unicode paths. > -- if it's a file-like object, have libpng write to a buffer (bytes > object) , and pass that to the file-like object in Python libpng does one better and allows us to stream directly to a callback which can then write to a Python object. This prevents double allocation of memory. > > anyway, not really a Cython issue, but that second object sure would > be easy on Cython.... > Yeah -- once I figured out how to make a real C callback function from Cython, the contents of the callback function itself is pretty easy to write. Mike ------------------------------------------------------------------------------ LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial Remotely access PCs and mobile devices and provide instant support Improve your efficiency, and focus on delivering more value-add services Discover what IT Professionals Know. Rescue delivers http://p.sf.net/sfu/logmein_12329d2d _______________________________________________ Matplotlib-devel mailing list Matplotlib-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-devel