From b129900e4828ae38b28c97403072630bed9f8d42 Mon Sep 17 00:00:00 2001 From: Duru Date: Fri, 21 Jun 2024 14:58:01 +0200 Subject: [PATCH] deekshu coverage tool and duru second func improv --- conftest.py | 10 +++++----- src/PIL/ImageCms.py | 28 ++++++++++++++++++++-------- src/PIL/McIdasImagePlugin.py | 19 ++++++++++++++----- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/conftest.py b/conftest.py index 4d99059ea..260330c28 100644 --- a/conftest.py +++ b/conftest.py @@ -4,6 +4,8 @@ import sys from PIL import Image from PIL import PdfParser +from PIL import ImageCms +from PIL import McIdasImagePlugin pytest_plugins = ["Tests.helper"] @@ -12,8 +14,8 @@ def calculate_coverage(test_name): all_branches = { "branches1": Image.branches, "branches2": PdfParser.XrefTable.branches, - "branches3": ImageCms.branches, - "branches4": McIdasImagePlugin.branches, + "branches3": ImageCms.ImageCmsProfile.branches, + "branches4": McIdasImagePlugin.McIdasImageFile.branches, # Add more } @@ -47,6 +49,4 @@ def pytest_sessionfinish(session, exitstatus): global test_name coverage = calculate_coverage(test_name) - print("\nBRANCH COVERAGE for", test_name, ":", coverage, "%\n") - - \ No newline at end of file + print("\nBRANCH COVERAGE for", test_name, ":", coverage, "%\n") \ No newline at end of file diff --git a/src/PIL/ImageCms.py b/src/PIL/ImageCms.py index 31ed11629..0b2702d70 100644 --- a/src/PIL/ImageCms.py +++ b/src/PIL/ImageCms.py @@ -248,6 +248,17 @@ _FLAGS = { class ImageCmsProfile: + branches = { + "1": False, + "2": False, + "3": False, + "4": False, + "5": False, + "6": False, + "7": False, + "8": False, + } + def __init__(self, profile: str | SupportsRead[bytes] | core.CmsProfile) -> None: """ :param profile: Either a string representing a filename, @@ -257,31 +268,32 @@ class ImageCmsProfile: """ if isinstance(profile, str): - branches["1"] = True + ImageCmsProfile.branches["1"] = True if sys.platform == "win32": - branches["2"] = True + ImageCmsProfile.branches["2"] = True profile_bytes_path = profile.encode() try: - branches["3"] = True + ImageCmsProfile.branches["3"] = True profile_bytes_path.decode("ascii") except UnicodeDecodeError: - branches["4"] = True + ImageCmsProfile.branches["4"] = True with open(profile, "rb") as f: - branches["5"] = True + ImageCmsProfile.branches["5"] = True self._set(core.profile_frombytes(f.read())) return self._set(core.profile_open(profile), profile) elif hasattr(profile, "read"): - branches["6"] = True + ImageCmsProfile.branches["6"] = True self._set(core.profile_frombytes(profile.read())) elif isinstance(profile, core.CmsProfile): - branches["7"] = True + ImageCmsProfile.branches["7"] = True self._set(profile) else: - branches["8"] = True + ImageCmsProfile.branches["8"] = True msg = "Invalid type for Profile" # type: ignore[unreachable] raise TypeError(msg) + def _set(self, profile: core.CmsProfile, filename: str | None = None) -> None: self.profile = profile self.filename = filename diff --git a/src/PIL/McIdasImagePlugin.py b/src/PIL/McIdasImagePlugin.py index 6d8273894..296e818ed 100644 --- a/src/PIL/McIdasImagePlugin.py +++ b/src/PIL/McIdasImagePlugin.py @@ -38,6 +38,14 @@ def _accept(prefix: bytes) -> bool: class McIdasImageFile(ImageFile.ImageFile): + branches = { + "1": False, + "2": False, + "3": False, + "4": False, + "5": False, + } + format = "MCIDAS" format_description = "McIdas area file" @@ -47,7 +55,7 @@ class McIdasImageFile(ImageFile.ImageFile): s = self.fp.read(256) if not _accept(s) or len(s) != 256: - branches["1"] = True + McIdasImageFile.branches["1"] = True msg = "not an McIdas area file" raise SyntaxError(msg) @@ -56,20 +64,20 @@ class McIdasImageFile(ImageFile.ImageFile): # get mode if w[11] == 1: - branches["2"] = True + McIdasImageFile.branches["2"] = True mode = rawmode = "L" elif w[11] == 2: - branches["3"] = True + McIdasImageFile.branches["3"] = True # FIXME: add memory map support mode = "I" rawmode = "I;16B" elif w[11] == 4: - branches["4"] = True + McIdasImageFile.branches["4"] = True # FIXME: add memory map support mode = "I" rawmode = "I;32B" else: - branches["5"] = True + McIdasImageFile.branches["5"] = True msg = "unsupported McIdas format" raise SyntaxError(msg) @@ -88,3 +96,4 @@ class McIdasImageFile(ImageFile.ImageFile): Image.register_open(McIdasImageFile.format, McIdasImageFile, _accept) # no default extension +