From 2e6bc546eeae57613ba86ea65ccb24ad2016e4b7 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 8 Sep 2024 06:48:43 +1000 Subject: [PATCH] Moved Buffer into _typing --- src/PIL/GifImagePlugin.py | 16 ++++------------ src/PIL/TiffImagePlugin.py | 10 +--------- src/PIL/_typing.py | 5 +++++ 3 files changed, 10 insertions(+), 21 deletions(-) diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index a27f7aae9dc..87d2420dc9e 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -49,6 +49,7 @@ if TYPE_CHECKING: from . import _imaging + from ._typing import Buffer class LoadingStrategy(IntEnum): @@ -1157,18 +1158,9 @@ def getdata( class Collector(BytesIO): data = [] - if sys.version_info >= (3, 12): - from collections.abc import Buffer - - def write(self, data: Buffer) -> int: - self.data.append(data) - return len(data) - - else: - - def write(self, data: Any) -> int: - self.data.append(data) - return len(data) + def write(self, data: Buffer) -> int: + self.data.append(data) + return len(data) im.load() # make sure raster data is available diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index 53f6ae621c7..726c556614e 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -63,7 +63,7 @@ from .TiffTags import TYPES if TYPE_CHECKING: - from ._typing import IntegralLike + from ._typing import Buffer, IntegralLike logger = logging.getLogger(__name__) @@ -2108,17 +2108,9 @@ def skipIFDs(self) -> None: num_tags = self.readShort() self.f.seek(num_tags * 12, os.SEEK_CUR) - if sys.version_info >= (3, 12): - 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: (value,) = struct.unpack(self.shortFmt, self.f.read(2)) return value diff --git a/src/PIL/_typing.py b/src/PIL/_typing.py index 093778464da..f3e7367fe00 100644 --- a/src/PIL/_typing.py +++ b/src/PIL/_typing.py @@ -15,6 +15,11 @@ except (ImportError, AttributeError): pass +if sys.version_info >= (3, 12): + from collections.abc import Buffer +else: + Buffer = Any + if sys.version_info >= (3, 10): from typing import TypeGuard else: