Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSError: [Errno cairo returned CAIRO_STATUS_WRITE_ERROR: b'error while writing to output stream'] 11 #143

Closed
brendabell opened this issue Dec 26, 2019 · 16 comments

Comments

@brendabell
Copy link

brendabell commented Dec 26, 2019

Python 3.7.4
CairoSVG 2.4.2
Mac OS X 10.14.6

Input file: https://gist.github.com/brendabell/408c12703d92868ed73bcdf04cf92ec7

Note that I did include absolute height, width and viewBox. My code is as follows:

result = generator.generate()
text_file = open("{}.svg".format(machine), "w")
text_file.write(result)
text_file.close()
png_file = open("{}.png".format(machine), "w")
cairosvg.svg2png(bytestring=result,write_to=png_file)
png_file.close()

The generated SVG file seems to be perfect. I started getting the error after the following upgrades:

Python 2.7.16 -> 3.7.4
CairoSVG 1.0.22 -> 2.4.2

@liZe
Copy link
Member

liZe commented Dec 26, 2019

It works for me. This error happens when you cannot write to your destination file, you should check the rights set on your destination folder (and your destination file if it already exists).

@brendabell
Copy link
Author

brendabell commented Dec 26, 2019

It works for me. This error happens when you cannot write to your destination file, you should check the rights set on your destination folder (and your destination file if it already exists).

I've confirmed the destination file does not exist. I also know it's not a permissions issue because my script is able to first create the SVG. It also creates the PNG, but the cairo write fails and I'm left with a zero-byte file.

Here's my pip freeze output. Can you verify whether you're using the same or different versions of something?

cairocffi==1.1.0
CairoSVG==2.4.2
certifi==2019.11.28
cffi==1.13.2
cssselect2==0.2.2
defusedxml==0.6.0
Pillow==6.2.1
pipenv==2018.11.26
pycparser==2.19
pyparsing==2.4.6
svgwrite==1.3.1
tinycss2==1.0.2
virtualenv==16.7.9
virtualenv-clone==0.5.3
webencodings==0.5.1

@brendabell
Copy link
Author

brendabell commented Dec 26, 2019

It works for me. This error happens when you cannot write to your destination file, you should check the rights set on your destination folder (and your destination file if it already exists).

I just found a work-around. Instead of this:

cairosvg.svg2png(file_obj=text_file,write_to=png_file)

I now have this which works:

png_content = cairosvg.svg2png(file_obj=text_file)
png_file.write(png_content)

I think this is further evidence that it's not a permissions problem.

@liZe
Copy link
Member

liZe commented Dec 26, 2019

Well… It may be related to #105 / #106, do you use a hardened system?

@L3o-pold
Copy link

L3o-pold commented Jan 9, 2020

With this kind of code it fails on a python3.7 docker image

png_content = cairosvg.svg2png(url="http://leopez.ch/glyphicons-halflings-regular.svg", dpi=96)
tmp_png.name.write(png_content)
  File "/usr/local/lib/python3.7/site-packages/cairosvg/__init__.py", line 70, in svg2png


    output_height=output_height)


  File "/usr/local/lib/python3.7/site-packages/cairosvg/surface.py", line 149, in convert


    instance.finish()


  File "/usr/local/lib/python3.7/site-packages/cairosvg/surface.py", line 499, in finish


    self.cairo.write_to_png(self.output)


  File "/usr/local/lib/python3.7/site-packages/cairocffi/surfaces.py", line 648, in write_to_png


    self._pointer, write_func, ffi.NULL))


  File "/usr/local/lib/python3.7/site-packages/cairocffi/__init__.py", line 88, in _check_status


    raise exception(message, status)


OSError: [Errno cairo returned CAIRO_STATUS_WRITE_ERROR: b'error while writing to output stream'] 11

@liZe
Copy link
Member

liZe commented Jan 9, 2020

@L3o-pold Do you use a hardened system?

@L3o-pold
Copy link

L3o-pold commented Jan 9, 2020

@liZe what do you mean by hardened?

@liZe
Copy link
Member

liZe commented Jan 9, 2020

@liZe what do you mean by hardened?

For example using SELinux.

@brendabell
Copy link
Author

Well… It may be related to #105 / #106, do you use a hardened system?

Unless Apple has done something really weird I don't know about in OS X 10.14.6, no.

@liZe
Copy link
Member

liZe commented Jan 21, 2020

@L3o-pold Your problem is different from @brendabell’s: you have to set a size in your SVG file (see #49).

(CairoSVG should check that and raise an explicit error.)

@L3o-pold
Copy link

Thanks for the answer. I have not the hand on the files.

@liZe
Copy link
Member

liZe commented Jan 21, 2020

Thanks for the answer. I have not the hand on the files.

http://leopez.ch/glyphicons-halflings-regular.svg is an SVG font, that’s why it has no size. It can’t be rendered by a SVG renderer (you can try to open it in your browser).

@L3o-pold
Copy link

@liZe yes I agree, maybe we should have a more adapted error message.

@liZe
Copy link
Member

liZe commented Jan 23, 2020

Unless Apple has done something really weird I don't know about in OS X 10.14.6, no.

I didn’t know, but macOS is also impacted by this problem, according to the cffi documentation. This bug is for sure a duplicate of #106, I’ll change its title to include macOS systems.

@liZe yes I agree, maybe we should have a more adapted error message.

Done in CairoSVG’s f4aa9e5 commit.

@liZe liZe closed this as completed Jan 23, 2020
liZe added a commit to Kozea/CairoSVG that referenced this issue Jan 23, 2020
@shibababa
Copy link

Code mistake
×png_file = open("{}.png".format(machine), "w")
○png_file = open("{}.png".format(machine), "wb")

netbsd-srcmastr referenced this issue in NetBSD/pkgsrc Apr 30, 2024
Version 1.7.0
.............

Released on 2024-04-27

* Drop Python 3.7 support, add Python 3.12 support
* `#221 <https://github.com/Kozea/cairocffi/pull/225>`_:
  Add environment variable to set folder where DLLs are installed on Windows
* `#225 <https://github.com/Kozea/cairocffi/pull/225>`_:
  Use Ruff instead of Flake8 and isort


Version 1.6.1
.............

Released on 2023-07-24

* `#217 <https://github.com/Kozea/cairocffi/issues/217>`_:
  Repair installation with PyInstaller


Version 1.6.0
.............

Released on 2023-06-12

**This version uses a new CFFI mode that may break your program.**

CairoCFFI now uses Flit for packaging and is also distributed as a Python
wheel.

Please test carefully and don’t hesitate to report issues before using it in
production.

* `#216 <https://github.com/Kozea/cairocffi/pull/216>`_:
  Use ABI-level in-line CFFI mode


Version 1.5.1
.............

Released on 2023-04-15

* `#212 <https://github.com/Kozea/cairocffi/issues/212>`_:
  Bring back XCB support during wheel generation


Version 1.5.0
.............

Released on 2023-03-17

* `#106 <https://github.com/Kozea/cairocffi/issues/106>`_,
  `#200 <https://github.com/Kozea/cairocffi/issues/200>`_:
  Fallback to manual PNG file creation on hardened systems
* `#210 <https://github.com/Kozea/cairocffi/pull/210>`_:
  Use pyproject.toml for packaging and remove other useless files


Version 1.4.0
.............

Released on 2022-09-23

* `#205 <https://github.com/Kozea/cairocffi/pull/205>`_:
  Use pikepdf to parse generated PDF
* `#171 <https://github.com/Kozea/cairocffi/pull/171>`_:
  Don’t use deprecated pytest-runner anymore


Version 1.3.0
.............

Released on 2021-10-04

* `2cd512d <https://github.com/Kozea/cairocffi/commit/2cd512d>`_:
  Drop Python 3.6 support
* `#196 <https://github.com/Kozea/cairocffi/pull/196>`_:
  Fix import `constants.py` import
* `#169 <https://github.com/Kozea/cairocffi/pull/169>`_:
  Add extra library name "cairo-2.dll"
* `#178 <https://github.com/Kozea/cairocffi/pull/178>`_:
  Workaround for testing date string with cairo 1.17.4
* `#186 <https://github.com/Kozea/cairocffi/pull/186>`_:
  Fix link in documentation
* `#195 <https://github.com/Kozea/cairocffi/pull/195>`_:
  Fix typo in documentation
* `#184 <https://github.com/Kozea/cairocffi/pull/184>`_,
  `a4fc2a7 <https://github.com/Kozea/cairocffi/commit/a4fc2a7>`_:
  Clean .gitignore


Version 1.2.0
.............

Released on 2020-10-29

* `#152 <https://github.com/Kozea/cairocffi/pull/152>`_:
  Add NumPy support
* `#143 <https://github.com/Kozea/cairocffi/issues/143>`_:
  Make write_to_png function work on hardened systems
* `#156 <https://github.com/Kozea/cairocffi/pull/156>`_:
  Use major version name to open shared libraries
* `#165 <https://github.com/Kozea/cairocffi/pull/165>`_:
  Don’t list setuptools as required for installation


Version 1.1.0
.............

Released on 2019-09-05

* `#135 <https://github.com/Kozea/cairocffi/pull/135>`_,
  `#127 <https://github.com/Kozea/cairocffi/pull/127>`_,
  `#119 <https://github.com/Kozea/cairocffi/pull/119>`_:
  Clean the way external libraries are found
* `#126 <https://github.com/Kozea/cairocffi/pull/126>`_:
  Remove const char* elements from cdef
* Support Cairo features up to 1.17.2
* Fix documentation generation


Version 1.0.2
.............

Released on 2019-02-15

* `#123 <https://github.com/Kozea/cairocffi/issues/123>`_:
  Rely on a recent version of setuptools to handle VERSION


Version 1.0.1
.............

Released on 2019-02-12

* `#120 <https://github.com/Kozea/cairocffi/issues/120>`_:
  Don't delete _generated modules on ffi_build import


Version 1.0.0
.............

Released on 2019-02-08

6 years after its first release, cairocffi can now be considered as stable.

* Drop Python 2.6, 2.7 and 3.4 support
* Test with Python 3.7
* Clean code, tests and packaging
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants