From 3af00edc85266f8be58bdb48f9964ad1743593f9 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 25 Nov 2021 23:16:07 +1100 Subject: [PATCH] Added context managers --- Tests/check_fli_oob.py | 10 +++++----- Tests/check_jp2_overflow.py | 10 +++++----- Tests/test_file_jpeg2k.py | 6 +++--- Tests/test_file_png.py | 4 ++-- Tests/test_image_resize.py | 4 ++-- Tests/test_imagechops.py | 24 ++++++++++++------------ Tests/test_pickle.py | 6 +++--- Tests/test_sgi_crash.py | 6 +++--- 8 files changed, 35 insertions(+), 35 deletions(-) diff --git a/Tests/check_fli_oob.py b/Tests/check_fli_oob.py index 6b63a68265c..7b3d4d7ee9d 100644 --- a/Tests/check_fli_oob.py +++ b/Tests/check_fli_oob.py @@ -61,8 +61,8 @@ for path in repro_ss2 + repro_lc + repro_advance + repro_brun + repro_copy: - im = Image.open(path) - try: - im.load() - except Exception as msg: - print(msg) + with Image.open(path) as im: + try: + im.load() + except Exception as msg: + print(msg) diff --git a/Tests/check_jp2_overflow.py b/Tests/check_jp2_overflow.py index f81a360cec8..0210505f5fe 100755 --- a/Tests/check_jp2_overflow.py +++ b/Tests/check_jp2_overflow.py @@ -19,8 +19,8 @@ repro = ("00r0_gray_l.jp2", "00r1_graya_la.jp2") for path in repro: - im = Image.open(path) - try: - im.load() - except Exception as msg: - print(msg) + with Image.open(path) as im: + try: + im.load() + except Exception as msg: + print(msg) diff --git a/Tests/test_file_jpeg2k.py b/Tests/test_file_jpeg2k.py index 0b4af45246e..2ef262e3e7f 100644 --- a/Tests/test_file_jpeg2k.py +++ b/Tests/test_file_jpeg2k.py @@ -31,9 +31,9 @@ def roundtrip(im, **options): im.save(out, "JPEG2000", **options) test_bytes = out.tell() out.seek(0) - im = Image.open(out) - im.bytes = test_bytes # for testing only - im.load() + with Image.open(out) as im: + im.bytes = test_bytes # for testing only + im.load() return im diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index ffacbbbf4f6..9a5577bbac0 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -757,8 +757,8 @@ class MyStdOut: if buffer: mystdout = mystdout.buffer - reloaded = Image.open(mystdout) - assert_image_equal_tofile(reloaded, TEST_PNG_FILE) + with Image.open(mystdout) as reloaded: + assert_image_equal_tofile(reloaded, TEST_PNG_FILE) @pytest.mark.skipif(is_win32(), reason="Requires Unix or macOS") diff --git a/Tests/test_image_resize.py b/Tests/test_image_resize.py index 17490e1a8c0..1fe278052fa 100644 --- a/Tests/test_image_resize.py +++ b/Tests/test_image_resize.py @@ -154,8 +154,8 @@ def test_cross_platform(self, tmp_path): @pytest.fixture def gradients_image(): - im = Image.open("Tests/images/radial_gradients.png") - im.load() + with Image.open("Tests/images/radial_gradients.png") as im: + im.load() try: yield im finally: diff --git a/Tests/test_imagechops.py b/Tests/test_imagechops.py index a19fbf239e3..b839a7b140a 100644 --- a/Tests/test_imagechops.py +++ b/Tests/test_imagechops.py @@ -368,11 +368,11 @@ def test_subtract_modulo_no_clip(): def test_soft_light(): # Arrange - im1 = Image.open("Tests/images/hopper.png") - im2 = Image.open("Tests/images/hopper-XYZ.png") + with Image.open("Tests/images/hopper.png") as im1: + with Image.open("Tests/images/hopper-XYZ.png") as im2: - # Act - new = ImageChops.soft_light(im1, im2) + # Act + new = ImageChops.soft_light(im1, im2) # Assert assert new.getpixel((64, 64)) == (163, 54, 32) @@ -381,11 +381,11 @@ def test_soft_light(): def test_hard_light(): # Arrange - im1 = Image.open("Tests/images/hopper.png") - im2 = Image.open("Tests/images/hopper-XYZ.png") + with Image.open("Tests/images/hopper.png") as im1: + with Image.open("Tests/images/hopper-XYZ.png") as im2: - # Act - new = ImageChops.hard_light(im1, im2) + # Act + new = ImageChops.hard_light(im1, im2) # Assert assert new.getpixel((64, 64)) == (144, 50, 27) @@ -394,11 +394,11 @@ def test_hard_light(): def test_overlay(): # Arrange - im1 = Image.open("Tests/images/hopper.png") - im2 = Image.open("Tests/images/hopper-XYZ.png") + with Image.open("Tests/images/hopper.png") as im1: + with Image.open("Tests/images/hopper-XYZ.png") as im2: - # Act - new = ImageChops.overlay(im1, im2) + # Act + new = ImageChops.overlay(im1, im2) # Assert assert new.getpixel((64, 64)) == (159, 50, 27) diff --git a/Tests/test_pickle.py b/Tests/test_pickle.py index f87801d7ed3..5fd04585563 100644 --- a/Tests/test_pickle.py +++ b/Tests/test_pickle.py @@ -88,10 +88,10 @@ def test_pickle_la_mode_with_palette(tmp_path): @skip_unless_feature("webp") def test_pickle_tell(): # Arrange - image = Image.open("Tests/images/hopper.webp") + with Image.open("Tests/images/hopper.webp") as image: - # Act: roundtrip - unpickled_image = pickle.loads(pickle.dumps(image)) + # Act: roundtrip + unpickled_image = pickle.loads(pickle.dumps(image)) # Assert assert unpickled_image.tell() == 0 diff --git a/Tests/test_sgi_crash.py b/Tests/test_sgi_crash.py index f9eaf9b1990..b5f9d442490 100644 --- a/Tests/test_sgi_crash.py +++ b/Tests/test_sgi_crash.py @@ -21,6 +21,6 @@ ) def test_crashes(test_file): with open(test_file, "rb") as f: - im = Image.open(f) - with pytest.raises(OSError): - im.load() + with Image.open(f) as im: + with pytest.raises(OSError): + im.load()