mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-10 19:56:47 +03:00
Added context managers
This commit is contained in:
parent
c2203a13a8
commit
83542c42bf
|
@ -5,4 +5,5 @@ from io import BytesIO
|
|||
|
||||
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,4 +5,7 @@ from io import BytesIO
|
|||
|
||||
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
|
||||
|
|
|
@ -54,15 +54,18 @@ class TestDecompressionBomb:
|
|||
|
||||
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:
|
||||
|
|
|
@ -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 test_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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -798,7 +798,8 @@ class TestFileJpeg:
|
|||
|
||||
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
|
||||
|
|
|
@ -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():
|
||||
|
|
|
@ -106,7 +106,8 @@ class TestFilePng:
|
|||
|
||||
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 @@ class TestFilePng:
|
|||
|
||||
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.
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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():
|
||||
|
|
|
@ -163,9 +163,8 @@ class TestFileTiff:
|
|||
|
||||
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 @@ class TestFileTiff:
|
|||
|
||||
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 @@ class TestFileTiff:
|
|||
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")
|
||||
|
|
|
@ -165,7 +165,8 @@ class TestFileWebp:
|
|||
|
||||
# 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))
|
||||
|
|
|
@ -92,11 +92,13 @@ class TestImage:
|
|||
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 @@ class TestImage:
|
|||
|
||||
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
|
||||
|
|
|
@ -110,4 +110,5 @@ if is_win32():
|
|||
DeleteObject(dib)
|
||||
DeleteDC(hdc)
|
||||
|
||||
Image.open(BytesIO(bitmap)).save(opath)
|
||||
with Image.open(BytesIO(bitmap)) as im:
|
||||
im.save(opath)
|
||||
|
|
Loading…
Reference in New Issue
Block a user