Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use functools.cached_property in GifImagePlugin #8037

Merged
merged 1 commit into from
May 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions src/PIL/GifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import os
import subprocess
from enum import IntEnum
from functools import cached_property

from . import (
Image,
Expand Down Expand Up @@ -112,8 +113,7 @@ def _open(self) -> None:

self._fp = self.fp # FIXME: hack
self.__rewind = self.fp.tell()
self._n_frames = None
self._is_animated = None
self._n_frames: int | None = None
self._seek(0) # get ready to read first frame

@property
Expand All @@ -128,24 +128,23 @@ def n_frames(self):
self.seek(current)
return self._n_frames

@property
def is_animated(self):
if self._is_animated is None:
if self._n_frames is not None:
self._is_animated = self._n_frames != 1
else:
current = self.tell()
if current:
self._is_animated = True
else:
try:
self._seek(1, False)
self._is_animated = True
except EOFError:
self._is_animated = False

self.seek(current)
return self._is_animated
@cached_property
def is_animated(self) -> bool:
if self._n_frames is not None:
return self._n_frames != 1

current = self.tell()
if current:
return True

try:
self._seek(1, False)
is_animated = True
except EOFError:
is_animated = False

self.seek(current)
return is_animated

def seek(self, frame: int) -> None:
if not self._seek_check(frame):
Expand Down
Loading