Merge pull request #7199 from radarhere/tiff_close

This commit is contained in:
Hugo van Kemenade 2023-06-30 09:27:26 +03:00 committed by GitHub
commit 0ac3677b56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 10 deletions

View File

@ -1253,9 +1253,8 @@ class TiffImageFile(ImageFile.ImageFile):
# To be nice on memory footprint, if there's a # To be nice on memory footprint, if there's a
# file descriptor, use that instead of reading # file descriptor, use that instead of reading
# into a string in python. # into a string in python.
# libtiff closes the file descriptor, so pass in a dup.
try: 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+ # flush the file descriptor, prevents error on pypy 2.4+
# should also eliminate the need for fp.tell # should also eliminate the need for fp.tell
# in _seek # in _seek
@ -1305,18 +1304,11 @@ class TiffImageFile(ImageFile.ImageFile):
# UNDONE -- so much for that buffer size thing. # UNDONE -- so much for that buffer size thing.
n, err = decoder.decode(self.fp.read()) n, err = decoder.decode(self.fp.read())
if fp:
try:
os.close(fp)
except OSError:
pass
self.tile = [] self.tile = []
self.readonly = 0 self.readonly = 0
self.load_end() self.load_end()
# libtiff closed the fp in a, we need to close self.fp, if possible
if close_self_fp: if close_self_fp:
self.fp.close() self.fp.close()
self.fp = None # might be shared self.fp = None # might be shared

View File

@ -720,7 +720,16 @@ ImagingLibTiffDecode(
} }
decode_err: decode_err:
// 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); TIFFClose(tiff);
}
TRACE(("Done Decoding, Returning \n")); TRACE(("Done Decoding, Returning \n"));
// Returning -1 here to force ImageFile.load to break, rather than // Returning -1 here to force ImageFile.load to break, rather than
// even think about looping back around. // even think about looping back around.