Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed exceptions when closing AppendingTiffWriter #8723

Merged
merged 1 commit into from
Jan 31, 2025

Conversation

radarhere
Copy link
Member

@radarhere radarhere commented Jan 30, 2025

Two exceptions are being raised in testing - https://github.com/python-pillow/Pillow/actions/runs/13051364088/job/36412180284#step:11:4911

Tests/test_file_tiff.py::TestFileTiff::test_appending_tiff_writer_writelong
  /Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/_pytest/unraisableexception.py:85: PytestUnraisableExceptionWarning: Exception ignored in: <PIL.TiffImagePlugin.AppendingTiffWriter object at 0x114c785e0>
  
  Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/PIL/TiffImagePlugin.py", line 2212, in close
      self.finalize()
      ~~~~~~~~~~~~~^^
    File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/PIL/TiffImagePlugin.py", line 2082, in finalize
      raise RuntimeError(msg)
  RuntimeError: IIMM of new page doesn't match IIMM of first page
  
    warnings.warn(pytest.PytestUnraisableExceptionWarning(msg))

Tests/test_file_tiff.py::TestFileTiff::test_appending_tiff_writer_rewritelastshorttolong
  /Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/_pytest/unraisableexception.py:85: PytestUnraisableExceptionWarning: Exception ignored in: <PIL.TiffImagePlugin.AppendingTiffWriter object at 0x114c7ade0>
  
  Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/PIL/TiffImagePlugin.py", line 2212, in close
      self.finalize()
      ~~~~~~~~~~~~~^^
    File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/PIL/TiffImagePlugin.py", line 2082, in finalize
      raise RuntimeError(msg)
  RuntimeError: IIMM of new page doesn't match IIMM of first page
  
    warnings.warn(pytest.PytestUnraisableExceptionWarning(msg))

These exceptions are occurred when AppendingTiffWriter is being closed at the end of the test method.

def test_appending_tiff_writer_writelong(self) -> None:
data = b"II\x2A\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
b = BytesIO(data)
with TiffImagePlugin.AppendingTiffWriter(b) as a:
a.writeLong(2**32 - 1)
assert b.getvalue() == data + b"\xff\xff\xff\xff"
def test_appending_tiff_writer_rewritelastshorttolong(self) -> None:
data = b"II\x2A\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
b = BytesIO(data)
with TiffImagePlugin.AppendingTiffWriter(b) as a:
a.rewriteLastShortToLong(2**32 - 1)
assert b.getvalue() == data[:-2] + b"\xff\xff\xff\xff"

Since that is after the assert, the code that is run during close can simply be disabled.

Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please could you add comments to explain why? It won't be obvious in the future without looking up the PR via the blame.

@radarhere
Copy link
Member Author

If that's confusing, then how about a different solution instead - I've updated the code so that it is now seeking backwards through the data first before the write/rewrite operation.

@hugovk hugovk merged commit c5d27af into python-pillow:main Jan 31, 2025
50 of 51 checks passed
@radarhere radarhere deleted the tiff_iimm branch January 31, 2025 23:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants