Description
Bug report
font_manager crashes with font not found errors,
Bug summary
there is an Issue allready for this, but after i fixed it i couldnt find that issue back as when i fixed it my errors where gone .
Some error about font not found
Code for reproduction
from matplotlib.font_manager import win32InstalledFonts
print(win32InstalledFonts())
Matplotlib version
- Operating system: Windows
- Matplotlib version: matplotlib:2.0.2
- Matplotlib backend (
print(matplotlib.get_backend())
): - Python version: Python:3.6.2
- Jupyter version (if applicable): (latest)
- Other libraries:
Conda default
Here is my fix:
def win32InstalledFonts(directory=None, fontext='ttf'):
"""
Search for fonts in the specified font directory, or use the
system directories if none given. A list of TrueType font
filenames are returned by default, or AFM fonts if *fontext* ==
'afm'.
"""
import winreg
if directory is None:
directory = win32FontDirectory()
fontext = get_fontext_synonyms(fontext)
items = set()
for fontdir in MSFontDirectories:
try:
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, fontdir) as local:
for j in range(winreg.QueryInfoKey(local)[1]):
key, direc, tp = winreg.EnumValue(local, j)
if not isinstance(direc, str):
continue
# Work around for https://bugs.python.org/issue25778, which
# is fixed in Py>=3.6.1.
direc = direc.split("\0", 1)[0]
try:
path = Path(directory, direc).resolve() #errors on file not found
items.add(str(path))
except:
#print ("found invalid font direc : {}",direc)
continue
except (OSError, MemoryError):
continue
return list(items)
Essentially problems are caused because the registry does not really reflect all the fonts, that was the reasons Path(directory,direc).resolve() crashed, so placed it inside another try excerpt and it now works.
The point here is registry does not reflect the file system, people can remove software in wild ways.
i left a commented option to show the missing files in the excerpt.
Its fixed now so i hope other people with the same issues can be happy and go coding now :),
its my first python opensource contribution