Bug 29582

Summary: Passing file objects to pycairo results in blank file on Windows
Product: pycairo Reporter: rei4dan
Component: generalAssignee: Steve Chaplin <d74n5pohf9>
Status: RESOLVED FIXED QA Contact:
Severity: major    
Priority: medium    
Version: unspecified   
Hardware: Other   
OS: Windows (All)   
Whiteboard:
i915 platform: i915 features:
Attachments: Example script demonstrating the issue with file objects
Example output file on Windows
Example output file on Linux
segmentation fault under Linux

Description rei4dan 2010-08-15 03:28:29 UTC
Created attachment 37883 [details]
Example script demonstrating the issue with file objects

Documentation of pycairo says that PDFSurface accepts as an argument a string or writable file object, but with file objects it doesn't work in all cases. The same is the case for write_to_png(). It works on Linux, but on Windows it doesn't always work.
I get blank PDF page on Windows if I pass file object, but the file is not empty.
If I pass filename as string then it works.
If I pass filename as Python Unicode string on Linux, I get segmentation fault.. the string has to be encoded.

I'm using it like this:
f = open(filename, 'w')
surf = cairo.PDFSurface(f, w, h)
...

I have installed pycairo 1.8.6 for Python 2.6 (from ftp.gnome.org) and using Cairo DLLs from the gtk+ bundle 2.20:
http://www.gtk.org/download-windows.html

The same thing happens on XP and Vista.
Comment 1 rei4dan 2010-08-15 03:34:10 UTC
Created attachment 37884 [details]
Example output file on Windows
Comment 2 rei4dan 2010-08-15 03:35:02 UTC
Created attachment 37885 [details]
Example output file on Linux
Comment 3 Steve Chaplin 2010-09-13 02:03:57 UTC
pycairo 1.8.10 is the current version - try this version and see if the problems are still there.

Could you provide a test case for the Linux segmentation fault.

For PDFSurface, write_to_png() (and nearly all other functions) pycairo uses the same code for Linux and Windows. So if something works in Linux and not Windows its unlikely to be a problem in pycairo, and more likely a problem in the specific Windows code in the cairo C library.

Try
 surface.flush()
 surface.finish()
 f.close()
in that order (flush before finish) - does that work?
Which combinations of the 3 work, and do not work?
Comment 4 rei4dan 2010-09-13 14:50:57 UTC
Created attachment 38680 [details]
segmentation fault under Linux
Comment 5 rei4dan 2010-09-13 14:59:39 UTC
(In reply to comment #3)
> pycairo 1.8.10 is the current version - try this version and see if the
> problems are still there.

I'll try to build and test that, but building it on Win is a pain.
 
> Could you provide a test case for the Linux segmentation fault.

This problem is still present in 1.8.10.

> For PDFSurface, write_to_png() (and nearly all other functions) pycairo uses
> the same code for Linux and Windows. So if something works in Linux and not
> Windows its unlikely to be a problem in pycairo, and more likely a problem in
> the specific Windows code in the cairo C library.
> 
> Try
>  surface.flush()
>  surface.finish()
>  f.close()
> in that order (flush before finish) - does that work?
> Which combinations of the 3 work, and do not work?

None of the combinations work.
Comment 6 Steve Chaplin 2010-09-14 05:42:40 UTC
The problem with unicode filenames has been fixed, and changes applied to the git repo.

For the Windows problem, use
  f = open("example.pdf","wb")  # write in binary mode

Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.