From 1bb21f0c85ec38234a2b13a6cd3bdddc13b9010e Mon Sep 17 00:00:00 2001 From: Johannes 'josch' Schauer Date: Sun, 5 Apr 2020 12:16:59 +0200 Subject: [PATCH] put read() into a different try block than open() --- src/img2pdf.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/img2pdf.py b/src/img2pdf.py index 860478e..c455a87 100755 --- a/src/img2pdf.py +++ b/src/img2pdf.py @@ -1864,13 +1864,19 @@ def convert(*images, **kwargs): # the thing doesn't have a read() function, so try if we can treat # it as a file name try: - with open(img, "rb") as f: - rawdata = f.read() + f = open(img, "rb") except Exception: # whatever the exception is (string could contain NUL # characters or the path could just not exist) it's not a file # name so we now try treating it as raw image content rawdata = img + else: + # we are not using a "with" block here because we only want to + # catch exceptions thrown by open(). The read() may throw its + # own exceptions like MemoryError which should be handled + # differently. + rawdata = f.read() + f.close() for ( color,