Added context managers

This commit is contained in:
Andrew Murray 2021-11-25 23:16:07 +11:00
parent 0bd97c3df9
commit 3af00edc85
8 changed files with 35 additions and 35 deletions

View File

@ -61,7 +61,7 @@ repro_copy = (
for path in repro_ss2 + repro_lc + repro_advance + repro_brun + repro_copy:
im = Image.open(path)
with Image.open(path) as im:
try:
im.load()
except Exception as msg:

View File

@ -19,7 +19,7 @@ from PIL import Image
repro = ("00r0_gray_l.jp2", "00r1_graya_la.jp2")
for path in repro:
im = Image.open(path)
with Image.open(path) as im:
try:
im.load()
except Exception as msg:

View File

@ -31,7 +31,7 @@ def roundtrip(im, **options):
im.save(out, "JPEG2000", **options)
test_bytes = out.tell()
out.seek(0)
im = Image.open(out)
with Image.open(out) as im:
im.bytes = test_bytes # for testing only
im.load()
return im

View File

@ -757,7 +757,7 @@ class TestFilePng:
if buffer:
mystdout = mystdout.buffer
reloaded = Image.open(mystdout)
with Image.open(mystdout) as reloaded:
assert_image_equal_tofile(reloaded, TEST_PNG_FILE)

View File

@ -154,7 +154,7 @@ class TestImagingCoreResize:
@pytest.fixture
def gradients_image():
im = Image.open("Tests/images/radial_gradients.png")
with Image.open("Tests/images/radial_gradients.png") as im:
im.load()
try:
yield im

View File

@ -368,8 +368,8 @@ 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)
@ -381,8 +381,8 @@ 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)
@ -394,8 +394,8 @@ 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)

View File

@ -88,7 +88,7 @@ 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))

View File

@ -21,6 +21,6 @@ from PIL import Image
)
def test_crashes(test_file):
with open(test_file, "rb") as f:
im = Image.open(f)
with Image.open(f) as im:
with pytest.raises(OSError):
im.load()