Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Comments

Only close file handle in ImagePalette.save() if it was opened internally#9444

Open
bysiber wants to merge 4 commits intopython-pillow:mainfrom
bysiber:fix-palette-save-close-caller
Open

Only close file handle in ImagePalette.save() if it was opened internally#9444
bysiber wants to merge 4 commits intopython-pillow:mainfrom
bysiber:fix-palette-save-close-caller

Conversation

@bysiber
Copy link

ImagePalette.save() unconditionally callsfp.close() at the end, even when the file handle was passed in by the caller. This closes the caller's file object, causing any subsequent operations on it to fail withValueError: I/O operation on closed file.

Whenfp is a string (filename),save() correctly opens the file internally — but only that internally-opened handle should be closed. The fix tracks whether we opened the file and only closes it in that case, wrapped intry/finally to avoid leaking the handle if a write fails.

@radarhere
Copy link
Member

Hi. You've opened 63 pull requests today after minimal prior GitHub activity. Are you an AI agent?

@bysiber
Copy link
Author

Hey, yeah I went on a bit of a contribution binge — I've been going through a bunch of popular Python libraries as a learning exercise and decided to upstream the fixes I found along the way. I realize the volume probably looks a bit unusual, sorry about that. Happy to take it slower if the review load is too much for you guys.

@radarhere
Copy link
Member

radarhere commentedFeb 20, 2026
edited
Loading

The review load is fine.

I've createdbysiber#2 with some suggestions.

wrapped intry/finally to avoid leaking the handle if a write fails.

Just for the record, could you give an example of when a write would fail?

@bysiber
Copy link
Author

bysiber commentedFeb 20, 2026
edited
Loading

Thanks for the suggestions PR, I'll merge those in.

Regarding when a write could fail honestly the most realistic case is disk full (OSError), but it could also happen with network-mounted filesystems dropping mid-write, or permission quirks after the open succeeds. It's more of a defensive pattern than something you'd hit often in practice. If you think the try/finally is overkill and a plain close is fine, I'm happy to simplify it, the main fix is really just the conditional close.

@radarhereradarhere changed the titleOnly close file handle in ImagePalette.save() when we opened itOnly close file handle in ImagePalette.save() if it was opened internallyFeb 20, 2026
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@radarhereradarhereradarhere approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@bysiber@radarhere

[8]ページ先頭

©2009-2026 Movatter.jp