26
26
# - 'light' is an invalid weight value, remove it.
27
27
28
28
from base64 import b64encode
29
+ from collections import namedtuple
29
30
import copy
30
31
import dataclasses
31
32
from functools import lru_cache
128
129
'sans' ,
129
130
}
130
131
132
+ _ExceptionProxy = namedtuple ('_ExceptionProxy' , ['klass' , 'message' ])
131
133
132
134
# OS Font paths
133
135
try :
@@ -1288,8 +1290,8 @@ def findfont(self, prop, fontext='ttf', directory=None,
1288
1290
ret = self ._findfont_cached (
1289
1291
prop , fontext , directory , fallback_to_default , rebuild_if_missing ,
1290
1292
rc_params )
1291
- if isinstance (ret , Exception ):
1292
- raise ret
1293
+ if isinstance (ret , _ExceptionProxy ):
1294
+ raise ret . klass ( ret . message )
1293
1295
return ret
1294
1296
1295
1297
def get_font_names (self ):
@@ -1440,10 +1442,12 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
1440
1442
fallback_to_default = False )
1441
1443
else :
1442
1444
# 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
1444
1446
# 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
+ )
1447
1451
else :
1448
1452
_log .debug ('findfont: Matching %s to %s (%r) with score of %f.' ,
1449
1453
prop , best_font .name , best_font .fname , best_score )
@@ -1463,9 +1467,9 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
1463
1467
prop , fontext , directory , rebuild_if_missing = False )
1464
1468
else :
1465
1469
# 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
1467
1471
# actually raised.
1468
- return ValueError ( "No valid font could be found" )
1472
+ return _ExceptionProxy ( ValueError , "No valid font could be found" )
1469
1473
1470
1474
return _cached_realpath (result )
1471
1475
0 commit comments