2626# - 'light' is an invalid weight value, remove it.
2727
2828from base64 import b64encode
29+ from collections import namedtuple
2930import copy
3031import dataclasses
3132from functools import lru_cache
128129 'sans' ,
129130}
130131
132+ _ExceptionProxy = namedtuple ('_ExceptionProxy' , ['klass' , 'message' ])
131133
132134# OS Font paths
133135try :
@@ -1288,8 +1290,8 @@ def findfont(self, prop, fontext='ttf', directory=None,
12881290 ret = self ._findfont_cached (
12891291 prop , fontext , directory , fallback_to_default , rebuild_if_missing ,
12901292 rc_params )
1291- if isinstance (ret , Exception ):
1292- raise ret
1293+ if isinstance (ret , _ExceptionProxy ):
1294+ raise ret . klass ( ret . message )
12931295 return ret
12941296
12951297 def get_font_names (self ):
@@ -1440,10 +1442,12 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
14401442 fallback_to_default = False )
14411443 else :
14421444 # This return instead of raise is intentional, as we wish to
1443- # cache the resulting exception , which will not occur if it was
1445+ # cache that it was not found , which will not occur if it was
14441446 # actually raised.
1445- return ValueError (f"Failed to find font { prop } , and fallback "
1446- f"to the default font was disabled" )
1447+ return _ExceptionProxy (
1448+ ValueError ,
1449+ f"Failed to find font { prop } , and fallback to the default font was disabled"
1450+ )
14471451 else :
14481452 _log .debug ('findfont: Matching %s to %s (%r) with score of %f.' ,
14491453 prop , best_font .name , best_font .fname , best_score )
@@ -1463,9 +1467,9 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
14631467 prop , fontext , directory , rebuild_if_missing = False )
14641468 else :
14651469 # This return instead of raise is intentional, as we wish to
1466- # cache the resulting exception , which will not occur if it was
1470+ # cache that it was not found , which will not occur if it was
14671471 # actually raised.
1468- return ValueError ( "No valid font could be found" )
1472+ return _ExceptionProxy ( ValueError , "No valid font could be found" )
14691473
14701474 return _cached_realpath (result )
14711475
0 commit comments