Skip to content

Commit

Permalink
Added return type to ImageFile.load()
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Aug 5, 2024
1 parent 71876cf commit 3e30d92
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
8 changes: 3 additions & 5 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,10 @@ class Quantize(IntEnum):
# Registries

if TYPE_CHECKING:
import mmap
from xml.etree.ElementTree import Element

from . import ImageFile, ImagePalette, TiffImagePlugin
from . import ImageFile, ImageFilter, ImagePalette, TiffImagePlugin
from ._typing import NumpyArray, StrOrBytesPath, TypeGuard
ID: list[str] = []
OPEN: dict[
Expand Down Expand Up @@ -612,7 +613,7 @@ def close(self) -> None:
logger.debug("Error closing: %s", msg)

if getattr(self, "map", None):
self.map = None
self.map: mmap.mmap | None = None

# Instead of simply setting to None, we're setting up a
# deferred error that will better explain that the core image
Expand Down Expand Up @@ -1336,9 +1337,6 @@ def _expand(self, xmargin: int, ymargin: int | None = None) -> Image:
self.load()
return self._new(self.im.expand(xmargin, ymargin))

if TYPE_CHECKING:
from . import ImageFilter

def filter(self, filter: ImageFilter.Filter | type[ImageFilter.Filter]) -> Image:
"""
Filters this image using the given filter. For a list of
Expand Down
19 changes: 8 additions & 11 deletions src/PIL/ImageFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def verify(self) -> None:
self.fp.close()
self.fp = None

def load(self):
def load(self) -> Image.core.PixelAccess | None:
"""Load image data based on tile list"""

if self.tile is None:
Expand All @@ -185,25 +185,25 @@ def load(self):
if not self.tile:
return pixel

self.map = None
self.map: mmap.mmap | None = None
use_mmap = self.filename and len(self.tile) == 1
# As of pypy 2.1.0, memory mapping was failing here.
use_mmap = use_mmap and not hasattr(sys, "pypy_version_info")

readonly = 0

# look for read/seek overrides
try:
if hasattr(self, "load_read"):
read = self.load_read
# don't use mmap if there are custom read/seek functions
use_mmap = False
except AttributeError:
else:
read = self.fp.read

try:
if hasattr(self, "load_seek"):
seek = self.load_seek
use_mmap = False
except AttributeError:
else:
seek = self.fp.seek

if use_mmap:
Expand Down Expand Up @@ -243,11 +243,8 @@ def load(self):
# sort tiles in file order
self.tile.sort(key=_tilesort)

try:
# FIXME: This is a hack to handle TIFF's JpegTables tag.
prefix = self.tile_prefix
except AttributeError:
prefix = b""
# FIXME: This is a hack to handle TIFF's JpegTables tag.
prefix = getattr(self, "tile_prefix", b"")

# Remove consecutive duplicates that only differ by their offset
self.tile = [
Expand Down

0 comments on commit 3e30d92

Please sign in to comment.