Skip to content

Fix saving PNGs to file objects in some places #16709

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

Merged
merged 2 commits into from
Mar 8, 2020

Conversation

QuLogic
Copy link
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