2424# - 'light' is an invalid weight value, remove it.
2525
2626from base64 import b64encode
27+ from collections import namedtuple
2728import copy
2829import dataclasses
2930from functools import lru_cache
126127 'sans' ,
127128}
128129
130+ _ExceptionProxy = namedtuple ('_ExceptionProxy' , ['klass' , 'message' ])
129131
130132# OS Font paths
131133try :
@@ -1259,8 +1261,8 @@ def findfont(self, prop, fontext='ttf', directory=None,
12591261 ret = self ._findfont_cached (
12601262 prop , fontext , directory , fallback_to_default , rebuild_if_missing ,
12611263 rc_params )
1262- if isinstance (ret , Exception ):
1263- raise ret
1264+ if isinstance (ret , _ExceptionProxy ):
1265+ raise ret . klass ( ret . message )
12641266 return ret
12651267
12661268 def get_font_names (self ):
@@ -1411,10 +1413,12 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
14111413 fallback_to_default = False )
14121414 else :
14131415 # This return instead of raise is intentional, as we wish to
1414- # cache the resulting exception , which will not occur if it was
1416+ # cache that it was not found , which will not occur if it was
14151417 # actually raised.
1416- return ValueError (f"Failed to find font { prop } , and fallback "
1417- f"to the default font was disabled" )
1418+ return _ExceptionProxy (
1419+ ValueError ,
1420+ f"Failed to find font { prop } , and fallback to the default font was disabled"
1421+ )
14181422 else :
14191423 _log .debug ('findfont: Matching %s to %s (%r) with score of %f.' ,
14201424 prop , best_font .name , best_font .fname , best_score )
@@ -1434,9 +1438,9 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
14341438 prop , fontext , directory , rebuild_if_missing = False )
14351439 else :
14361440 # This return instead of raise is intentional, as we wish to
1437- # cache the resulting exception , which will not occur if it was
1441+ # cache that it was not found , which will not occur if it was
14381442 # actually raised.
1439- return ValueError ( "No valid font could be found" )
1443+ return _ExceptionProxy ( ValueError , "No valid font could be found" )
14401444
14411445 return _cached_realpath (result )
14421446
0 commit comments