Skip to content

Raise SyntaxError for a SPIDER image within a stack opened directly - #10425

Merged
radarhere merged 3 commits into
python-pillow:mainfrom
lee3Q:fix/spider-image-in-stack-direct-open
Nov 6, 2026
Merged

radarhere merged 3 commits into
python-pillow:mainfrom
lee3Q:fix/spider-image-in-stack-direct-open

Conversation

@lee3Q

@lee3Q lee3Q commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Changes proposed in this pull request:

  • A SPIDER header with stack flag 0 and image number above 0 describes an image within a stack. SpiderImageFile._open() only reads it when seek() has already set stkoffset. When such a file is opened directly, self.stkoffset does not exist and Image.open() raises AttributeError. The other bad-header cases raise SyntaxError, which Image.open() turns into UnidentifiedImageError.
  • Add a hasattr(self, "stkoffset") check to that branch, so a directly opened image of this kind falls through to the existing "inconsistent stack header values" SyntaxError.
  • Add a test that builds such a header from Tests/images/hopper.spider (image number set to 1) and expects the SyntaxError from SpiderImageFile and UnidentifiedImageError from Image.open().

This does not change how stacks are read through seek(). It treats a lone header of this kind as invalid. Reading it as a single image instead would be a different choice, and the SPIDER header documentation does not describe such a file as standalone.

How this was found and checked:

  • This change was made with AI assistance. It was found by mutating header bytes of 18 image formats (1500 mutations); SPIDER was the only format that raised an unexpected exception type.
  • Tests/test_file_spider.py: the new test fails on the base commit 55078c5 with the AttributeError, and the file passes (20 passed) with this change. This was run twice, in separate runs, on a source build with Python 3.12 and the codec libraries disabled.
  • Tests/test_image.py and Tests/test_imagefile.py show the same 24 failures before and after this change in that build, all "decoder/encoder not available" errors from the missing codecs, and 194 passed, 9 skipped.
  • Not run: the full test suite, CI with codecs, other Python versions.

lee3Q added 2 commits November 5, 2026 20:14
An image header with stack flag 0 and image number above 0 is only read through seek(). Opening it directly raised AttributeError, so it now raises the usual SyntaxError for inconsistent stack headers.
Covers a header with stack flag 0 and image number 1: SpiderImageFile raises the inconsistent stack header SyntaxError and Image.open raises UnidentifiedImageError.
@radarhere radarhere added the 🤖-assisted AI-assisted label Nov 5, 2026
@radarhere
radarhere force-pushed the fix/spider-image-in-stack-direct-open branch from 4028f87 to 1b80670 Compare November 6, 2026 01:08
@radarhere
radarhere force-pushed the fix/spider-image-in-stack-direct-open branch from 1b80670 to 60af02c Compare November 6, 2026 01:16
@radarhere radarhere changed the title Raise SyntaxError for an image within a stack opened directly Raise SyntaxError for an image within a SPIDER stack opened directly Nov 6, 2026
@radarhere radarhere changed the title Raise SyntaxError for an image within a SPIDER stack opened directly Raise SyntaxError for a SPIDER image within a stack opened directly Nov 6, 2026
@radarhere
radarhere enabled auto-merge (squash) November 6, 2026 01:18
@radarhere
radarhere merged commit d897c3b into python-pillow:main Nov 6, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤖-assisted AI-assisted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants