From bd14f3a6cf2f95c1ea235305b8709d43dee0b655 Mon Sep 17 00:00:00 2001 From: Arnaud Fontaine <arnaud.fontaine@nexedi.com> Date: Fri, 11 Oct 2019 18:29:12 +0900 Subject: [PATCH] PortalTransforms: subprocesstransform: Make sure that the NamedTemporaryFile is closed. --- .../libtransforms/commandtransform.py | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/product/PortalTransforms/libtransforms/commandtransform.py b/product/PortalTransforms/libtransforms/commandtransform.py index c0a4138d1a..783d7bd0d4 100644 --- a/product/PortalTransforms/libtransforms/commandtransform.py +++ b/product/PortalTransforms/libtransforms/commandtransform.py @@ -147,24 +147,24 @@ class subprocesstransform: def convert(self, data, cache, **kwargs): command = "%s %s" % (self.binary, self.binaryArgs) + stdin_file = PIPE + try: + if not self.useStdin: + stdin_file = tempfile.NamedTemporaryFile() + stdin_file.write( data) + stdin_file.seek(0) + command = command % {'infile': stdin_file.name} # apply tmp name to command + data = None + + argument_list = shlex.split(command) + process = Popen(argument_list, stdin=stdin_file, stdout=PIPE, + stderr=PIPE, close_fds=True) + data_out, data_err = process.communicate(input=data) + if process.returncode: + raise OSError, data_err + cache.setData(data_out) + return cache - if not self.useStdin: - tempfile_object = tempfile.NamedTemporaryFile() - tmpname = tempfile_object.name - tempfile_object.write( data) - tempfile_object.seek(0) - command = command % {'infile': tmpname} # apply tmp name to command - argument_list = shlex.split(command) - if self.useStdin: - process = Popen(argument_list, stdin=tempfile_object, stdout=PIPE, - stderr=PIPE, close_fds=True) - data_out, data_err = process.communicate() - tempfile_object.close() - else: - process = Popen(argument_list, stdin=PIPE, stdout=PIPE, - stderr=PIPE, close_fds=True) - data_out, data_err = process.communicate(input=data) - if process.returncode: - raise OSError, data_err - cache.setData(data_out) - return cache + finally: + if isinstance(stdin_file, file): + stdin_file.close() -- 2.30.9