From c76f1344a525c009bbdf3747c1595fd78c7a367e Mon Sep 17 00:00:00 2001 From: "Ryan C. Thompson" Date: Wed, 5 Nov 2014 23:46:47 -0800 Subject: [PATCH] Avoid leaking file descriptors This change prevents img2pdf from opening *all* input files at once, which means it now works with thousands of input files. --- src/img2pdf.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/img2pdf.py b/src/img2pdf.py index ae6a9fd..16745c7 100755 --- a/src/img2pdf.py +++ b/src/img2pdf.py @@ -204,7 +204,9 @@ def convert(images, dpi, x, y, title=None, author=None, creator=None, producer=N pdf = pdfdoc(3, title, author, creator, producer, creationdate, moddate, subject, keywords) - for im in images: + for imfilename in images: + debug_out("Reading %s"%imfilename, verbose) + im = open(imfilename, "rb") rawdata = im.read() im.seek(0) try: @@ -295,7 +297,7 @@ def valid_date(string): parser = argparse.ArgumentParser( description='Lossless conversion/embedding of images (in)to pdf') parser.add_argument( - 'images', metavar='infile', type=argparse.FileType('rb'), + 'images', metavar='infile', type=str, nargs='+', help='input file(s)') parser.add_argument( '-o', '--output', metavar='out', type=argparse.FileType('wb'),