Bug 29582 - Passing file objects to pycairo results in blank file on Windows
Summary: Passing file objects to pycairo results in blank file on Windows
Status: RESOLVED FIXED
Alias: None
Product: pycairo
Classification: Unclassified
Component: general (show other bugs)
Version: unspecified
Hardware: Other Windows (All)
: medium major
Assignee: Steve Chaplin
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-08-15 03:28 UTC by rei4dan
Modified: 2010-11-01 04:15 UTC (History)
0 users

See Also:
i915 platform:
i915 features:


Attachments
Example script demonstrating the issue with file objects (1.18 KB, text/x-python)
2010-08-15 03:28 UTC, rei4dan
Details
Example output file on Windows (3.10 KB, application/pdf)
2010-08-15 03:34 UTC, rei4dan
Details
Example output file on Linux (2.92 KB, application/pdf)
2010-08-15 03:35 UTC, rei4dan
Details
segmentation fault under Linux (1.20 KB, text/x-python)
2010-09-13 14:50 UTC, rei4dan
Details

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.