mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-25 01:16:16 +03:00
Added context managers
This commit is contained in:
parent
0bd97c3df9
commit
3af00edc85
|
@ -61,8 +61,8 @@ repro_copy = (
|
||||||
|
|
||||||
|
|
||||||
for path in repro_ss2 + repro_lc + repro_advance + repro_brun + 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:
|
try:
|
||||||
im.load()
|
im.load()
|
||||||
except Exception as msg:
|
except Exception as msg:
|
||||||
print(msg)
|
print(msg)
|
||||||
|
|
|
@ -19,8 +19,8 @@ from PIL import Image
|
||||||
repro = ("00r0_gray_l.jp2", "00r1_graya_la.jp2")
|
repro = ("00r0_gray_l.jp2", "00r1_graya_la.jp2")
|
||||||
|
|
||||||
for path in repro:
|
for path in repro:
|
||||||
im = Image.open(path)
|
with Image.open(path) as im:
|
||||||
try:
|
try:
|
||||||
im.load()
|
im.load()
|
||||||
except Exception as msg:
|
except Exception as msg:
|
||||||
print(msg)
|
print(msg)
|
||||||
|
|
|
@ -31,9 +31,9 @@ def roundtrip(im, **options):
|
||||||
im.save(out, "JPEG2000", **options)
|
im.save(out, "JPEG2000", **options)
|
||||||
test_bytes = out.tell()
|
test_bytes = out.tell()
|
||||||
out.seek(0)
|
out.seek(0)
|
||||||
im = Image.open(out)
|
with Image.open(out) as im:
|
||||||
im.bytes = test_bytes # for testing only
|
im.bytes = test_bytes # for testing only
|
||||||
im.load()
|
im.load()
|
||||||
return im
|
return im
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -757,8 +757,8 @@ class TestFilePng:
|
||||||
|
|
||||||
if buffer:
|
if buffer:
|
||||||
mystdout = mystdout.buffer
|
mystdout = mystdout.buffer
|
||||||
reloaded = Image.open(mystdout)
|
with Image.open(mystdout) as reloaded:
|
||||||
assert_image_equal_tofile(reloaded, TEST_PNG_FILE)
|
assert_image_equal_tofile(reloaded, TEST_PNG_FILE)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skipif(is_win32(), reason="Requires Unix or macOS")
|
@pytest.mark.skipif(is_win32(), reason="Requires Unix or macOS")
|
||||||
|
|
|
@ -154,8 +154,8 @@ class TestImagingCoreResize:
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def gradients_image():
|
def gradients_image():
|
||||||
im = Image.open("Tests/images/radial_gradients.png")
|
with Image.open("Tests/images/radial_gradients.png") as im:
|
||||||
im.load()
|
im.load()
|
||||||
try:
|
try:
|
||||||
yield im
|
yield im
|
||||||
finally:
|
finally:
|
||||||
|
|
|
@ -368,11 +368,11 @@ def test_subtract_modulo_no_clip():
|
||||||
|
|
||||||
def test_soft_light():
|
def test_soft_light():
|
||||||
# Arrange
|
# Arrange
|
||||||
im1 = Image.open("Tests/images/hopper.png")
|
with Image.open("Tests/images/hopper.png") as im1:
|
||||||
im2 = Image.open("Tests/images/hopper-XYZ.png")
|
with Image.open("Tests/images/hopper-XYZ.png") as im2:
|
||||||
|
|
||||||
# Act
|
# Act
|
||||||
new = ImageChops.soft_light(im1, im2)
|
new = ImageChops.soft_light(im1, im2)
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
assert new.getpixel((64, 64)) == (163, 54, 32)
|
assert new.getpixel((64, 64)) == (163, 54, 32)
|
||||||
|
@ -381,11 +381,11 @@ def test_soft_light():
|
||||||
|
|
||||||
def test_hard_light():
|
def test_hard_light():
|
||||||
# Arrange
|
# Arrange
|
||||||
im1 = Image.open("Tests/images/hopper.png")
|
with Image.open("Tests/images/hopper.png") as im1:
|
||||||
im2 = Image.open("Tests/images/hopper-XYZ.png")
|
with Image.open("Tests/images/hopper-XYZ.png") as im2:
|
||||||
|
|
||||||
# Act
|
# Act
|
||||||
new = ImageChops.hard_light(im1, im2)
|
new = ImageChops.hard_light(im1, im2)
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
assert new.getpixel((64, 64)) == (144, 50, 27)
|
assert new.getpixel((64, 64)) == (144, 50, 27)
|
||||||
|
@ -394,11 +394,11 @@ def test_hard_light():
|
||||||
|
|
||||||
def test_overlay():
|
def test_overlay():
|
||||||
# Arrange
|
# Arrange
|
||||||
im1 = Image.open("Tests/images/hopper.png")
|
with Image.open("Tests/images/hopper.png") as im1:
|
||||||
im2 = Image.open("Tests/images/hopper-XYZ.png")
|
with Image.open("Tests/images/hopper-XYZ.png") as im2:
|
||||||
|
|
||||||
# Act
|
# Act
|
||||||
new = ImageChops.overlay(im1, im2)
|
new = ImageChops.overlay(im1, im2)
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
assert new.getpixel((64, 64)) == (159, 50, 27)
|
assert new.getpixel((64, 64)) == (159, 50, 27)
|
||||||
|
|
|
@ -88,10 +88,10 @@ def test_pickle_la_mode_with_palette(tmp_path):
|
||||||
@skip_unless_feature("webp")
|
@skip_unless_feature("webp")
|
||||||
def test_pickle_tell():
|
def test_pickle_tell():
|
||||||
# Arrange
|
# Arrange
|
||||||
image = Image.open("Tests/images/hopper.webp")
|
with Image.open("Tests/images/hopper.webp") as image:
|
||||||
|
|
||||||
# Act: roundtrip
|
# Act: roundtrip
|
||||||
unpickled_image = pickle.loads(pickle.dumps(image))
|
unpickled_image = pickle.loads(pickle.dumps(image))
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
assert unpickled_image.tell() == 0
|
assert unpickled_image.tell() == 0
|
||||||
|
|
|
@ -21,6 +21,6 @@ from PIL import Image
|
||||||
)
|
)
|
||||||
def test_crashes(test_file):
|
def test_crashes(test_file):
|
||||||
with open(test_file, "rb") as f:
|
with open(test_file, "rb") as f:
|
||||||
im = Image.open(f)
|
with Image.open(f) as im:
|
||||||
with pytest.raises(OSError):
|
with pytest.raises(OSError):
|
||||||
im.load()
|
im.load()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user