From d68ffc3a480d4b67dd11bf3ab4485c0e7ab789e3 Mon Sep 17 00:00:00 2001 From: Craig Northway Date: Fri, 25 Jul 2014 12:13:16 +1000 Subject: [PATCH] Closing file handles/streams --- git/cmd.py | 5 +++++ git/objects/commit.py | 35 +++++++++++++++++++++-------------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/git/cmd.py b/git/cmd.py index 1d9b4efbb..cbbd0a7ad 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -382,6 +382,11 @@ def execute(self, command, finally: proc.stdout.close() proc.stderr.close() + if proc.stdin: + proc.stdin.close() + proc.poll() + if proc.returncode is None: + proc.terminate() if self.GIT_PYTHON_TRACE == 'full': cmdstr = " ".join(command) diff --git a/git/objects/commit.py b/git/objects/commit.py index f9923e4de..e64d4da30 100644 --- a/git/objects/commit.py +++ b/git/objects/commit.py @@ -350,24 +350,31 @@ def _iter_from_process_or_stream(cls, odb, proc_or_stream): :param proc: git-rev-list process instance - one sha per line :return: iterator returning Commit objects""" stream = proc_or_stream + close_std_err = False if not hasattr(stream,'readline'): stream = proc_or_stream.stdout + close_std_err = True readline = stream.readline - while True: - line = readline() - if not line: - break - hexsha = line.strip() - if len(hexsha) > 40: - # split additional information, as returned by bisect for instance - hexsha, rest = line.split(None, 1) - # END handle extra info - - assert len(hexsha) == 40, "Invalid line: %s" % hexsha - yield cls(odb, hex_to_bin(hexsha)) - # END for each line in stream - + try: + while True: + line = readline() + if not line: + break + hexsha = line.strip() + if len(hexsha) > 40: + # split additional information, as returned by bisect for instance + hexsha, rest = line.split(None, 1) + # END handle extra info + + assert len(hexsha) == 40, "Invalid line: %s" % hexsha + yield cls(odb, hex_to_bin(hexsha)) + # END for each line in stream + finally: + stream.close() + if close_std_err: + proc_or_stream.stderr.close() + #{ Serializable Implementation def _serialize(self, stream):