Skip to content

Fix saving PNGs to file objects in some places#16709

Merged
timhoffm merged 2 commits intomatplotlib:v3.2.xfrom
QuLogic:fix-png-write-io
Mar 8, 2020
Merged

Fix saving PNGs to file objects in some places#16709
timhoffm merged 2 commits intomatplotlib:v3.2.xfrom
QuLogic:fix-png-write-io

Conversation

@QuLogic
Copy link
Copy Markdown
Member

@QuLogic QuLogic commented Mar 8, 2020

PR Summary

The IPython automated rebuild started failing when Matplotlib 3.2.0 was introduced, with stuff like:

  File "/builddir/build/BUILDROOT/ipython-7.13.0-1.fc33.noarch/usr/lib/python3.8/site-packages/IPython/lib/tests/test_latextools.py", line 64, in test_latex_to_html
    img = latextools.latex_to_html("$x^2$")
  File "/builddir/build/BUILDROOT/ipython-7.13.0-1.fc33.noarch/usr/lib/python3.8/site-packages/IPython/lib/latextools.py", line 216, in latex_to_html
    base64_data = latex_to_png(s, encode=True).decode('ascii')
  File "/builddir/build/BUILDROOT/ipython-7.13.0-1.fc33.noarch/usr/lib/python3.8/site-packages/IPython/lib/latextools.py", line 104, in latex_to_png
    bin_data = f(s, wrap, color, scale)
  File "/builddir/build/BUILDROOT/ipython-7.13.0-1.fc33.noarch/usr/lib/python3.8/site-packages/IPython/lib/latextools.py", line 126, in latex_to_png_mpl
    mt.to_png(f, s, fontsize=12, dpi=dpi, color=color)
  File "/usr/lib64/python3.8/site-packages/matplotlib/mathtext.py", line 3436, in to_png
    with open(filename, "wb") as file:
TypeError: expected str, bytes or os.PathLike object, not _io.BytesIO

MathText.to_png is documented as accepting a file-like object though, and this was dropped accidentally as the _png module was refactored, so this PR fixes that. I also found that _ImageBase.write_png was changed in a similar way in the same commit, so I also modified it. But I couldn't find any usage of it and its parameters are undocumented, so I'm not sure whether that was necessary.

This PR targets v3.2.x directly, because master was subsequently switched to direct Pillow calls that accept file-like objects themselves.

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • [N/A] New features are documented, with examples if plot related
  • [N/A] Documentation is sphinx and numpydoc compliant
  • [N/A] Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • [N/A] Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@QuLogic QuLogic added this to the v3.2.1 milestone Mar 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants