Skip to content

Commit fe7c4e5

Browse files
committed
Assert fp is not None
1 parent 8494b06 commit fe7c4e5

37 files changed

+63
-3
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_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: ImageFile.ImageFile) -> 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: ImageFile.ImageFile) -> 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
@@ -1133,8 +1133,9 @@ def test_fd_leak(self, tmp_path: Path) -> None:
11331133
im.save(tmpfile)
11341134

11351135
im = Image.open(tmpfile)
1136+
assert im.fp is not None
1137+
assert not im.fp.closed
11361138
fp = im.fp
1137-
assert not fp.closed
11381139
with pytest.raises(OSError):
11391140
os.remove(tmpfile)
11401141
im.load()

Tests/test_file_libtiff.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,8 +610,9 @@ def test_bw_compression_w_rgb(self, compression: str, tmp_path: Path) -> None:
610610
im.save(out, compression=compression)
611611

612612
def test_fp_leak(self) -> None:
613-
im: Image.Image | None = Image.open("Tests/images/hopper_g4_500.tif")
613+
im: ImageFile.ImageFile | None = Image.open("Tests/images/hopper_g4_500.tif")
614614
assert im is not None
615+
assert im.fp is not None
615616
fn = im.fp.fileno()
616617

617618
os.fstat(fn)

Tests/test_file_tiff.py

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

972972
im = Image.open(tmpfile)
973973
fp = im.fp
974+
assert fp is not None
974975
assert not fp.closed
975976
im.load()
976977
assert fp.closed
@@ -984,6 +985,7 @@ def test_close_on_load_nonexclusive(self, tmp_path: Path) -> None:
984985
with open(tmpfile, "rb") as f:
985986
im = Image.open(f)
986987
fp = im.fp
988+
assert fp is not None
987989
assert not fp.closed
988990
im.load()
989991
assert not fp.closed
@@ -1034,8 +1036,9 @@ def test_fd_leak(self, tmp_path: Path) -> None:
10341036
im.save(tmpfile)
10351037

10361038
im = Image.open(tmpfile)
1039+
assert im.fp is not None
1040+
assert not im.fp.closed
10371041
fp = im.fp
1038-
assert not fp.closed
10391042
with pytest.raises(OSError):
10401043
os.remove(tmpfile)
10411044
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,

src/PIL/BlpImagePlugin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ class BlpImageFile(ImageFile.ImageFile):
258258
format_description = "Blizzard Mipmap Format"
259259

260260
def _open(self) -> None:
261+
assert self.fp is not None
261262
self.magic = self.fp.read(4)
262263
if not _accept(self.magic):
263264
msg = f"Bad BLP magic {repr(self.magic)}"

0 commit comments

Comments
 (0)