From 9a3d4a6ab1ec92f8d998f37d858b2705434f8d98 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 13 Jul 2024 12:32:50 +1000 Subject: [PATCH] When IFD is missing, connect get_ifd() dictionary to Exif --- Tests/test_image.py | 8 ++++++++ src/PIL/Image.py | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 4fdc4179170..77be5826154 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -769,6 +769,14 @@ def test_empty_exif(self) -> None: exif.load(b"Exif\x00\x00") assert not dict(exif) + def test_empty_get_ifd(self) -> None: + exif = Image.Exif() + ifd = exif.get_ifd(0x8769) + assert ifd == {} + + ifd[36864] = b"0220" + assert exif.get_ifd(0x8769) == {36864: b"0220"} + @mark_if_feature_version( pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing" ) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 565abe71d45..67b9cd4b8cc 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -4073,7 +4073,9 @@ def get_ifd(self, tag): else: # Interop self._ifds[tag] = self._get_ifd_dict(tag_data, tag) - ifd = self._ifds.get(tag, {}) + if tag not in self._ifds: + self._ifds[tag] = {} + ifd = self._ifds[tag] if tag == ExifTags.IFD.Exif and self._hidden_data: ifd = { k: v