Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Tests/test_file_jpeg2k.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def test_reduce() -> None:
with Image.open("Tests/images/test-card-lossless.jp2") as im:
assert callable(im.reduce)

im.reduce = 2
im.reduce = 2 # type: ignore[assignment, method-assign]
assert im.reduce == 2

im.load()
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_file_png.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ class MyStdOut:
monkeypatch.setattr(sys, "stdout", mystdout)

with Image.open(TEST_PNG_FILE) as im:
im.save(sys.stdout, "PNG")
im.save(sys.stdout, "PNG") # type: ignore[arg-type]

if isinstance(mystdout, MyStdOut):
mystdout = mystdout.buffer
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_file_ppm.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ class MyStdOut:
monkeypatch.setattr(sys, "stdout", mystdout)

with Image.open(TEST_FILE) as im:
im.save(sys.stdout, "PPM")
im.save(sys.stdout, "PPM") # type: ignore[arg-type]

if isinstance(mystdout, MyStdOut):
mystdout = mystdout.buffer
Expand Down
4 changes: 2 additions & 2 deletions Tests/test_image_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,14 @@ def test_blank_fill(self) -> None:
def test_missing_method_data(self) -> None:
with hopper() as im:
with pytest.raises(ValueError):
im.transform((100, 100), None)
im.transform((100, 100), None) # type: ignore[arg-type]

@pytest.mark.parametrize("resample", (Image.Resampling.BOX, "unknown"))
def test_unknown_resampling_filter(self, resample: Image.Resampling | str) -> None:
with hopper() as im:
(w, h) = im.size
with pytest.raises(ValueError):
im.transform((100, 100), Image.Transform.EXTENT, (0, 0, w, h), resample)
im.transform((100, 100), Image.Transform.EXTENT, (0, 0, w, h), resample) # type: ignore[arg-type]


class TestImageTransformAffine:
Expand Down
11 changes: 3 additions & 8 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,16 +590,11 @@ def _new(self, im: core.ImagingCore) -> Image:
return new

# Context manager support
def __enter__(self):
def __enter__(self) -> Image:
return self

def __exit__(self, *args):
from . import ImageFile

if isinstance(self, ImageFile.ImageFile):
if getattr(self, "_exclusive_fp", False):
self._close_fp()
self.fp = None
def __exit__(self, *args: object) -> None:
pass

def close(self) -> None:
"""
Expand Down
12 changes: 11 additions & 1 deletion src/PIL/ImageFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def __init__(
self.decoderconfig: tuple[Any, ...] = ()
self.decodermaxblock = MAXBLOCK

self.fp: IO[bytes] | None
self._fp: IO[bytes] | DeferredError
if is_path(fp):
# filename
Expand Down Expand Up @@ -168,6 +169,10 @@ def __init__(
def _open(self) -> None:
pass

# Context manager support
def __enter__(self) -> ImageFile:
return self

def _close_fp(self) -> None:
if getattr(self, "_fp", False) and not isinstance(self._fp, DeferredError):
if self._fp != self.fp:
Expand All @@ -176,6 +181,11 @@ def _close_fp(self) -> None:
if self.fp:
self.fp.close()

def __exit__(self, *args: object) -> None:
if getattr(self, "_exclusive_fp", False):
self._close_fp()
self.fp = None

def close(self) -> None:
"""
Closes the file pointer, if possible.
Expand Down Expand Up @@ -268,7 +278,7 @@ def verify(self) -> None:

# raise exception if something's wrong. must be called
# directly after open, and closes file when finished.
if self._exclusive_fp:
if self._exclusive_fp and self.fp:
self.fp.close()
self.fp = None

Expand Down
Loading