Skip to content

Commit 8a6050e

Browse files
radarherenulano
authored andcommitted
Replaced __internal__ argument with warning filters
1 parent 729fe6f commit 8a6050e

File tree

3 files changed

+70
-69
lines changed

3 files changed

+70
-69
lines changed

src/PIL/ImageDraw.py

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import math
3434
import numbers
35+
import warnings
3536

3637
from . import Image, ImageColor
3738
from ._deprecate import deprecate
@@ -375,15 +376,16 @@ def _multiline_split(self, text):
375376

376377
def _multiline_spacing(self, font, spacing, stroke_width):
377378
# this can be replaced with self.textbbox(...)[3] when textsize is removed
378-
return (
379-
self.textsize(
380-
"A",
381-
font=font,
382-
stroke_width=stroke_width,
383-
__internal__=True,
384-
)[1]
385-
+ spacing
386-
)
379+
with warnings.catch_warnings():
380+
warnings.filterwarnings("ignore", category=DeprecationWarning)
381+
return (
382+
self.textsize(
383+
"A",
384+
font=font,
385+
stroke_width=stroke_width,
386+
)[1]
387+
+ spacing
388+
)
387389

388390
def text(
389391
self,
@@ -582,34 +584,34 @@ def textsize(
582584
features=None,
583585
language=None,
584586
stroke_width=0,
585-
__internal__=False,
586587
):
587588
"""Get the size of a given string, in pixels."""
588-
if not __internal__:
589-
deprecate("textsize", 10, "textbbox or textlength")
589+
deprecate("textsize", 10, "textbbox or textlength")
590590
if self._multiline_check(text):
591-
return self.multiline_textsize(
591+
with warnings.catch_warnings():
592+
warnings.filterwarnings("ignore", category=DeprecationWarning)
593+
return self.multiline_textsize(
594+
text,
595+
font,
596+
spacing,
597+
direction,
598+
features,
599+
language,
600+
stroke_width,
601+
)
602+
603+
if font is None:
604+
font = self.getfont()
605+
with warnings.catch_warnings():
606+
warnings.filterwarnings("ignore", category=DeprecationWarning)
607+
return font.getsize(
592608
text,
593-
font,
594-
spacing,
595609
direction,
596610
features,
597611
language,
598612
stroke_width,
599-
__internal__=True,
600613
)
601614

602-
if font is None:
603-
font = self.getfont()
604-
return font.getsize(
605-
text,
606-
direction,
607-
features,
608-
language,
609-
stroke_width,
610-
__internal__=True,
611-
)
612-
613615
def multiline_textsize(
614616
self,
615617
text,
@@ -619,25 +621,24 @@ def multiline_textsize(
619621
features=None,
620622
language=None,
621623
stroke_width=0,
622-
__internal__=False,
623624
):
624-
if not __internal__:
625-
deprecate("multiline_textsize", 10, "multiline_textbbox")
625+
deprecate("multiline_textsize", 10, "multiline_textbbox")
626626
max_width = 0
627627
lines = self._multiline_split(text)
628628
line_spacing = self._multiline_spacing(font, spacing, stroke_width)
629-
for line in lines:
630-
line_width, line_height = self.textsize(
631-
line,
632-
font,
633-
spacing,
634-
direction,
635-
features,
636-
language,
637-
stroke_width,
638-
__internal__=True,
639-
)
640-
max_width = max(max_width, line_width)
629+
with warnings.catch_warnings():
630+
warnings.filterwarnings("ignore", category=DeprecationWarning)
631+
for line in lines:
632+
line_width, line_height = self.textsize(
633+
line,
634+
font,
635+
spacing,
636+
direction,
637+
features,
638+
language,
639+
stroke_width,
640+
)
641+
max_width = max(max_width, line_width)
641642
return max_width, len(lines) * line_spacing - spacing
642643

643644
def textlength(
@@ -662,14 +663,15 @@ def textlength(
662663
return font.getlength(text, mode, direction, features, language)
663664
except AttributeError:
664665
deprecate("textlength support for fonts without getlength", 10)
665-
size = self.textsize(
666-
text,
667-
font,
668-
direction=direction,
669-
features=features,
670-
language=language,
671-
__internal__=True,
672-
)
666+
with warnings.catch_warnings():
667+
warnings.filterwarnings("ignore", category=DeprecationWarning)
668+
size = self.textsize(
669+
text,
670+
font,
671+
direction=direction,
672+
features=features,
673+
language=language,
674+
)
673675
if direction == "ttb":
674676
return size[1]
675677
return size[0]

src/PIL/ImageDraw2.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
"""
2525

2626

27+
import warnings
28+
2729
from . import Image, ImageColor, ImageDraw, ImageFont, ImagePath
2830
from ._deprecate import deprecate
2931

@@ -180,7 +182,9 @@ def textsize(self, text, font):
180182
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.textsize`
181183
"""
182184
deprecate("textsize", 10, "textbbox or textlength")
183-
return self.draw.textsize(text, font=font.font, __internal__=True)
185+
with warnings.catch_warnings():
186+
warnings.filterwarnings("ignore", category=DeprecationWarning)
187+
return self.draw.textsize(text, font=font.font)
184188

185189
def textbbox(self, xy, text, font):
186190
"""

src/PIL/ImageFont.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ def getsize(self, text, *args, **kwargs):
147147
148148
:return: (width, height)
149149
"""
150-
if not kwargs.get("__internal__"):
151-
deprecate("getsize", 10, "getbbox or getlength")
150+
deprecate("getsize", 10, "getbbox or getlength")
152151
return self.font.getsize(text)
153152

154153
def getmask(self, text, mode="", *args, **kwargs):
@@ -425,7 +424,6 @@ def getsize(
425424
features=None,
426425
language=None,
427426
stroke_width=0,
428-
__internal__=False,
429427
):
430428
"""
431429
.. deprecated:: 9.2.0
@@ -479,8 +477,7 @@ def getsize(
479477
480478
:return: (width, height)
481479
"""
482-
if not __internal__:
483-
deprecate("getsize", 10, "getbbox or getlength")
480+
deprecate("getsize", 10, "getbbox or getlength")
484481
# vertical offset is added for historical reasons
485482
# see https://github.com/python-pillow/Pillow/pull/4910#discussion_r486682929
486483
size, offset = self.font.getsize(text, "L", direction, features, language)
@@ -545,14 +542,14 @@ def getsize_multiline(
545542
deprecate("getsize_multiline", 10, "ImageDraw.multiline_textbbox")
546543
max_width = 0
547544
lines = self._multiline_split(text)
548-
line_spacing = (
549-
self.getsize("A", stroke_width=stroke_width, __internal__=True)[1] + spacing
550-
)
551-
for line in lines:
552-
line_width, line_height = self.getsize(
553-
line, direction, features, language, stroke_width, __internal__=True
554-
)
555-
max_width = max(max_width, line_width)
545+
with warnings.catch_warnings():
546+
warnings.filterwarnings("ignore", category=DeprecationWarning)
547+
line_spacing = self.getsize("A", stroke_width=stroke_width)[1] + spacing
548+
for line in lines:
549+
line_width, line_height = self.getsize(
550+
line, direction, features, language, stroke_width
551+
)
552+
max_width = max(max_width, line_width)
556553

557554
return max_width, len(lines) * line_spacing - spacing
558555

@@ -856,11 +853,9 @@ def getsize(self, text, *args, **kwargs):
856853
857854
Use :py:meth:`.getbbox` or :py:meth:`.getlength` instead.
858855
"""
859-
if not kwargs.get("__internal__"):
860-
deprecate("getsize", 10, "getbbox or getlength")
861-
try:
862-
w, h = self.font.getsize(text, __internal__=True)
863-
except TypeError:
856+
deprecate("getsize", 10, "getbbox or getlength")
857+
with warnings.catch_warnings():
858+
warnings.filterwarnings("ignore", category=DeprecationWarning)
864859
w, h = self.font.getsize(text)
865860
if self.orientation in (Image.Transpose.ROTATE_90, Image.Transpose.ROTATE_270):
866861
return h, w

0 commit comments

Comments
 (0)