8000 Simplify _png extension by handling file open/close in Python. by anntzer · Pull Request #15095 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Simplify _png extension by handling file open/close in Python. #15095

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 1 commit into from
Aug 22, 2019

Conversation

anntzer
Copy link
Contributor
@anntzer anntzer commented Aug 21, 2019

Make functions in the (private) _png extension only take file-like
objects as arguments, rather than "filename or file-like". Filenames
were anyways handled by calling the python C-API to open/close the
files, so we may as well just do this from Python where a simple
with open(...): does the job. The public API is not changed.

PR Summary

PR Checklist

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

Make functions in the (private) _png extension only take file-like
objects as arguments, rather than "filename or file-like".  Filenames
were anyways handled by calling the python C-API to open/close the
files, so we may as well just do this from Python where a simple
`with open(...):` does the job.  The public API is not changed.
@@ -639,7 +639,8 @@ def draw_image(self, gc, x, y, im, transform=None):
fname = os.path.splitext(os.path.basename(self.fh.name))[0]
fname_img = "%s-img%d.png" % (fname, self.image_counter)
self.image_counter += 1
_png.write_png(im[::-1], os.path.join(path, fname_img))
with pathlib.Path(path, fname_img).open("wb") as file:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite like assigning to builtin names, but acknowledge that there's already precedence for this in the library.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a builtin name anymore...

Copy link
Member
@jklymak jklymak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly a lot simpler!

@jklymak jklymak merged commit b779492 into matplotlib:master Aug 22, 2019
@jklymak jklymak added this to the v3.2.0 milestone Aug 22, 2019
@anntzer anntzer deleted the pngfile branch August 22, 2019 16:22
@anntzer anntzer mentioned this pull request Aug 22, 2019
6 tasks
@moorepants
Copy link

The public API is not changed.

Even though the public API hasn't changed, i think this does seem to change the error types raised and has broken downstream code in SymPy. See this issue: sympy/sympy#18815

The offending changed lines are:

https://github.com/matplotlib/matplotlib/blame/v3.2.0/lib/matplotlib/mathtext.py#L3436

I haven't determined yet if it is ultimately SymPy's bad design for relying on a specific error but this PR might cause all Jupyter notebook printing for SymPy to break once people install matplotlib 3.2.0.

@tacaswell
Copy link
Member

Can you try with the 3.2.x branch? We just merged #16709 which I think is related.

@QuLogic
Copy link
Member
QuLogic commented Mar 10, 2020

Yes, this is the same bug; see backtrace on linked issue.

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

Successfully merging this pull request may close these issues.

6 participants
0