From e8ab2487dfd24c3df729dcfb409b687e50591f14 Mon Sep 17 00:00:00 2001 From: Oliver Kisielius Date: Fri, 13 Apr 2018 00:00:35 -0400 Subject: [PATCH 1/2] Fix resource warnings under Python 3.6, issue #151 --- pygraphviz/agraph.py | 58 ++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/pygraphviz/agraph.py b/pygraphviz/agraph.py index 4cb8fc24..2e60c05b 100644 --- a/pygraphviz/agraph.py +++ b/pygraphviz/agraph.py @@ -1304,35 +1304,35 @@ def _run_prog(self, prog='nop', args=''): runprog = r'"%s"' % self._get_prog(prog) cmd = ' '.join([runprog, args]) dotargs = shlex.split(cmd) - p = subprocess.Popen(dotargs, - shell=False, - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - close_fds=False) - (child_stdin, - child_stdout, - child_stderr) = (p.stdin, p.stdout, p.stderr) - # Use threading to avoid blocking - data = [] - errors = [] - threads = [PipeReader(data, child_stdout), - PipeReader(errors, child_stderr)] - for t in threads: - t.start() - - self.write(child_stdin) - child_stdin.close() - - for t in threads: - t.join() - - if not data: - raise IOError(b"".join(errors).decode(self.encoding)) - - if len(errors) > 0: - warnings.warn(b"".join(errors).decode(self.encoding), RuntimeWarning) - return b"".join(data) + with subprocess.Popen(dotargs, + shell=False, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + close_fds=False) as p: + (child_stdin, + child_stdout, + child_stderr) = (p.stdin, p.stdout, p.stderr) + # Use threading to avoid blocking + data = [] + errors = [] + threads = [PipeReader(data, child_stdout), + PipeReader(errors, child_stderr)] + for t in threads: + t.start() + + self.write(child_stdin) + child_stdin.close() + + for t in threads: + t.join() + + if not data: + raise IOError(b"".join(errors).decode(self.encoding)) + + if len(errors) > 0: + warnings.warn(b"".join(errors).decode(self.encoding), RuntimeWarning) + return b"".join(data) def layout(self, prog='neato', args=''): """Assign positions to nodes in graph. From 3b296ffa13a40b804b2a5e2c41ff620d638dd852 Mon Sep 17 00:00:00 2001 From: Oliver Kisielius Date: Fri, 13 Apr 2018 00:13:38 -0400 Subject: [PATCH 2/2] Less ham-handed than previous commit --- pygraphviz/agraph.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pygraphviz/agraph.py b/pygraphviz/agraph.py index 2e60c05b..9a66cf81 100644 --- a/pygraphviz/agraph.py +++ b/pygraphviz/agraph.py @@ -1327,12 +1327,12 @@ def _run_prog(self, prog='nop', args=''): for t in threads: t.join() - if not data: - raise IOError(b"".join(errors).decode(self.encoding)) + if not data: + raise IOError(b"".join(errors).decode(self.encoding)) - if len(errors) > 0: - warnings.warn(b"".join(errors).decode(self.encoding), RuntimeWarning) - return b"".join(data) + if len(errors) > 0: + warnings.warn(b"".join(errors).decode(self.encoding), RuntimeWarning) + return b"".join(data) def layout(self, prog='neato', args=''): """Assign positions to nodes in graph.