Skip to content

Commit

Permalink
Moved Buffer into _typing
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Sep 7, 2024
1 parent 1105256 commit f6f9c8c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 25 deletions.
17 changes: 4 additions & 13 deletions src/PIL/GifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import math
import os
import subprocess
import sys
from enum import IntEnum
from functools import cached_property
from typing import IO, TYPE_CHECKING, Any, Literal, NamedTuple, Union
Expand All @@ -49,6 +48,7 @@

if TYPE_CHECKING:
from . import _imaging
from ._typing import Buffer


class LoadingStrategy(IntEnum):
Expand Down Expand Up @@ -1157,18 +1157,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

Expand Down
15 changes: 3 additions & 12 deletions src/PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import math
import os
import struct
import sys
import warnings
from collections.abc import Iterator, MutableMapping
from fractions import Fraction
Expand All @@ -63,7 +62,7 @@
from .TiffTags import TYPES

if TYPE_CHECKING:
from ._typing import IntegralLike
from ._typing import Buffer, IntegralLike

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -2108,16 +2107,8 @@ 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 write(self, data: Buffer, /) -> int:
return self.f.write(data)

def readShort(self) -> int:
(value,) = struct.unpack(self.shortFmt, self.f.read(2))
Expand Down
5 changes: 5 additions & 0 deletions src/PIL/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit f6f9c8c

Please sign in to comment.