On 12/03/2012 07:16 PM, Nathaniel Smith wrote:
> On Mon, Dec 3, 2012 at 11:50 PM, Chris Barker - NOAA Federal
> <chris.bar...@noaa.gov> wrote:
>> On Mon, Dec 3, 2012 at 2:21 PM, Nathaniel Smith <n...@pobox.com> wrote:
>>> For the file handle, I would just write
>>>
>>>    cdef FILE *fp = fdopen(file_obj.fileno(), "w")
>>>
>>> and be done with it. This will work with any version of Python etc.
>> yeah, that makes sense -- though what if you want to be able to
>> read_to/write_from a file that is already open, and in the middle of
>> the file somewhere -- would that work?
>>
>> I just posted a question to the Cython list, and indeed, it looks like
>> there is no easy answer to the file issue.
> 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)
>
> cdef void flush_pyfile(png_structp s):
>      # Not sure if this is even needed
>      fobj = <object>png_get_io_ptr(s)
>      fobj.flush()
>
> # in write_png:
> write_png_c(<png_byte*>pix_buffer, width, height,
>    NULL, <void*>file_obj, write_to_pyfile, flush_pyfile, dpi)

This is what my original version already does in the event that the 
file_obj is not a "real" file.  In practice, you need to support both 
methods -- the callback approach is many times slower than writing 
directly to a regular old FILE object, because there is overhead both at 
the libpng and Python level, and there's no way to select a good buffer 
size.

>
> But this is a separate issue :-) (and needs further fiddling to make
> exception handling work).
>
> Or if you're only going to work on real OS-level file objects anyway,
> you might as well just accept a filename as a string and fopen() it
> locally. Having Python do the fopen just makes your life harder for no
> reason.
There's actually a very good reason.  It is difficult to deal with 
Unicode in file paths from C in a portable way.  On Windows, for 
example, if the user's name contains non-ascii characters, you can't 
write to the home directory using fopen, etc.  It's doable with some 
care by using platform-specific C APIs etc., but CPython has already 
done all of the hard work for us, so it's easiest just to leverage that 
by opening the file from Python.

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

Reply via email to