Skip to content

Commit 03dc994

Browse files
authored
Check that _fp type is not DeferredError before use (#8640)
1 parent 6bffa3a commit 03dc994

10 files changed

+32
-2
lines changed

src/PIL/DcxImagePlugin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
from . import Image
2626
from ._binary import i32le as i32
27+
from ._util import DeferredError
2728
from .PcxImagePlugin import PcxImageFile
2829

2930
MAGIC = 0x3ADE68B1 # QUIZ: what's this value, then?
@@ -66,6 +67,8 @@ def _open(self) -> None:
6667
def seek(self, frame: int) -> None:
6768
if not self._seek_check(frame):
6869
return
70+
if isinstance(self._fp, DeferredError):
71+
raise self._fp.ex
6972
self.frame = frame
7073
self.fp = self._fp
7174
self.fp.seek(self._offset[frame])

src/PIL/FliImagePlugin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from ._binary import i16le as i16
2323
from ._binary import i32le as i32
2424
from ._binary import o8
25+
from ._util import DeferredError
2526

2627
#
2728
# decoder
@@ -134,6 +135,8 @@ def seek(self, frame: int) -> None:
134135
self._seek(f)
135136

136137
def _seek(self, frame: int) -> None:
138+
if isinstance(self._fp, DeferredError):
139+
raise self._fp.ex
137140
if frame == 0:
138141
self.__frame = -1
139142
self._fp.seek(self.__rewind)

src/PIL/GifImagePlugin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from ._binary import i16le as i16
4646
from ._binary import o8
4747
from ._binary import o16le as o16
48+
from ._util import DeferredError
4849

4950
if TYPE_CHECKING:
5051
from . import _imaging
@@ -167,6 +168,8 @@ def seek(self, frame: int) -> None:
167168
raise EOFError(msg) from e
168169

169170
def _seek(self, frame: int, update_image: bool = True) -> None:
171+
if isinstance(self._fp, DeferredError):
172+
raise self._fp.ex
170173
if frame == 0:
171174
# rewind
172175
self.__offset = 0

src/PIL/ImImagePlugin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from typing import IO, Any
3232

3333
from . import Image, ImageFile, ImagePalette
34+
from ._util import DeferredError
3435

3536
# --------------------------------------------------------------------
3637
# Standard tags
@@ -290,6 +291,8 @@ def is_animated(self) -> bool:
290291
def seek(self, frame: int) -> None:
291292
if not self._seek_check(frame):
292293
return
294+
if isinstance(self._fp, DeferredError):
295+
raise self._fp.ex
293296

294297
self.frame = frame
295298

src/PIL/ImageFile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def _open(self) -> None:
167167
pass
168168

169169
def _close_fp(self):
170-
if getattr(self, "_fp", False):
170+
if getattr(self, "_fp", False) and not isinstance(self._fp, DeferredError):
171171
if self._fp != self.fp:
172172
self._fp.close()
173173
self._fp = DeferredError(ValueError("Operation on closed image"))

src/PIL/MpoImagePlugin.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
TiffImagePlugin,
3333
)
3434
from ._binary import o32le
35+
from ._util import DeferredError
3536

3637

3738
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
@@ -125,11 +126,15 @@ def _after_jpeg_open(self, mpheader: dict[int, Any] | None = None) -> None:
125126
self.readonly = 1
126127

127128
def load_seek(self, pos: int) -> None:
129+
if isinstance(self._fp, DeferredError):
130+
raise self._fp.ex
128131
self._fp.seek(pos)
129132

130133
def seek(self, frame: int) -> None:
131134
if not self._seek_check(frame):
132135
return
136+
if isinstance(self._fp, DeferredError):
137+
raise self._fp.ex
133138
self.fp = self._fp
134139
self.offset = self.__mpoffsets[frame]
135140

src/PIL/PngImagePlugin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
from ._binary import o8
4949
from ._binary import o16be as o16
5050
from ._binary import o32be as o32
51+
from ._util import DeferredError
5152

5253
if TYPE_CHECKING:
5354
from . import _imaging
@@ -869,6 +870,8 @@ def seek(self, frame: int) -> None:
869870

870871
def _seek(self, frame: int, rewind: bool = False) -> None:
871872
assert self.png is not None
873+
if isinstance(self._fp, DeferredError):
874+
raise self._fp.ex
872875

873876
self.dispose: _imaging.ImagingCore | None
874877
dispose_extent = None

src/PIL/PsdImagePlugin.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from ._binary import i32be as i32
2828
from ._binary import si16be as si16
2929
from ._binary import si32be as si32
30+
from ._util import DeferredError
3031

3132
MODES = {
3233
# (photoshop mode, bits) -> (pil mode, required channels)
@@ -148,6 +149,8 @@ def layers(
148149
) -> list[tuple[str, str, tuple[int, int, int, int], list[ImageFile._Tile]]]:
149150
layers = []
150151
if self._layers_position is not None:
152+
if isinstance(self._fp, DeferredError):
153+
raise self._fp.ex
151154
self._fp.seek(self._layers_position)
152155
_layer_data = io.BytesIO(ImageFile._safe_read(self._fp, self._layers_size))
153156
layers = _layerinfo(_layer_data, self._layers_size)
@@ -167,6 +170,8 @@ def is_animated(self) -> bool:
167170
def seek(self, layer: int) -> None:
168171
if not self._seek_check(layer):
169172
return
173+
if isinstance(self._fp, DeferredError):
174+
raise self._fp.ex
170175

171176
# seek to given layer (1..max)
172177
_, mode, _, tile = self.layers[layer - 1]

src/PIL/SpiderImagePlugin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from typing import IO, TYPE_CHECKING, Any, cast
4141

4242
from . import Image, ImageFile
43+
from ._util import DeferredError
4344

4445

4546
def isInt(f: Any) -> int:
@@ -178,6 +179,8 @@ def seek(self, frame: int) -> None:
178179
raise EOFError(msg)
179180
if not self._seek_check(frame):
180181
return
182+
if isinstance(self._fp, DeferredError):
183+
raise self._fp.ex
181184
self.stkoffset = self.hdrlen + frame * (self.hdrlen + self.imgbytes)
182185
self.fp = self._fp
183186
self.fp.seek(self.stkoffset)

src/PIL/TiffImagePlugin.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
from ._binary import o8
5959
from ._deprecate import deprecate
6060
from ._typing import StrOrBytesPath
61-
from ._util import is_path
61+
from ._util import DeferredError, is_path
6262
from .TiffTags import TYPES
6363

6464
if TYPE_CHECKING:
@@ -1222,6 +1222,8 @@ def seek(self, frame: int) -> None:
12221222
self._im = None
12231223

12241224
def _seek(self, frame: int) -> None:
1225+
if isinstance(self._fp, DeferredError):
1226+
raise self._fp.ex
12251227
self.fp = self._fp
12261228

12271229
while len(self._frame_pos) <= frame:

0 commit comments

Comments
 (0)