Skip to content

Restore image position after an error in ImageSequence.all_frames() - #9999

Merged
radarhere merged 3 commits into
python-pillow:mainfrom
niukanen1:fix/restore-all-frames-position
Sep 14, 2026
Merged

Restore image position after an error in ImageSequence.all_frames()#9999
radarhere merged 3 commits into
python-pillow:mainfrom
niukanen1:fix/restore-all-frames-position

Conversation

@niukanen1

@niukanen1 niukanen1 commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

Changes proposed in this pull request:

  • Run the existing frame-position restore in ImageSequence.all_frames() when frame collection raises, as well as when it succeeds.
  • Add a regression with a custom image that raises during seek(), checking that the exception propagates and the original position is restored.

Previously, an exception while collecting frames left the caller's image at the failed frame. The finally block covers frame collection only; callback execution and successful results keep their existing behavior.

The change is also documented in the 13.0.0 release notes.

Validation on the current head:

  • python3 selftest.py — 59 passed.
  • pytest -q Tests/test_imagesequence.py — 9 passed.
  • Applicable pre-commit hooks for the changed test and release note — passed.

Before the test simplification, the unchanged production fix also passed the full local test suite, repository lint, and mypy.

@radarhere

Copy link
Copy Markdown
Member

Hi. I'm curious - is this a theoretical concern, or something that actually caused a problem for you? If nothing else, I want to make sure that individual Pillow formats aren't raising errors during seek() on valid images.

@radarhere

Copy link
Copy Markdown
Member

I've created niukanen1#1 with suggestions.

@radarhere radarhere changed the title Restore image position when all_frames copying fails Restore image position after an error in ImageSequence.all_frames() Sep 14, 2026
@niukanen1

Copy link
Copy Markdown
Contributor Author

Thanks for asking. This came from inspecting ImageSequence.all_frames(), not from a user report. I reproduced the state leak with Tests/images/multipage.tiff by starting on frame 2 and deliberately raising a pre-created RuntimeError from Image.Image.copy() on frame 1. Before the fix, the image remained on frame 1. I have not observed any Pillow format raising from seek() on a valid image, so I do not have evidence of a spontaneous format-specific seek failure.

Thanks as well for the fork PR. I applied both suggested commits. The custom Image test exercises the same finally cleanup path more simply, and the release-note wording is more accurate for list input because processing stops at the exception and later images are not visited. The current head passes selftest.py, Tests/test_imagesequence.py, and the applicable pre-commit hooks.

@radarhere
radarhere merged commit b9329b8 into python-pillow:main Sep 14, 2026
53 of 54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants