Added type hints to TiffImagePlugin _save

This commit is contained in:
Andrew Murray 2024-09-06 21:33:30 +10:00
parent 06c331af5d
commit 93a0bf0276
2 changed files with 22 additions and 6 deletions

View File

@ -121,7 +121,7 @@ nitpicky = True
# generating warnings in “nitpicky mode”. Note that type should include the domain name # generating warnings in “nitpicky mode”. Note that type should include the domain name
# if present. Example entries would be ('py:func', 'int') or # if present. Example entries would be ('py:func', 'int') or
# ('envvar', 'LD_LIBRARY_PATH'). # ('envvar', 'LD_LIBRARY_PATH').
# nitpick_ignore = [] nitpick_ignore = [("py:class", "_io.BytesIO")]
# -- Options for HTML output ---------------------------------------------- # -- Options for HTML output ----------------------------------------------

View File

@ -46,6 +46,7 @@ import logging
import math import math
import os import os
import struct import struct
import sys
import warnings import warnings
from collections.abc import Iterator, MutableMapping from collections.abc import Iterator, MutableMapping
from fractions import Fraction from fractions import Fraction
@ -1647,7 +1648,7 @@ SAVE_INFO = {
} }
def _save(im: Image.Image, fp, filename: str | bytes) -> None: def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
try: try:
rawmode, prefix, photo, format, bits, extra = SAVE_INFO[im.mode] rawmode, prefix, photo, format, bits, extra = SAVE_INFO[im.mode]
except KeyError as e: except KeyError as e:
@ -1958,7 +1959,7 @@ def _save(im: Image.Image, fp, filename: str | bytes) -> None:
setattr(im, "_debug_multipage", ifd) setattr(im, "_debug_multipage", ifd)
class AppendingTiffWriter: class AppendingTiffWriter(io.BytesIO):
fieldSizes = [ fieldSizes = [
0, # None 0, # None
1, # byte 1, # byte
@ -2068,6 +2069,12 @@ class AppendingTiffWriter:
return self.f.tell() - self.offsetOfNewPage return self.f.tell() - self.offsetOfNewPage
def seek(self, offset: int, whence: int = io.SEEK_SET) -> int: def seek(self, offset: int, whence: int = io.SEEK_SET) -> int:
"""
:param offset: Distance to seek.
:param whence: Whether the distance is relative to the start,
end or current position.
:returns: The resulting position, relative to the start.
"""
if whence == os.SEEK_SET: if whence == os.SEEK_SET:
offset += self.offsetOfNewPage offset += self.offsetOfNewPage
@ -2101,8 +2108,16 @@ class AppendingTiffWriter:
num_tags = self.readShort() num_tags = self.readShort()
self.f.seek(num_tags * 12, os.SEEK_CUR) self.f.seek(num_tags * 12, os.SEEK_CUR)
def write(self, data: bytes) -> int | None: if sys.version_info >= (3, 12):
return self.f.write(data) from collections.abc import Buffer
def write(self, data: Buffer, /) -> int:
return self.f.write(data)
else:
def write(self, data: Any, /) -> int:
return self.f.write(data)
def readShort(self) -> int: def readShort(self) -> int:
(value,) = struct.unpack(self.shortFmt, self.f.read(2)) (value,) = struct.unpack(self.shortFmt, self.f.read(2))
@ -2143,7 +2158,8 @@ class AppendingTiffWriter:
def close(self) -> None: def close(self) -> None:
self.finalize() self.finalize()
self.f.close() if self.close_fp:
self.f.close()
def fixIFD(self) -> None: def fixIFD(self) -> None:
num_tags = self.readShort() num_tags = self.readShort()