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

Skip to content

Commit 75afc13

Browse files
authored
Merge pull request #23332 from anntzer/ls
Validate Text linespacing on input.
2 parents cbc1b19 + ffde360 commit 75afc13

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

lib/matplotlib/tests/test_text.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,8 @@ def test_two_2line_texts(spacing1, spacing2):
519519
fig = plt.figure()
520520
renderer = fig.canvas.get_renderer()
521521

522-
text1 = plt.text(0.25, 0.5, text_string, linespacing=spacing1)
523-
text2 = plt.text(0.25, 0.5, text_string, linespacing=spacing2)
522+
text1 = fig.text(0.25, 0.5, text_string, linespacing=spacing1)
523+
text2 = fig.text(0.25, 0.5, text_string, linespacing=spacing2)
524524
fig.canvas.draw()
525525

526526
box1 = text1.get_window_extent(renderer=renderer)
@@ -534,6 +534,11 @@ def test_two_2line_texts(spacing1, spacing2):
534534
assert box1.height != box2.height
535535

536536

537+
def test_validate_linespacing():
538+
with pytest.raises(TypeError):
539+
plt.text(.25, .5, "foo", linespacing="abc")
540+
541+
537542
def test_nonfinite_pos():
538543
fig, ax = plt.subplots()
539544
ax.text(0, np.nan, 'nan')

lib/matplotlib/text.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import functools
66
import logging
77
import math
8-
import numbers
8+
from numbers import Real
99
import weakref
1010

1111
import numpy as np
@@ -181,7 +181,7 @@ def __init__(self,
181181
self._renderer = None
182182
if linespacing is None:
183183
linespacing = 1.2 # Maybe use rcParam later.
184-
self._linespacing = linespacing
184+
self.set_linespacing(linespacing)
185185
self.set_rotation_mode(rotation_mode)
186186
self.update(kwargs)
187187

@@ -1000,6 +1000,7 @@ def set_linespacing(self, spacing):
10001000
----------
10011001
spacing : float (multiple of font size)
10021002
"""
1003+
_api.check_isinstance(Real, spacing=spacing)
10031004
self._linespacing = spacing
10041005
self.stale = True
10051006

@@ -1186,7 +1187,7 @@ def set_rotation(self, s):
11861187
The rotation angle in degrees in mathematically positive direction
11871188
(counterclockwise). 'horizontal' equals 0, 'vertical' equals 90.
11881189
"""
1189-
if isinstance(s, numbers.Real):
1190+
if isinstance(s, Real):
11901191
self._rotation = float(s) % 360
11911192
elif cbook._str_equal(s, 'horizontal') or s is None:
11921193
self._rotation = 0.

0 commit comments

Comments
 (0)