Skip to content

Commit

Permalink
Use @cached_property
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed May 22, 2024
1 parent d879f39 commit 33e304e
Showing 1 changed file with 19 additions and 20 deletions.
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

0 comments on commit 33e304e

Please sign in to comment.