mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-03 19:33:07 +03:00
commit
d053920e04
|
@ -1087,6 +1087,3 @@ class TestRegistry:
|
||||||
def test_encode_registry_fail(self) -> None:
|
def test_encode_registry_fail(self) -> None:
|
||||||
with pytest.raises(OSError):
|
with pytest.raises(OSError):
|
||||||
Image._getencoder("RGB", "DoesNotExist", ("args",), extra=("extra",))
|
Image._getencoder("RGB", "DoesNotExist", ("args",), extra=("extra",))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,11 @@ import pytest
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
|
def calculate_branch_coverage():
|
||||||
|
b = Image.Branches
|
||||||
|
print("Branches covered:", sum(b.values()))
|
||||||
|
|
||||||
|
|
||||||
def test_merge_wrong_number_of_bands():
|
def test_merge_wrong_number_of_bands():
|
||||||
R = Image.new('L', (100, 100), color=255)
|
R = Image.new('L', (100, 100), color=255)
|
||||||
G = Image.new('L', (100, 100), color=128)
|
G = Image.new('L', (100, 100), color=128)
|
||||||
|
|
48
conftest.py
48
conftest.py
|
@ -1,3 +1,51 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
import pytest
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from PIL import Image
|
||||||
|
from PIL import PdfParser
|
||||||
|
|
||||||
pytest_plugins = ["Tests.helper"]
|
pytest_plugins = ["Tests.helper"]
|
||||||
|
|
||||||
|
|
||||||
|
def calculate_coverage(test_name):
|
||||||
|
all_branches = {
|
||||||
|
"branches1": Image.branches,
|
||||||
|
"branches2": PdfParser.XrefTable.branches,
|
||||||
|
# Add more
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, branches in all_branches.items():
|
||||||
|
num_branches = len(branches)
|
||||||
|
branch_covered = {key: value for key, value in branches.items() if value is True}
|
||||||
|
sum_branches = len(branch_covered)
|
||||||
|
coverage = (sum_branches / num_branches) * 100
|
||||||
|
|
||||||
|
print(f"\n{name} - Branches covered: {sum_branches}")
|
||||||
|
print(f"{name} - Total branches: {num_branches}")
|
||||||
|
print(f"{name} - BRANCH COVERAGE: {coverage}%\n")
|
||||||
|
|
||||||
|
|
||||||
|
return all_branches["branches1"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.hookimpl(tryfirst=True)
|
||||||
|
def pytest_runtest_protocol(item, nextitem):
|
||||||
|
global test_name
|
||||||
|
|
||||||
|
last_arg = sys.argv[-1]
|
||||||
|
|
||||||
|
test_name = last_arg.split('/')[-1].split('::')[-1]
|
||||||
|
|
||||||
|
test_name = test_name.rstrip('.py')
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
@pytest.hookimpl(tryfirst=True)
|
||||||
|
def pytest_sessionfinish(session, exitstatus):
|
||||||
|
global test_name
|
||||||
|
|
||||||
|
coverage = calculate_coverage(test_name)
|
||||||
|
print("\nBRANCH COVERAGE for", test_name, ":", coverage, "%\n")
|
||||||
|
|
||||||
|
|
|
@ -1,22 +0,0 @@
|
||||||
from PIL import Image
|
|
||||||
from PIL.Image import branches
|
|
||||||
|
|
||||||
# Define a function to calculate and print branch coverage
|
|
||||||
|
|
||||||
|
|
||||||
def calculate_branch_coverage():
|
|
||||||
num_branches = len(branches)
|
|
||||||
branch_covered = {key: value for key, value in branches.items() if value is True}
|
|
||||||
sum_branches = len(branch_covered)
|
|
||||||
coverage = (sum_branches/num_branches) * 100
|
|
||||||
print(f"Branches covered: {sum_branches}")
|
|
||||||
print(f"Total branches: {num_branches}")
|
|
||||||
print("\nBRANCH COVERAGE:", coverage, "%\n")
|
|
||||||
|
|
||||||
R = Image.new('L', (100, 100), color=255)
|
|
||||||
G = Image.new('L', (100, 100), color=128)
|
|
||||||
B = Image.new('L', (100, 100), color=0)
|
|
||||||
merged_image = Image.merge('RGB', (R, G, B))
|
|
||||||
merged_image.save('merged_image.png')
|
|
||||||
|
|
||||||
calculate_branch_coverage()
|
|
|
@ -67,6 +67,13 @@ except ImportError:
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
branches = {
|
||||||
|
"1": False,
|
||||||
|
"2": False,
|
||||||
|
"3": False,
|
||||||
|
"4": False,
|
||||||
|
"5": False,
|
||||||
|
}
|
||||||
|
|
||||||
class DecompressionBombWarning(RuntimeWarning):
|
class DecompressionBombWarning(RuntimeWarning):
|
||||||
pass
|
pass
|
||||||
|
@ -75,14 +82,6 @@ class DecompressionBombWarning(RuntimeWarning):
|
||||||
class DecompressionBombError(Exception):
|
class DecompressionBombError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
branches = {
|
|
||||||
"1": False,
|
|
||||||
"2": False,
|
|
||||||
"3": False,
|
|
||||||
"4": False,
|
|
||||||
"5": False,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
# Limit to around a quarter gigabyte for a 24-bit (3 bpp) image
|
# Limit to around a quarter gigabyte for a 24-bit (3 bpp) image
|
||||||
MAX_IMAGE_PIXELS: int | None = int(1024 * 1024 * 1024 // 4 // 3)
|
MAX_IMAGE_PIXELS: int | None = int(1024 * 1024 * 1024 // 4 // 3)
|
||||||
|
@ -594,6 +593,7 @@ class Image:
|
||||||
self._close_fp()
|
self._close_fp()
|
||||||
self.fp = None
|
self.fp = None
|
||||||
|
|
||||||
|
|
||||||
def close(self) -> None:
|
def close(self) -> None:
|
||||||
"""
|
"""
|
||||||
Closes the file pointer, if possible.
|
Closes the file pointer, if possible.
|
||||||
|
|
|
@ -112,6 +112,14 @@ class IndirectObjectDef(IndirectReference):
|
||||||
|
|
||||||
|
|
||||||
class XrefTable:
|
class XrefTable:
|
||||||
|
|
||||||
|
branches = {
|
||||||
|
"1": False,
|
||||||
|
"2": False,
|
||||||
|
"3": False,
|
||||||
|
"4": False,
|
||||||
|
}
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.existing_entries = {} # object ID => (offset, generation)
|
self.existing_entries = {} # object ID => (offset, generation)
|
||||||
self.new_entries = {} # object ID => (offset, generation)
|
self.new_entries = {} # object ID => (offset, generation)
|
||||||
|
@ -134,15 +142,19 @@ class XrefTable:
|
||||||
|
|
||||||
def __delitem__(self, key):
|
def __delitem__(self, key):
|
||||||
if key in self.new_entries:
|
if key in self.new_entries:
|
||||||
|
XrefTable.branches["1"] = True
|
||||||
generation = self.new_entries[key][1] + 1
|
generation = self.new_entries[key][1] + 1
|
||||||
del self.new_entries[key]
|
del self.new_entries[key]
|
||||||
self.deleted_entries[key] = generation
|
self.deleted_entries[key] = generation
|
||||||
elif key in self.existing_entries:
|
elif key in self.existing_entries:
|
||||||
|
XrefTable.branches["2"] = True
|
||||||
generation = self.existing_entries[key][1] + 1
|
generation = self.existing_entries[key][1] + 1
|
||||||
self.deleted_entries[key] = generation
|
self.deleted_entries[key] = generation
|
||||||
elif key in self.deleted_entries:
|
elif key in self.deleted_entries:
|
||||||
|
XrefTable.branches["3"] = True
|
||||||
generation = self.deleted_entries[key]
|
generation = self.deleted_entries[key]
|
||||||
else:
|
else:
|
||||||
|
XrefTable.branches["4"] = True
|
||||||
msg = f"object ID {key} cannot be deleted because it doesn't exist"
|
msg = f"object ID {key} cannot be deleted because it doesn't exist"
|
||||||
raise IndexError(msg)
|
raise IndexError(msg)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user