Skip to content

Commit

Permalink
Merge pull request #7199 from radarhere/tiff_close
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Jun 30, 2023
2 parents 9abef1a + 0835be9 commit 0ac3677
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
10 changes: 1 addition & 9 deletions src/PIL/TiffImagePlugin.py
Expand Up @@ -1253,9 +1253,8 @@ def _load_libtiff(self):
# To be nice on memory footprint, if there's a
# file descriptor, use that instead of reading
# into a string in python.
# libtiff closes the file descriptor, so pass in a dup.
try:
fp = hasattr(self.fp, "fileno") and os.dup(self.fp.fileno())
fp = hasattr(self.fp, "fileno") and self.fp.fileno()
# flush the file descriptor, prevents error on pypy 2.4+
# should also eliminate the need for fp.tell
# in _seek
Expand Down Expand Up @@ -1305,18 +1304,11 @@ def _load_libtiff(self):
# UNDONE -- so much for that buffer size thing.
n, err = decoder.decode(self.fp.read())

if fp:
try:
os.close(fp)
except OSError:
pass

self.tile = []
self.readonly = 0

self.load_end()

# libtiff closed the fp in a, we need to close self.fp, if possible
if close_self_fp:
self.fp.close()
self.fp = None # might be shared
Expand Down
11 changes: 10 additions & 1 deletion src/libImaging/TiffDecode.c
Expand Up @@ -720,7 +720,16 @@ ImagingLibTiffDecode(
}

decode_err:
TIFFClose(tiff);
// TIFFClose in libtiff calls tif_closeproc and TIFFCleanup
if (clientstate->fp) {
// Pillow will manage the closing of the file rather than libtiff
// So only call TIFFCleanup
TIFFCleanup(tiff);
} else {
// When tif_closeproc refers to our custom _tiffCloseProc though,
// that is fine, as it does not close the file
TIFFClose(tiff);
}
TRACE(("Done Decoding, Returning \n"));
// Returning -1 here to force ImageFile.load to break, rather than
// even think about looping back around.
Expand Down

0 comments on commit 0ac3677

Please sign in to comment.