2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2024-01-23 10:51:41 +03:00
|
|
|
from PIL import Image, ImageDraw, ImageFont, _util
|
2019-11-21 05:42:52 +03:00
|
|
|
|
2024-01-23 10:51:41 +03:00
|
|
|
from .helper import PillowLeakTestCase, features, skip_unless_feature
|
|
|
|
|
|
|
|
original_core = ImageFont.core
|
2017-09-02 19:03:56 +03:00
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2017-09-02 19:03:56 +03:00
|
|
|
class TestTTypeFontLeak(PillowLeakTestCase):
|
2021-10-15 17:30:05 +03:00
|
|
|
# fails at iteration 3 in main
|
2017-09-02 19:03:56 +03:00
|
|
|
iterations = 10
|
2018-03-04 07:33:44 +03:00
|
|
|
mem_limit = 4096 # k
|
2017-09-02 19:03:56 +03:00
|
|
|
|
2024-01-22 10:42:43 +03:00
|
|
|
def _test_font(self, font: ImageFont.FreeTypeFont) -> None:
|
2019-06-13 18:54:24 +03:00
|
|
|
im = Image.new("RGB", (255, 255), "white")
|
2017-09-02 19:03:56 +03:00
|
|
|
draw = ImageDraw.ImageDraw(im)
|
2019-06-13 18:54:24 +03:00
|
|
|
self._test_leak(
|
|
|
|
lambda: draw.text(
|
|
|
|
(0, 0), "some text " * 1024, font=font, fill="black" # ~10k
|
|
|
|
)
|
|
|
|
)
|
2018-01-27 09:07:24 +03:00
|
|
|
|
2020-02-18 01:03:32 +03:00
|
|
|
@skip_unless_feature("freetype2")
|
2024-01-22 10:42:43 +03:00
|
|
|
def test_leak(self) -> None:
|
2019-06-13 18:54:24 +03:00
|
|
|
ttype = ImageFont.truetype("Tests/fonts/FreeMono.ttf", 20)
|
2017-09-02 19:03:56 +03:00
|
|
|
self._test_font(ttype)
|
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2017-09-02 19:03:56 +03:00
|
|
|
class TestDefaultFontLeak(TestTTypeFontLeak):
|
2021-10-15 17:30:05 +03:00
|
|
|
# fails at iteration 37 in main
|
2017-09-02 19:03:56 +03:00
|
|
|
iterations = 100
|
2018-03-04 07:33:44 +03:00
|
|
|
mem_limit = 1024 # k
|
2018-01-27 09:07:24 +03:00
|
|
|
|
2024-01-22 10:42:43 +03:00
|
|
|
def test_leak(self) -> None:
|
2024-01-23 10:51:41 +03:00
|
|
|
if features.check_module("freetype2"):
|
|
|
|
ImageFont.core = _util.DeferredError(ImportError)
|
2024-01-24 00:12:06 +03:00
|
|
|
try:
|
|
|
|
default_font = ImageFont.load_default()
|
|
|
|
finally:
|
|
|
|
ImageFont.core = original_core
|
2024-01-23 10:51:41 +03:00
|
|
|
|
2017-09-02 19:03:56 +03:00
|
|
|
self._test_font(default_font)
|