@@ -363,7 +363,7 @@ def test_verify_struct_error(self) -> None:
363363 with pytest .raises ((OSError , SyntaxError )):
364364 im .verify ()
365365
366- def test_verify_ignores_crc_error (self ) -> None :
366+ def test_verify_ignores_crc_error (self , monkeypatch : pytest . MonkeyPatch ) -> None :
367367 # check ignores crc errors in ancillary chunks
368368
369369 chunk_data = chunk (b"tEXt" , b"spam" )
@@ -373,24 +373,20 @@ def test_verify_ignores_crc_error(self) -> None:
373373 with pytest .raises (SyntaxError ):
374374 PngImagePlugin .PngImageFile (BytesIO (image_data ))
375375
376- ImageFile .LOAD_TRUNCATED_IMAGES = True
377- try :
378- im = load (image_data )
379- assert im is not None
380- finally :
381- ImageFile .LOAD_TRUNCATED_IMAGES = False
376+ monkeypatch .setattr (ImageFile , "LOAD_TRUNCATED_IMAGES" , True )
377+ im = load (image_data )
378+ assert im is not None
382379
383- def test_verify_not_ignores_crc_error_in_required_chunk (self ) -> None :
380+ def test_verify_not_ignores_crc_error_in_required_chunk (
381+ self , monkeypatch : pytest .MonkeyPatch
382+ ) -> None :
384383 # check does not ignore crc errors in required chunks
385384
386385 image_data = MAGIC + IHDR [:- 1 ] + b"q" + TAIL
387386
388- ImageFile .LOAD_TRUNCATED_IMAGES = True
389- try :
390- with pytest .raises (SyntaxError ):
391- PngImagePlugin .PngImageFile (BytesIO (image_data ))
392- finally :
393- ImageFile .LOAD_TRUNCATED_IMAGES = False
387+ monkeypatch .setattr (ImageFile , "LOAD_TRUNCATED_IMAGES" , True )
388+ with pytest .raises (SyntaxError ):
389+ PngImagePlugin .PngImageFile (BytesIO (image_data ))
394390
395391 def test_roundtrip_dpi (self ) -> None :
396392 # Check dpi roundtripping
@@ -600,7 +596,7 @@ def test_roundtrip_private_chunk(self) -> None:
600596 (b"prIV" , b"VALUE3" , True ),
601597 ]
602598
603- def test_textual_chunks_after_idat (self ) -> None :
599+ def test_textual_chunks_after_idat (self , monkeypatch : pytest . MonkeyPatch ) -> None :
604600 with Image .open ("Tests/images/hopper.png" ) as im :
605601 assert "comment" in im .text
606602 for k , v in {
@@ -614,18 +610,17 @@ def test_textual_chunks_after_idat(self) -> None:
614610 with pytest .raises (OSError ):
615611 assert isinstance (im .text , dict )
616612
613+ # Raises an EOFError in load_end
614+ with Image .open ("Tests/images/hopper_idat_after_image_end.png" ) as im :
615+ assert im .text == {"TXT" : "VALUE" , "ZIP" : "VALUE" }
616+
617617 # Raises a UnicodeDecodeError in load_end
618618 with Image .open ("Tests/images/truncated_image.png" ) as im :
619619 # The file is truncated
620620 with pytest .raises (OSError ):
621621 im .text
622- ImageFile . LOAD_TRUNCATED_IMAGES = True
622+ monkeypatch . setattr ( ImageFile , " LOAD_TRUNCATED_IMAGES" , True )
623623 assert isinstance (im .text , dict )
624- ImageFile .LOAD_TRUNCATED_IMAGES = False
625-
626- # Raises an EOFError in load_end
627- with Image .open ("Tests/images/hopper_idat_after_image_end.png" ) as im :
628- assert im .text == {"TXT" : "VALUE" , "ZIP" : "VALUE" }
629624
630625 def test_unknown_compression_method (self ) -> None :
631626 with pytest .raises (SyntaxError , match = "Unknown compression method" ):
@@ -651,15 +646,16 @@ def test_padded_idat(self) -> None:
651646 @pytest .mark .parametrize (
652647 "cid" , (b"IHDR" , b"sRGB" , b"pHYs" , b"acTL" , b"fcTL" , b"fdAT" )
653648 )
654- def test_truncated_chunks (self , cid : bytes ) -> None :
649+ def test_truncated_chunks (
650+ self , cid : bytes , monkeypatch : pytest .MonkeyPatch
651+ ) -> None :
655652 fp = BytesIO ()
656653 with PngImagePlugin .PngStream (fp ) as png :
657654 with pytest .raises (ValueError ):
658655 png .call (cid , 0 , 0 )
659656
660- ImageFile . LOAD_TRUNCATED_IMAGES = True
657+ monkeypatch . setattr ( ImageFile , " LOAD_TRUNCATED_IMAGES" , True )
661658 png .call (cid , 0 , 0 )
662- ImageFile .LOAD_TRUNCATED_IMAGES = False
663659
664660 @pytest .mark .parametrize ("save_all" , (True , False ))
665661 def test_specify_bits (self , save_all : bool , tmp_path : Path ) -> None :
@@ -789,17 +785,14 @@ class MyStdOut:
789785 with Image .open (mystdout ) as reloaded :
790786 assert_image_equal_tofile (reloaded , TEST_PNG_FILE )
791787
792- def test_truncated_end_chunk (self ) -> None :
788+ def test_truncated_end_chunk (self , monkeypatch : pytest . MonkeyPatch ) -> None :
793789 with Image .open ("Tests/images/truncated_end_chunk.png" ) as im :
794790 with pytest .raises (OSError ):
795791 im .load ()
796792
797- ImageFile .LOAD_TRUNCATED_IMAGES = True
798- try :
799- with Image .open ("Tests/images/truncated_end_chunk.png" ) as im :
800- assert_image_equal_tofile (im , "Tests/images/hopper.png" )
801- finally :
802- ImageFile .LOAD_TRUNCATED_IMAGES = False
793+ monkeypatch .setattr (ImageFile , "LOAD_TRUNCATED_IMAGES" , True )
794+ with Image .open ("Tests/images/truncated_end_chunk.png" ) as im :
795+ assert_image_equal_tofile (im , "Tests/images/hopper.png" )
803796
804797
805798@pytest .mark .skipif (is_win32 (), reason = "Requires Unix or macOS" )
@@ -808,19 +801,16 @@ class TestTruncatedPngPLeaks(PillowLeakTestCase):
808801 mem_limit = 2 * 1024 # max increase in K
809802 iterations = 100 # Leak is 56k/iteration, this will leak 5.6megs
810803
811- def test_leak_load (self ) -> None :
804+ def test_leak_load (self , monkeypatch : pytest . MonkeyPatch ) -> None :
812805 with open ("Tests/images/hopper.png" , "rb" ) as f :
813806 DATA = BytesIO (f .read (16 * 1024 ))
814807
815- ImageFile . LOAD_TRUNCATED_IMAGES = True
808+ monkeypatch . setattr ( ImageFile , " LOAD_TRUNCATED_IMAGES" , True )
816809 with Image .open (DATA ) as im :
817810 im .load ()
818811
819812 def core () -> None :
820813 with Image .open (DATA ) as im :
821814 im .load ()
822815
823- try :
824- self ._test_leak (core )
825- finally :
826- ImageFile .LOAD_TRUNCATED_IMAGES = False
816+ self ._test_leak (core )
0 commit comments