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

Skip to content

Commit c654b9c

Browse files
committed
Suppress exception chaining in FontProperties.
For `FontProperties(stretch=12345)`; before: ``` Traceback (most recent call last): File ".../matplotlib/font_manager.py", line 888, in set_stretch raise ValueError() ValueError The above exception was the direct cause of the following exception: Traceback (most recent call last): File "<string>", line 1, in <module> File ".../matplotlib/font_manager.py", line 695, in __init__ self.set_stretch(stretch) File ".../matplotlib/font_manager.py", line 891, in set_stretch raise ValueError("stretch is invalid") from err ValueError: stretch is invalid ``` after ``` Traceback (most recent call last): File "<string>", line 1, in <module> File ".../matplotlib/font_manager.py", line 695, in __init__ self.set_stretch(stretch) File ".../matplotlib/font_manager.py", line 900, in set_stretch raise ValueError(f"{stretch=} is invalid") ValueError: stretch=12345 is invalid ```
1 parent 63e9ea3 commit c654b9c

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

lib/matplotlib/font_manager.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -860,14 +860,18 @@ def set_weight(self, weight):
860860
"""
861861
if weight is None:
862862
weight = rcParams['font.weight']
863+
if weight in weight_dict:
864+
self._weight = weight
865+
return
863866
try:
864867
weight = int(weight)
865-
if weight < 0 or weight > 1000:
866-
raise ValueError()
867868
except ValueError:
868-
if weight not in weight_dict:
869-
raise ValueError("weight is invalid")
870-
self._weight = weight
869+
pass
870+
else:
871+
if 0 <= weight <= 1000:
872+
self._weight = weight
873+
return
874+
raise ValueError(f"{weight=} is invalid")
871875

872876
def set_stretch(self, stretch):
873877
"""
@@ -882,14 +886,18 @@ def set_stretch(self, stretch):
882886
"""
883887
if stretch is None:
884888
stretch = rcParams['font.stretch']
889+
if stretch in stretch_dict:
890+
self._stretch = stretch
891+
return
885892
try:
886893
stretch = int(stretch)
887-
if stretch < 0 or stretch > 1000:
888-
raise ValueError()
889894
except ValueError as err:
890-
if stretch not in stretch_dict:
891-
raise ValueError("stretch is invalid") from err
892-
self._stretch = stretch
895+
pass
896+
else:
897+
if 0 <= stretch <= 1000:
898+
self._stretch = stretch
899+
return
900+
raise ValueError(f"{stretch=} is invalid")
893901

894902
def set_size(self, size):
895903
"""

0 commit comments

Comments
 (0)