Skip to content

FIX: Catch IOError on font-cache write #9676

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
Nov 4, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,8 +964,10 @@ def json_dump(data, filename):
Handles FontManager and its fields."""

with open(filename, 'w') as fh:
json.dump(data, fh, cls=JSONEncoder, indent=2)

try:
json.dump(data, fh, cls=JSONEncoder, indent=2)
except IOError as e:
warnings.warn('Could not save font_manager cache ', e)
Copy link

Choose a reason for hiding this comment

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

Guys, this use of warnings.warn does not seem to match the API specification:

https://docs.python.org/2/library/warnings.html#warnings.warn

I was just hit by this problem:

Traceback (most recent call last):
File "/my/python/file.py", line 15, in <module>
    from matplotlib import pyplot as plt
File "/usr/lib64/python2.7/site-packages/matplotlib/pyplot.py", line 31, in <module>
    import matplotlib.colorbar
File "/usr/lib64/python2.7/site-packages/matplotlib/colorbar.py", line 36, in <module>
    import matplotlib.contour as contour
File "/usr/lib64/python2.7/site-packages/matplotlib/contour.py", line 20, in <module>
    import matplotlib.font_manager as font_manager
File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 1469, in <module>
    _rebuild()
File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 1454, in _rebuild
    json_dump(fontManager, _fmcache)
File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 979, in json_dump
    warnings.warn('Could not save font_manager cache ', e)
TypeError: issubclass() arg 1 must be a class

Copy link

Choose a reason for hiding this comment

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

It doesn't seem to match the py3 version as well:

https://docs.python.org/3/library/warnings.html#warnings.warn

The function's 2nd argument takes an exception class, not an exception instance.

Copy link

Choose a reason for hiding this comment

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

@jklymak Can you take a look at this? If you want me to send a patch, no problem.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ooops, well, at least its not worse than it was. A PR would be great, or if you aren't comfortable I could add one. Its pretty hard to test unless you get this error somehow.

Copy link
Member Author

Choose a reason for hiding this comment

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

Belay that - this code is not in master anymore, so it'll be fixed next release. Sorry about the inconveninece in this release.

Copy link
Member

@QuLogic QuLogic Mar 21, 2018

Choose a reason for hiding this comment

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

It's not in master, but it is in 2.2.2, so that may not be the next release. Though I suppose it technically might not count as a regression.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I guess we could backport a fix onto 2.2.3 if there is one.

Copy link
Member

Choose a reason for hiding this comment

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

There will definitely be 2.2.3, 2.2 is going to keep getting bug-fix releases until 2020.

Copy link

Choose a reason for hiding this comment

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

It'd be no problem for me to send a PR. It's just that, at the time, I had a lot of other things to do so I just commented out here. Since you guys already have the repositories cloned and everything, it was the quicker solution.

Next time I'll create a PR. Thanks!


def json_load(filename):
"""Loads a data structure as JSON from the named file.
Expand Down