diff --git a/PIL/EpsImagePlugin.py b/PIL/EpsImagePlugin.py index 34cccf5a7..fb5bf7ffe 100644 --- a/PIL/EpsImagePlugin.py +++ b/PIL/EpsImagePlugin.py @@ -405,13 +405,15 @@ def _save(im, fp, filename, eps=1): fp.write("[%d 0 0 -%d 0 %d]\n" % (im.size[0], im.size[1], im.size[1])) fp.write("{ currentfile buf readhexstring pop } bind\n") fp.write(operator[2] + "\n") - fp.flush() + if hasattr(fp, "flush"): + fp.flush() ImageFile._save(im, base_fp, [("eps", (0, 0)+im.size, 0, None)]) fp.write("\n%%%%EndBinary\n") fp.write("grestore end\n") - fp.flush() + if hasattr(fp, "flush"): + fp.flush() # # -------------------------------------------------------------------- diff --git a/PIL/IcnsImagePlugin.py b/PIL/IcnsImagePlugin.py index 060596b48..a4366e9e7 100644 --- a/PIL/IcnsImagePlugin.py +++ b/PIL/IcnsImagePlugin.py @@ -306,10 +306,8 @@ def _save(im, fp, filename): OS X only. """ - try: + if hasattr(fp, "flush"): fp.flush() - except: - pass # create the temporary set of pngs iconset = tempfile.mkdtemp('.iconset') diff --git a/PIL/ImageFile.py b/PIL/ImageFile.py index da86e1155..f26a7260e 100644 --- a/PIL/ImageFile.py +++ b/PIL/ImageFile.py @@ -489,10 +489,8 @@ def _save(im, fp, tile, bufsize=0): if s < 0: raise IOError("encoder error %d when writing image file" % s) e.cleanup() - try: + if hasattr(fp, "flush"): fp.flush() - except: - pass def _safe_read(fp, size): diff --git a/PIL/PalmImagePlugin.py b/PIL/PalmImagePlugin.py index 8836341df..4f415ff7c 100644 --- a/PIL/PalmImagePlugin.py +++ b/PIL/PalmImagePlugin.py @@ -227,7 +227,8 @@ def _save(im, fp, filename, check=0): ImageFile._save( im, fp, [("raw", (0, 0)+im.size, 0, (rawmode, rowbytes, 1))]) - fp.flush() + if hasattr(fp, "flush"): + fp.flush() # diff --git a/PIL/PdfImagePlugin.py b/PIL/PdfImagePlugin.py index 466fc6723..7decf0ee5 100644 --- a/PIL/PdfImagePlugin.py +++ b/PIL/PdfImagePlugin.py @@ -244,7 +244,8 @@ def _save(im, fp, filename, save_all=False): fp.write("%010d 00000 n \n" % x) fp.write("trailer\n<<\n/Size %d\n/Root 1 0 R\n>>\n" % len(xref)) fp.write("startxref\n%d\n%%%%EOF\n" % startxref) - fp.flush() + if hasattr(fp, "flush"): + fp.flush() # # -------------------------------------------------------------------- diff --git a/PIL/PngImagePlugin.py b/PIL/PngImagePlugin.py index 92695d493..d6778821b 100644 --- a/PIL/PngImagePlugin.py +++ b/PIL/PngImagePlugin.py @@ -762,10 +762,8 @@ def _save(im, fp, filename, chunk=putchunk, check=0): chunk(fp, b"IEND", b"") - try: + if hasattr(fp, "flush"): fp.flush() - except: - pass # -------------------------------------------------------------------- diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index 06d692891..1f974226c 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -1048,7 +1048,8 @@ class TiffImageFile(ImageFile.ImageFile): # flush the file descriptor, prevents error on pypy 2.4+ # should also eliminate the need for fp.tell for py3 # in _seek - self.fp.flush() + if hasattr(self.fp, "flush"): + self.fp.flush() except IOError: # io.BytesIO have a fileno, but returns an IOError if # it doesn't use a file descriptor.