Close file pointer earlier

This commit is contained in:
Andrew Murray 2025-04-15 18:31:29 +10:00
parent bd39801a7b
commit 4a6792e525
4 changed files with 15 additions and 15 deletions

View File

@ -190,9 +190,9 @@ def test_rle8() -> None:
# Signal end of bitmap before the image is finished
with open("Tests/images/bmp/g/pal8rle.bmp", "rb") as fp:
data = fp.read(1063) + b"\x01"
with Image.open(io.BytesIO(data)) as im:
with pytest.raises(ValueError):
im.load()
with Image.open(io.BytesIO(data)) as im:
with pytest.raises(ValueError):
im.load()
def test_rle4() -> None:
@ -214,9 +214,9 @@ def test_rle4() -> None:
def test_rle8_eof(file_name: str, length: int) -> None:
with open(file_name, "rb") as fp:
data = fp.read(length)
with Image.open(io.BytesIO(data)) as im:
with pytest.raises(ValueError):
im.load()
with Image.open(io.BytesIO(data)) as im:
with pytest.raises(ValueError):
im.load()
def test_offset() -> None:

View File

@ -457,8 +457,8 @@ def test_comment() -> None:
# Test an image that is truncated partway through a codestream
with open("Tests/images/comment.jp2", "rb") as fp:
b = BytesIO(fp.read(130))
with Image.open(b) as im:
pass
with Image.open(b) as im:
pass
def test_save_comment(card: ImageFile.ImageFile) -> None:

View File

@ -81,7 +81,7 @@ class TestFileLibTiff(LibTiffTestCase):
s = io.BytesIO()
with open(test_file, "rb") as f:
s.write(f.read())
s.seek(0)
s.seek(0)
with Image.open(s) as im:
assert im.size == (500, 500)
self._assert_noerr(tmp_path, im)
@ -1050,12 +1050,12 @@ class TestFileLibTiff(LibTiffTestCase):
with open("Tests/images/old-style-jpeg-compression.tif", "rb") as fp:
data = fp.read()
# Set EXIF Orientation to 2
data = data[:102] + b"\x02" + data[103:]
# Set EXIF Orientation to 2
data = data[:102] + b"\x02" + data[103:]
with Image.open(io.BytesIO(data)) as im:
im = im.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
assert_image_equal_tofile(im, "Tests/images/old-style-jpeg-compression.png")
with Image.open(io.BytesIO(data)) as im:
im = im.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
assert_image_equal_tofile(im, "Tests/images/old-style-jpeg-compression.png")
def test_open_missing_samplesperpixel(self) -> None:
with Image.open(

View File

@ -32,7 +32,7 @@ class TestFileLibTiffSmall(LibTiffTestCase):
s = BytesIO()
with open(test_file, "rb") as f:
s.write(f.read())
s.seek(0)
s.seek(0)
with Image.open(s) as im:
assert im.size == (128, 128)
self._assert_noerr(tmp_path, im)