Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ff31356

Browse files
committed
Don't hide exceptions in FontManager.addfont.
We only need to hide them when constructing the default fontManager instance (because we don't want a single bad font in the system to prevent Matplotlib from importing, but if the end user manually calls addfont, errors should not pass silently). Release critical as addfont is a new API in 3.2.
1 parent 508f21f commit ff31356

File tree

1 file changed

+12
-29
lines changed

1 file changed

+12
-29
lines changed

lib/matplotlib/font_manager.py

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,13 @@ def __init__(self, size=None, weight='normal'):
986986
for fontext in ["afm", "ttf"]:
987987
for path in [*findSystemFonts(paths, fontext=fontext),
988988
*findSystemFonts(fontext=fontext)]:
989-
self.addfont(path)
989+
try:
990+
self.addfont(path)
991+
except OSError as exc:
992+
_log.info("Failed to open font file %s: %s", path, exc)
993+
except Exception as exc:
994+
_log.info("Failed to extract properties from %s: %s",
995+
path, exc)
990996

991997
def addfont(self, path):
992998
"""
@@ -998,36 +1004,13 @@ def addfont(self, path):
9981004
path : str or path-like
9991005
"""
10001006
if Path(path).suffix.lower() == ".afm":
1001-
try:
1002-
with open(path, "rb") as fh:
1003-
font = afm.AFM(fh)
1004-
except EnvironmentError:
1005-
_log.info("Could not open font file %s", path)
1006-
return
1007-
except RuntimeError:
1008-
_log.info("Could not parse font file %s", path)
1009-
return
1010-
try:
1011-
prop = afmFontProperty(path, font)
1012-
except KeyError as exc:
1013-
_log.info("Could not extract properties for %s: %s", path, exc)
1014-
return
1007+
with open(path, "rb") as fh:
1008+
font = afm.AFM(fh)
1009+
prop = afmFontProperty(path, font)
10151010
self.afmlist.append(prop)
10161011
else:
1017-
try:
1018-
font = ft2font.FT2Font(path)
1019-
except (OSError, RuntimeError) as exc:
1020-
_log.info("Could not open font file %s: %s", path, exc)
1021-
return
1022-
except UnicodeError:
1023-
_log.info("Cannot handle unicode filenames")
1024-
return
1025-
try:
1026-
prop = ttfFontProperty(font)
1027-
except (KeyError, RuntimeError, ValueError,
1028-
NotImplementedError) as exc:
1029-
_log.info("Could not extract properties for %s: %s", path, exc)
1030-
return
1012+
font = ft2font.FT2Font(path)
1013+
prop = ttfFontProperty(font)
10311014
self.ttflist.append(prop)
10321015

10331016
@property

0 commit comments

Comments
 (0)