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

Added ImageFile close() #8702

Merged
merged 2 commits into from
Jan 20, 2025
Merged

Added ImageFile close() #8702

merged 2 commits into from
Jan 20, 2025

Conversation

radarhere
Copy link
Member

@radarhere radarhere commented Jan 18, 2025

This is part of #8362 - I'm hoping to break down that PR into easier-to-review chunks.

  1. Image's close() starts out with file handling.

    Pillow/src/PIL/Image.py

    Lines 632 to 637 in cf7dd2f

    if hasattr(self, "fp"):
    try:
    self._close_fp()
    self.fp = None
    except Exception as msg:
    logger.debug("Error closing: %s", msg)

    Since file handling is only part of ImageFile, this PR moves that code into a new ImageFile close() method that then calls super().close() to return to the rest of the method.

  2. Similarly, Image's _close_fp() can be moved to ImageFile for the same reason.

    Pillow/src/PIL/Image.py

    Lines 606 to 612 in cf7dd2f

    def _close_fp(self):
    if getattr(self, "_fp", False):
    if self._fp != self.fp:
    self._fp.close()
    self._fp = DeferredError(ValueError("Operation on closed image"))
    if self.fp:
    self.fp.close()

@hugovk hugovk merged commit 58dd4a7 into python-pillow:main Jan 20, 2025
52 checks passed
@radarhere radarhere deleted the imagefile_fp branch January 20, 2025 20:44
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