Skip to content

Commit dcce582

Browse files
committed
Assert fp is not None
1 parent 0ba6961 commit dcce582

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+79
-4
lines changed

Tests/test_file_bufrstub.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def open(self, im: ImageFile.StubImageFile) -> None:
6161

6262
def load(self, im: ImageFile.StubImageFile) -> Image.Image:
6363
self.loaded = True
64+
assert im.fp is not None
6465
im.fp.close()
6566
return Image.new("RGB", (1, 1))
6667

Tests/test_file_cur.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def test_invalid_file() -> None:
2626
no_cursors_file = "Tests/images/no_cursors.cur"
2727

2828
cur = CurImagePlugin.CurImageFile(TEST_FILE)
29+
assert cur.fp is not None
2930
cur.fp.close()
3031
with open(no_cursors_file, "rb") as cur.fp:
3132
with pytest.raises(TypeError):

Tests/test_file_gribstub.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def open(self, im: Image.Image) -> None:
6161

6262
def load(self, im: Image.Image) -> Image.Image:
6363
self.loaded = True
64+
assert im.fp is not None
6465
im.fp.close()
6566
return Image.new("RGB", (1, 1))
6667

Tests/test_file_hdf5stub.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def open(self, im: Image.Image) -> None:
6363

6464
def load(self, im: Image.Image) -> Image.Image:
6565
self.loaded = True
66+
assert im.fp is not None
6667
im.fp.close()
6768
return Image.new("RGB", (1, 1))
6869

Tests/test_file_jpeg.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1111,8 +1111,9 @@ def test_fd_leak(self, tmp_path: Path) -> None:
11111111
im.save(tmpfile)
11121112

11131113
im = Image.open(tmpfile)
1114+
assert im.fp is not None
1115+
assert not im.fp.closed
11141116
fp = im.fp
1115-
assert not fp.closed
11161117
with pytest.raises(OSError):
11171118
os.remove(tmpfile)
11181119
im.load()

Tests/test_file_libtiff.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@
1111

1212
import pytest
1313

14-
from PIL import Image, ImageFilter, ImageOps, TiffImagePlugin, TiffTags, features
14+
from PIL import (
15+
Image,
16+
ImageFile,
17+
ImageFilter,
18+
ImageOps,
19+
TiffImagePlugin,
20+
TiffTags,
21+
features,
22+
)
1523
from PIL.TiffImagePlugin import OSUBFILETYPE, SAMPLEFORMAT, STRIPOFFSETS, SUBIFD
1624

1725
from .helper import (
@@ -564,8 +572,9 @@ def test_bw_compression_w_rgb(self, compression: str, tmp_path: Path) -> None:
564572
im.save(out, compression=compression)
565573

566574
def test_fp_leak(self) -> None:
567-
im: Image.Image | None = Image.open("Tests/images/hopper_g4_500.tif")
575+
im: ImageFile.ImageFile | None = Image.open("Tests/images/hopper_g4_500.tif")
568576
assert im is not None
577+
assert im.fp is not None
569578
fn = im.fp.fileno()
570579

571580
os.fstat(fn)

Tests/test_file_tiff.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,7 @@ def test_close_on_load_exclusive(self, tmp_path: Path) -> None:
957957

958958
im = Image.open(tmpfile)
959959
fp = im.fp
960+
assert fp is not None
960961
assert not fp.closed
961962
im.load()
962963
assert fp.closed
@@ -970,6 +971,7 @@ def test_close_on_load_nonexclusive(self, tmp_path: Path) -> None:
970971
with open(tmpfile, "rb") as f:
971972
im = Image.open(f)
972973
fp = im.fp
974+
assert fp is not None
973975
assert not fp.closed
974976
im.load()
975977
assert not fp.closed
@@ -1020,8 +1022,9 @@ def test_fd_leak(self, tmp_path: Path) -> None:
10201022
im.save(tmpfile)
10211023

10221024
im = Image.open(tmpfile)
1025+
assert im.fp is not None
1026+
assert not im.fp.closed
10231027
fp = im.fp
1024-
assert not fp.closed
10251028
with pytest.raises(OSError):
10261029
os.remove(tmpfile)
10271030
im.load()

Tests/test_image_load.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def test_close_after_load(caplog: pytest.LogCaptureFixture) -> None:
3838
def test_contextmanager() -> None:
3939
fn = None
4040
with Image.open("Tests/images/hopper.gif") as im:
41+
assert im.fp is not None
4142
fn = im.fp.fileno()
4243
os.fstat(fn)
4344

docs/example/DdsImagePlugin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ class DdsImageFile(ImageFile.ImageFile):
213213
format_description = "DirectDraw Surface"
214214

215215
def _open(self) -> None:
216+
assert self.fp is not None
216217
if not _accept(self.fp.read(4)):
217218
msg = "not a DDS file"
218219
raise SyntaxError(msg)

src/PIL/AvifImagePlugin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ def _open(self) -> None:
7777
):
7878
msg = "Invalid opening codec"
7979
raise ValueError(msg)
80+
81+
assert self.fp is not None
8082
self._decoder = _avif.AvifDecoder(
8183
self.fp.read(),
8284
DECODE_CODEC_CHOICE,

0 commit comments

Comments
 (0)