From 38fd77ca7dc4786ddafba064bcce789bed41f216 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 15 Nov 2016 20:13:25 +1100 Subject: [PATCH] Fixed bug in saving to fp-objects in Python >= 3.4 --- PIL/Image.py | 2 +- Tests/test_image.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/PIL/Image.py b/PIL/Image.py index 655aa3d07..368186f81 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -1659,7 +1659,7 @@ class Image(object): if isinstance(fp, Path): filename = str(fp) open_fp = True - elif hasattr(fp, "name") and isPath(fp.name): + if not filename and hasattr(fp, "name") and isPath(fp.name): # only set the name for metadata purposes filename = fp.name diff --git a/Tests/test_image.py b/Tests/test_image.py index a625a429b..ef9aa16af 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -63,6 +63,18 @@ class TestImage(PillowTestCase): os.remove(temp_file) im.save(Path(temp_file)) + def test_fp_name(self): + temp_file = self.tempfile("temp.jpg") + + class FP(object): + def write(a, b): + pass + fp = FP() + fp.name = temp_file + + im = hopper() + im.save(fp) + def test_tempfile(self): # see #1460, pathlib support breaks tempfile.TemporaryFile on py27 # Will error out on save on 3.0.0