Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Tests/benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

import hashlib
import os
import pathlib
import re
import warnings
from importlib.util import find_spec
from io import BytesIO
from pathlib import Path

import pytest

Expand All @@ -32,7 +32,7 @@
pytest.skip("pytest-benchmark or pytest-codspeed required", allow_module_level=True)

_save_results = os.environ.get("PILLOW_BENCHMARK_SAVE_RESULTS_PATH")
SAVE_RESULTS_PATH = pathlib.Path(_save_results) if _save_results else None
SAVE_RESULTS_PATH = Path(_save_results) if _save_results else None

# These can be adjusted to add more modes to benchmark
# (however all features benchmarked might not support all PIL modes).
Expand All @@ -45,7 +45,7 @@
SIZES = [(1237, 811)] # Primes, non-power-of-two, asymmetric, approximately 1024x1024

# For benchmarks that act on test fixture files, these are the paths loaded.
IMAGES_PATH = pathlib.Path(__file__).parent / "images"
IMAGES_PATH = Path(__file__).parent / "images"
PATHS = [
IMAGES_PATH / "flower2.jpg",
]
Expand All @@ -60,7 +60,7 @@ def _format_size(size: tuple[int, int]) -> str:
return f"{size[0]}x{size[1]}"


def _format_path(path: pathlib.Path) -> str:
def _format_path(path: Path) -> str:
return path.name


Expand Down Expand Up @@ -569,7 +569,7 @@ def test_draw_lines_blend(

@pytest.mark.benchmark(group="load")
@pytest.mark.parametrize("path", PATHS, ids=_format_path)
def test_load(bench: BenchmarkFixture, path: pathlib.Path) -> None:
def test_load(bench: BenchmarkFixture, path: Path) -> None:
def run() -> None:
with Image.open(path) as im:
im.load()
Expand All @@ -579,7 +579,7 @@ def run() -> None:

@pytest.mark.benchmark(group="save")
@pytest.mark.parametrize("path", PATHS, ids=_format_path)
def test_save_jpeg(bench: BenchmarkFixture, path: pathlib.Path) -> None:
def test_save_jpeg(bench: BenchmarkFixture, path: Path) -> None:
with Image.open(path) as im:
im.load()
bench(lambda: im.save(BytesIO(), format="JPEG", quality=85))
Expand Down Expand Up @@ -982,11 +982,11 @@ def test_quantize_to_palette(
benchmark_save: BenchmarkSave,
dither: Image.Dither,
output_mode: str,
source_type: str | pathlib.Path,
source_type: str | Path,
palette_type: str,
size: tuple[int, int],
) -> None:
if isinstance(source_type, pathlib.Path):
if isinstance(source_type, Path):
with Image.open(source_type) as source_im:
im = source_im.convert("RGB").resize(size)
elif source_type == "synthetic":
Expand Down
37 changes: 20 additions & 17 deletions Tests/oss-fuzz/test_fuzzers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import subprocess
import sys
from pathlib import Path

import fuzzers
import packaging
Expand All @@ -21,17 +21,22 @@
reason="Known failing with libjpeg_turbo 2.0"
)

tests_path = Path(__file__).parent.parent


def find_files(subdir: str) -> list[Path]:
return [path for path in tests_path.joinpath(subdir).rglob("*") if path.is_file()]


@pytest.mark.parametrize(
"path",
subprocess.check_output("find Tests/images -type f", shell=True).split(b"\n"),
find_files("images"),
ids=lambda p: str(p.relative_to(tests_path)),
)
def test_fuzz_images(path: str) -> None:
def test_fuzz_images(path: Path) -> None:
fuzzers.enable_decompressionbomb_error()
try:
with open(path, "rb") as f:
fuzzers.fuzz_image(f.read())
assert True
fuzzers.fuzz_image(path.read_bytes())
except (
# Known exceptions from Pillow
OSError,
Expand All @@ -44,21 +49,19 @@ def test_fuzz_images(path: str) -> None:
Image.DecompressionBombError,
Image.DecompressionBombWarning,
):
assert True
pass
finally:
fuzzers.disable_decompressionbomb_error()


@skip_unless_feature("freetype2")
@pytest.mark.parametrize(
"path", subprocess.check_output("find Tests/fonts -type f", shell=True).split(b"\n")
"path",
find_files("fonts"),
ids=lambda p: str(p.relative_to(tests_path)),
)
def test_fuzz_fonts(path: str) -> None:
if not path:
return
with open(path, "rb") as f:
try:
fuzzers.fuzz_font(f.read())
except (Image.DecompressionBombError, Image.DecompressionBombWarning, OSError):
pass
assert True
def test_fuzz_fonts(path: Path) -> None:
try:
fuzzers.fuzz_font(path.read_bytes())
except (Image.DecompressionBombError, Image.DecompressionBombWarning, OSError):
pass
Loading