From 4b47177634abb31b8b961c58b182a56763c26215 Mon Sep 17 00:00:00 2001 From: Eric Soroos Date: Fri, 5 Jan 2018 10:58:04 +0000 Subject: [PATCH] Typing: Image.save --- src/PIL/Image.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 2bc20c472..61ab5fd20 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1996,15 +1996,16 @@ class Image(object): filename = "" open_fp = False - if isPath(fp): - filename = fp - open_fp = True - elif HAS_PATHLIB and isinstance(fp, Path): + _fp = None # type: BinaryIO + if isPath(fp) or (HAS_PATHLIB and isinstance(fp, Path)): filename = str(fp) open_fp = True - if not filename and hasattr(fp, "name") and isPath(fp.name): - # only set the name for metadata purposes - filename = fp.name + else: + _fp = fp # type: ignore + + if not filename and hasattr(_fp, "name") and isPath(_fp.name): + # only set the name for metadata purposes + filename = _fp.name # may mutate self! self.load() @@ -2037,14 +2038,14 @@ class Image(object): if open_fp: # Open also for reading ("+"), because TIFF save_all # writer needs to go back and edit the written data. - fp = builtins.open(filename, "w+b") + _fp = builtins.open(filename, "w+b") try: - save_handler(self, fp, filename) + save_handler(self, _fp, filename) finally: # do what we can to clean up if open_fp: - fp.close() + _fp.close() def seek(self, frame): # type: (int) -> None