invalid/misconfigured fonts cause the font manager to fail #1601

Closed
jcarrano opened this Issue Dec 16, 2012 · 6 comments

Comments

Projects
None yet
3 participants

System:
Gentoo
matplotlib-1.1.1 and 1.2.0
Lots of fonts installed

Description:
when attempting to use pyplot, matplotlib fails to create a list of fonts.
font.get_weight(), inside afmFontProperty(fpath, font) throws a KeyError. This function is called by createFontList.
This problem might be one of system configuration. However, matplotlib should be able to cope with invalid fonts/configurations.
In the source code for createFontList, I can see ttfFontProperty is surrounded with a try...except block:

try: prop = ttfFontProperty(font)
except: continue

afmFontProperty is not enclosed in a try...except and it is what is causing the error.

In 1.2.0 neither ttfFontProperty, nor afmFontProperty are enclosed in try...except.

Traceback (most recent call last):
  File "/home/juan/itba/audifonos/fft/fft.py", line 8, in <module>
    import matplotlib.pyplot as plt
  File "/home/juan/.py-env/lib/python2.7/site-packages/matplotlib/pyplot.py", line 26, in <module>
    from matplotlib.figure import Figure, figaspect
  File "/home/juan/.py-env/lib/python2.7/site-packages/matplotlib/figure.py", line 34, in <module>
    import matplotlib.colorbar as cbar
  File "/home/juan/.py-env/lib/python2.7/site-packages/matplotlib/colorbar.py", line 29, in <module>
    import matplotlib.collections as collections
  File "/home/juan/.py-env/lib/python2.7/site-packages/matplotlib/collections.py", line 23, in <module>
    import matplotlib.backend_bases as backend_bases
  File "/home/juan/.py-env/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 37, in <module>
    import matplotlib.widgets as widgets
  File "/home/juan/.py-env/lib/python2.7/site-packages/matplotlib/widgets.py", line 17, in <module>
    from lines import Line2D
  File "/home/juan/.py-env/lib/python2.7/site-packages/matplotlib/lines.py", line 25, in <module>
    from matplotlib.font_manager import FontProperties
  File "/home/juan/.py-env/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1325, in <module>
    _rebuild()
  File "/home/juan/.py-env/lib/python2.7/site-packages/matplotlib/font_manager.py", line 1312, in _rebuild
    fontManager = FontManager()
  File "/home/juan/.py-env/lib/python2.7/site-packages/matplotlib/font_manager.py", line 993, in __init__
    self.afmlist = createFontList(self.afmfiles, fontext='afm')
  File "/home/juan/.py-env/lib/python2.7/site-packages/matplotlib/font_manager.py", line 568, in createFontList
    prop = afmFontProperty(fpath, font)
  File "/home/juan/.py-env/lib/python2.7/site-packages/matplotlib/font_manager.py", line 504, in afmFontProperty
    weight = weight_as_number(font.get_weight().lower())
  File "/home/juan/.py-env/lib/python2.7/site-packages/matplotlib/afm.py", line 522, in get_weight
    return self._header[b'Weight']
KeyError: 'Weight'
Member

dmcdougall commented Dec 16, 2012

@jcarrano Thanks for reporting this! One question:

In the source code for createFontList, I can see ttfFontProperty is surrounded with a try...except block:

try: prop = ttfFontProperty(font)
except: continue

I do not see that. I see that neither this nor afmFontProperty are enclosed in a try/except block.

Edit: Grammar.

Member

dmcdougall commented Dec 16, 2012

Aha! Looks like it was removed in ba5610f. I'll patch those up.

dmcdougall was assigned Dec 16, 2012

Owner

mdboom commented Dec 17, 2012

I wonder if the try/except, when reinstated, could mention the exception type by name (KeyError), so as not to silently ignore every exception.

Member

dmcdougall commented Dec 17, 2012

@mdboom Good idea. Also forced me not to except:. I.e, without even Exception.

Member

dmcdougall commented Dec 17, 2012

See #1604.

Member

dmcdougall commented Dec 17, 2012

Fixed by #1604.

dmcdougall closed this Dec 17, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment