Skip to content

Commit

Permalink
MAI: call subprocess.Popen.communicate, instead of reading and waiting
Browse files Browse the repository at this point in the history
because it avoids need to join list of byte objects
(which is a list of `str` in python 2,
but a list of `bytes` in python 3).
The code is also simpler.

Raise any exceptions after cleaning auxiliary files.
  • Loading branch information
johnyf committed Jul 2, 2016
1 parent bfeca93 commit 9b3c1a1
Showing 1 changed file with 12 additions and 31 deletions.
43 changes: 12 additions & 31 deletions pydot.py
Expand Up @@ -1876,37 +1876,18 @@ def create(self, prog=None, format='ps'):
prog=prog))
else:
raise
stderr = p.stderr
stdout = p.stdout
stdout_output = list()
while True:
data = stdout.read()
if not data:
break
stdout_output.append(data)
stdout.close()
stdout_output = ''.join(stdout_output)
if not stderr.closed:
stderr_output = list()
while True:
data = stderr.read()
if not data:
break
stderr_output.append(data)
stderr.close()
if stderr_output:
stderr_output = ''.join(stderr_output)
# pid, status = os.waitpid(p.pid, 0)
status = p.wait()
if status != 0:
raise InvocationException(
('Program terminated with status:'
' %d. stderr follows: %s') % (
status, stderr_output))
elif stderr_output:
print(stderr_output)
# For each of the image files...
stdout_data, stderr_data = p.communicate()
# clean file litter
for img in self.shape_files:
os.unlink(os.path.join(tmp_dir, os.path.basename(img)))
os.unlink(tmp_name)
return stdout_output
# print(stdout_data)
if p.returncode != 0:
print(
('{cmdline} return code: {c}\n\n'
'stdout, stderr:\n {out}\n\n').format(
c=p.returncode,
out=stdout_data,
err=stderr_data))
assert p.returncode == 0, p.returncode
return stdout_data

0 comments on commit 9b3c1a1

Please sign in to comment.