Skip to content

Commit

Permalink
Merge pull request #5261 from radarhere/context_managers
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Feb 13, 2021
2 parents 20329f3 + a1b4b02 commit fdd8b68
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 29 deletions.
3 changes: 2 additions & 1 deletion Tests/check_icns_dos.py
Expand Up @@ -5,4 +5,5 @@

from PIL import Image

Image.open(BytesIO(b"icns\x00\x00\x00\x10hang\x00\x00\x00\x00"))
with Image.open(BytesIO(b"icns\x00\x00\x00\x10hang\x00\x00\x00\x00")):
pass
5 changes: 4 additions & 1 deletion Tests/check_j2k_dos.py
Expand Up @@ -5,4 +5,7 @@

from PIL import Image

Image.open(BytesIO(b"\x00\x00\x00\x0cjP\x20\x20\x0d\x0a\x87\x0a\x00\x00\x00\x00hang"))
with Image.open(
BytesIO(b"\x00\x00\x00\x0cjP\x20\x20\x0d\x0a\x87\x0a\x00\x00\x00\x00hang")
):
pass
9 changes: 6 additions & 3 deletions Tests/test_decompression_bomb.py
Expand Up @@ -54,15 +54,18 @@ def test_exception(self):

def test_exception_ico(self):
with pytest.raises(Image.DecompressionBombError):
Image.open("Tests/images/decompression_bomb.ico")
with Image.open("Tests/images/decompression_bomb.ico"):
pass

def test_exception_gif(self):
with pytest.raises(Image.DecompressionBombError):
Image.open("Tests/images/decompression_bomb.gif")
with Image.open("Tests/images/decompression_bomb.gif"):
pass

def test_exception_bmp(self):
with pytest.raises(Image.DecompressionBombError):
Image.open("Tests/images/bmp/b/reallybig.bmp")
with Image.open("Tests/images/bmp/b/reallybig.bmp"):
pass


class TestDecompressionCrop:
Expand Down
9 changes: 6 additions & 3 deletions Tests/test_file_dds.py
Expand Up @@ -123,7 +123,8 @@ def test_dx10_r8g8b8a8_unorm_srgb():

def test_unimplemented_dxgi_format():
with pytest.raises(NotImplementedError):
Image.open("Tests/images/unimplemented_dxgi_format.dds")
with Image.open("Tests/images/unimplemented_dxgi_format.dds"):
pass


def test_uncompressed_rgb():
Expand Down Expand Up @@ -170,7 +171,8 @@ def test_short_header():
img_file = f.read()

def short_header():
Image.open(BytesIO(img_file[:119]))
with Image.open(BytesIO(img_file[:119])):
pass # pragma: no cover

with pytest.raises(OSError):
short_header()
Expand All @@ -192,4 +194,5 @@ def short_file():

def test_unimplemented_pixel_format():
with pytest.raises(NotImplementedError):
Image.open("Tests/images/unimplemented_pixel_format.dds")
with Image.open("Tests/images/unimplemented_pixel_format.dds"):
pass
3 changes: 2 additions & 1 deletion Tests/test_file_fpx.py
Expand Up @@ -21,4 +21,5 @@ def test_invalid_file():

def test_fpx_invalid_number_of_bands():
with pytest.raises(OSError, match="Invalid number of bands"):
Image.open("Tests/images/input_bw_five_bands.fpx")
with Image.open("Tests/images/input_bw_five_bands.fpx"):
pass
3 changes: 2 additions & 1 deletion Tests/test_file_jpeg.py
Expand Up @@ -798,7 +798,8 @@ def read(n=-1):

buffer.read = read
with pytest.raises(UnidentifiedImageError):
Image.open(buffer)
with Image.open(buffer):
pass

# Assert the entire file has not been read
assert 0 < buffer.max_pos < size
Expand Down
3 changes: 2 additions & 1 deletion Tests/test_file_jpeg2k.py
Expand Up @@ -219,7 +219,8 @@ def test_16bit_jp2_roundtrips():
def test_unbound_local():
# prepatch, a malformed jp2 file could cause an UnboundLocalError exception.
with pytest.raises(OSError):
Image.open("Tests/images/unbound_variable.jp2")
with Image.open("Tests/images/unbound_variable.jp2"):
pass


def test_parser_feed():
Expand Down
6 changes: 4 additions & 2 deletions Tests/test_file_png.py
Expand Up @@ -106,7 +106,8 @@ def test_broken(self):

test_file = "Tests/images/broken.png"
with pytest.raises(OSError):
Image.open(test_file)
with Image.open(test_file):
pass

def test_bad_text(self):
# Make sure PIL can read malformed tEXt chunks (@PIL152)
Expand Down Expand Up @@ -464,7 +465,8 @@ def test_scary(self):

pngfile = BytesIO(data)
with pytest.raises(OSError):
Image.open(pngfile)
with Image.open(pngfile):
pass

def test_trns_rgb(self):
# Check writing and reading of tRNS chunks for RGB images.
Expand Down
6 changes: 4 additions & 2 deletions Tests/test_file_ppm.py
Expand Up @@ -56,7 +56,8 @@ def test_truncated_file(tmp_path):
f.write("P6")

with pytest.raises(ValueError):
Image.open(path)
with Image.open(path):
pass


def test_neg_ppm():
Expand All @@ -66,7 +67,8 @@ def test_neg_ppm():
# sizes.

with pytest.raises(OSError):
Image.open("Tests/images/negative_size.ppm")
with Image.open("Tests/images/negative_size.ppm"):
pass


def test_mimetypes(tmp_path):
Expand Down
3 changes: 2 additions & 1 deletion Tests/test_file_psd.py
Expand Up @@ -128,4 +128,5 @@ def test_combined_larger_than_size():
# If we instead take the 'size' of the extra data field as the source of truth,
# then the seek can't be negative
with pytest.raises(OSError):
Image.open("Tests/images/combined_larger_than_size.psd")
with Image.open("Tests/images/combined_larger_than_size.psd"):
pass
3 changes: 2 additions & 1 deletion Tests/test_file_spider.py
Expand Up @@ -136,7 +136,8 @@ def test_invalid_file():
invalid_file = "Tests/images/invalid.spider"

with pytest.raises(OSError):
Image.open(invalid_file)
with Image.open(invalid_file):
pass


def test_nonstack_file():
Expand Down
11 changes: 6 additions & 5 deletions Tests/test_file_tiff.py
Expand Up @@ -163,9 +163,8 @@ def test_save_dpi_rounding(self, tmp_path):

def test_save_setting_missing_resolution(self):
b = BytesIO()
Image.open("Tests/images/10ct_32bit_128.tiff").save(
b, format="tiff", resolution=123.45
)
with Image.open("Tests/images/10ct_32bit_128.tiff") as im:
im.save(b, format="tiff", resolution=123.45)
with Image.open(b) as im:
assert float(im.tag_v2[X_RESOLUTION]) == 123.45
assert float(im.tag_v2[Y_RESOLUTION]) == 123.45
Expand Down Expand Up @@ -248,7 +247,8 @@ def test_32bit_float(self):

def test_unknown_pixel_mode(self):
with pytest.raises(OSError):
Image.open("Tests/images/hopper_unknown_pixel_mode.tif")
with Image.open("Tests/images/hopper_unknown_pixel_mode.tif"):
pass

def test_n_frames(self):
for path, n_frames in [
Expand Down Expand Up @@ -605,7 +605,8 @@ def test_close_on_load_nonexclusive(self, tmp_path):
def test_string_dimension(self):
# Assert that an error is raised if one of the dimensions is a string
with pytest.raises(ValueError):
Image.open("Tests/images/string_dimension.tiff")
with Image.open("Tests/images/string_dimension.tiff"):
pass


@pytest.mark.skipif(not is_win32(), reason="Windows only")
Expand Down
3 changes: 2 additions & 1 deletion Tests/test_file_webp.py
Expand Up @@ -165,7 +165,8 @@ def test_background_from_gif(self, tmp_path):

# Save as GIF
out_gif = str(tmp_path / "temp.gif")
Image.open(out_webp).save(out_gif)
with Image.open(out_webp) as im:
im.save(out_gif)

with Image.open(out_gif) as reread:
reread_value = reread.convert("RGB").getpixel((1, 1))
Expand Down
15 changes: 10 additions & 5 deletions Tests/test_image.py
Expand Up @@ -92,11 +92,13 @@ def test_open_formats(self):
JPGFILE = "Tests/images/hopper.jpg"

with pytest.raises(TypeError):
Image.open(PNGFILE, formats=123)
with Image.open(PNGFILE, formats=123):
pass

for formats in [["JPEG"], ("JPEG",), ["jpeg"], ["Jpeg"], ["jPeG"], ["JpEg"]]:
with pytest.raises(UnidentifiedImageError):
Image.open(PNGFILE, formats=formats)
with Image.open(PNGFILE, formats=formats):
pass

with Image.open(JPGFILE, formats=formats) as im:
assert im.mode == "RGB"
Expand All @@ -120,15 +122,18 @@ def test_invalid_image(self):

im = io.BytesIO(b"")
with pytest.raises(UnidentifiedImageError):
Image.open(im)
with Image.open(im):
pass

def test_bad_mode(self):
with pytest.raises(ValueError):
Image.open("filename", "bad mode")
with Image.open("filename", "bad mode"):
pass

def test_stringio(self):
with pytest.raises(ValueError):
Image.open(io.StringIO())
with Image.open(io.StringIO()):
pass

def test_pathlib(self, tmp_path):
from PIL.Image import Path
Expand Down
3 changes: 2 additions & 1 deletion Tests/test_imagewin_pointers.py
Expand Up @@ -110,4 +110,5 @@ def test_pointer(tmp_path):
DeleteObject(dib)
DeleteDC(hdc)

Image.open(BytesIO(bitmap)).save(opath)
with Image.open(BytesIO(bitmap)) as im:
im.save(opath)

0 comments on commit fdd8b68

Please sign in to comment.