Skip to content

BUG: implement SeekableFileObject.readline for Pillow HTTP streams (#1007) - #1209

Merged
FirefoxMetzger merged 2 commits into
imageio:masterfrom
cpruijsen:fix/issue-1007
Sep 26, 2026
Merged

FirefoxMetzger merged 2 commits into
imageio:masterfrom
cpruijsen:fix/issue-1007

Conversation

@cpruijsen

Copy link
Copy Markdown
Contributor

Fixes #1007.

SeekableFileObject is the wrapper ImageIO puts around HTTP (and zip)
streams so Pillow can seek. Pillow's IM plugin is always probed once
Image.open falls through the common formats (accept is None), and
XPM/IM parse headers with fp.readline(). A missing readline raised
AttributeError, which neither Pillow nor imopen's try-all path treat
as "wrong plugin."

#1084 added a stub (yield from self._file.readline()). That never
returned line bytes: the wrapped handle is self.f, and yield from
makes a generator. JPEG/WebP still opened because Pillow swallows
TypeError from that generator; formats that actually need readline
(XPM) still failed. Delegating to self.f.readline() would also be
wrong after Request.firstbytes has already read 256 bytes and seeked
the wrapper back to 0.

What I chose: BytesIO-compatible readline(size=-1) through the
existing buffer (stop at newline, EOF, or size).
Alternative: return self.read(100) (reporter), always consume the
remainder with no newline scan (suggested on #1007), or catch
AttributeError in imopen's try-all.
Reason: seek() already mimics BytesIO; BytesIO.readline is
already "until newline or EOF," which is "no EOL → rest of the file"
plus the correct stop when a newline exists. Catching AttributeError
would hide the next missing file method.
Happy to switch to always-read-rest if that is preferred.

Note to Maintainers

Remember to squash-merge the PR and to use an appropriate tag in the commit
message title.

@FirefoxMetzger

Copy link
Copy Markdown
Contributor

Okay, this is indeed a bug and your PR fixes it so this shall be merged.

A bit curious how you encountered it. From what I can see the way you would be able to trigger this is if you read some super old pillow formats directly from web. That's XM, XV Thumbnails and IFUNC Image Memory ... those formats predate the 2000s and I thought they would be long resting in peace by now. The world is a wondrous place :)

@FirefoxMetzger
FirefoxMetzger merged commit b6761b9 into imageio:master Sep 26, 2026
17 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.

SeekableFileObject needs readline() for Pillow plugin

2 participants