From 83542c42bf6ffeb944f965d811ad68074d5d2a19 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 11 Feb 2021 21:43:54 +1100 Subject: [PATCH 1/2] Added context managers --- Tests/check_icns_dos.py | 3 ++- Tests/check_j2k_dos.py | 5 ++++- Tests/test_decompression_bomb.py | 9 ++++++--- Tests/test_file_dds.py | 9 ++++++--- Tests/test_file_fpx.py | 3 ++- Tests/test_file_jpeg.py | 3 ++- Tests/test_file_jpeg2k.py | 3 ++- Tests/test_file_png.py | 6 ++++-- Tests/test_file_ppm.py | 6 ++++-- Tests/test_file_psd.py | 3 ++- Tests/test_file_spider.py | 3 ++- Tests/test_file_tiff.py | 11 ++++++----- Tests/test_file_webp.py | 3 ++- Tests/test_image.py | 15 ++++++++++----- Tests/test_imagewin_pointers.py | 3 ++- 15 files changed, 56 insertions(+), 29 deletions(-) diff --git a/Tests/check_icns_dos.py b/Tests/check_icns_dos.py index 3f4fb6518e2..a34bee45c51 100644 --- a/Tests/check_icns_dos.py +++ b/Tests/check_icns_dos.py @@ -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 diff --git a/Tests/check_j2k_dos.py b/Tests/check_j2k_dos.py index 273c18585e8..71dcea4f39f 100644 --- a/Tests/check_j2k_dos.py +++ b/Tests/check_j2k_dos.py @@ -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 diff --git a/Tests/test_decompression_bomb.py b/Tests/test_decompression_bomb.py index 7671cdc09b8..80ab92666ac 100644 --- a/Tests/test_decompression_bomb.py +++ b/Tests/test_decompression_bomb.py @@ -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: diff --git a/Tests/test_file_dds.py b/Tests/test_file_dds.py index 1cd7a1be75c..5e2e841fd9d 100644 --- a/Tests/test_file_dds.py +++ b/Tests/test_file_dds.py @@ -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(): @@ -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 with pytest.raises(OSError): short_header() @@ -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 diff --git a/Tests/test_file_fpx.py b/Tests/test_file_fpx.py index c3cc37ddf00..818565f88b3 100644 --- a/Tests/test_file_fpx.py +++ b/Tests/test_file_fpx.py @@ -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 diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 435ecbaa7ad..5862d7eba27 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -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 diff --git a/Tests/test_file_jpeg2k.py b/Tests/test_file_jpeg2k.py index c9e37f8b048..e22cff087b6 100644 --- a/Tests/test_file_jpeg2k.py +++ b/Tests/test_file_jpeg2k.py @@ -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(): diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 289c0976794..76eac58f856 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -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) @@ -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. diff --git a/Tests/test_file_ppm.py b/Tests/test_file_ppm.py index e7c3fb06ff7..c7be5d7805f 100644 --- a/Tests/test_file_ppm.py +++ b/Tests/test_file_ppm.py @@ -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(): @@ -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): diff --git a/Tests/test_file_psd.py b/Tests/test_file_psd.py index 8bb45630ed9..8028636b938 100644 --- a/Tests/test_file_psd.py +++ b/Tests/test_file_psd.py @@ -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 diff --git a/Tests/test_file_spider.py b/Tests/test_file_spider.py index 9cdb451c976..b2d815d2ed6 100644 --- a/Tests/test_file_spider.py +++ b/Tests/test_file_spider.py @@ -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(): diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index f644ef8870e..945f245ccf6 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -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 @@ -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 [ @@ -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") diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index 11fbd9fd579..7cc46ef6953 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -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)) diff --git a/Tests/test_image.py b/Tests/test_image.py index 3c2d128ee73..c15c580e978 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -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" @@ -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 diff --git a/Tests/test_imagewin_pointers.py b/Tests/test_imagewin_pointers.py index a5cac96e42c..c51a66089c8 100644 --- a/Tests/test_imagewin_pointers.py +++ b/Tests/test_imagewin_pointers.py @@ -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) From a1b4b026ffdb3196f0e06a32abbb9bc04a986a4a Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 14 Feb 2021 07:58:16 +1100 Subject: [PATCH 2/2] Added pragma no cover --- Tests/test_file_dds.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/test_file_dds.py b/Tests/test_file_dds.py index 5e2e841fd9d..37869288fbc 100644 --- a/Tests/test_file_dds.py +++ b/Tests/test_file_dds.py @@ -172,7 +172,7 @@ def test_short_header(): def short_header(): with Image.open(BytesIO(img_file[:119])): - pass + pass # pragma: no cover with pytest.raises(OSError): short_header()