Skip to content

Commit 388a3f6

Browse files
committed
Fix test and code.
1 parent 23fd509 commit 388a3f6

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

Tests/test_file_png.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,16 @@ class MyStdOut:
777777
mystdout = mystdout.buffer
778778
with Image.open(mystdout) as reloaded:
779779
assert_image_equal_tofile(reloaded, TEST_PNG_FILE)
780+
781+
def test_end_truncated_filed(self):
782+
ImageFile.LOAD_TRUNCATED_IMAGES = True
783+
try:
784+
img = Image.open("Tests/images/end_trunc_file.png")
785+
img.load()
786+
assert img is not None
787+
finally:
788+
ImageFile.LOAD_TRUNCATED_IMAGES = False
789+
780790

781791

782792
@pytest.mark.skipif(is_win32(), reason="Requires Unix or macOS")
@@ -786,20 +796,18 @@ class TestTruncatedPngPLeaks(PillowLeakTestCase):
786796
iterations = 100 # Leak is 56k/iteration, this will leak 5.6megs
787797

788798
def test_leak_load(self):
789-
files = ['Tests/images/hopper.png', 'Tests/images/end_trunc_file.png']
790-
for file in files:
791-
with open(file, "rb") as f:
792-
DATA = BytesIO(f.read(16 * 1024))
799+
with open("Tests/images/hopper.png", "rb") as f:
800+
DATA = BytesIO(f.read(16 * 1024))
793801

794-
ImageFile.LOAD_TRUNCATED_IMAGES = True
802+
ImageFile.LOAD_TRUNCATED_IMAGES = True
803+
with Image.open(DATA) as im:
804+
im.load()
805+
806+
def core():
795807
with Image.open(DATA) as im:
796808
im.load()
797809

798-
def core():
799-
with Image.open(DATA) as im:
800-
im.load()
801-
802-
try:
803-
self._test_leak(core)
804-
finally:
805-
ImageFile.LOAD_TRUNCATED_IMAGES = False
810+
try:
811+
self._test_leak(core)
812+
finally:
813+
ImageFile.LOAD_TRUNCATED_IMAGES = False

src/PIL/PngImagePlugin.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -984,9 +984,7 @@ def load_end(self):
984984
try:
985985
ImageFile._safe_read(self.fp, length)
986986
except OSError as e:
987-
if (
988-
str(e) == "Truncated File Read"
989-
) and ImageFile.LOAD_TRUNCATED_IMAGES:
987+
if ImageFile.LOAD_TRUNCATED_IMAGES:
990988
logger.debug("%r %s %s (Truncated File Read)", cid, pos, length)
991989
break
992990
else:

0 commit comments

Comments
 (0)