8000 Overhaul external process calls · matplotlib/matplotlib@8f4f12c · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 8f4f12c

Browse files
committed
Overhaul external process calls
This commits starts with moving from `os.system` to the `subprocess` module and captures stderr and includes it in the exceptions. For more information see gh-7490.
1 parent a517c60 commit 8f4f12c

File tree

3 files changed

+95
-51
lines changed

3 files changed

+95
-51
lines changed

doc/make.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,16 @@ def check_build():
4040

4141
def doctest():
4242
"""Execute Sphinx 'doctest' target. """
43-
os.system('sphinx-build -b doctest -d build/doctrees . build/doctest')
43+
subprocess.call(
44+
'sphinx-build -b doctest -d build/doctrees . build/doctest',
45+
shell=True)
4446

4547

4648
def linkcheck():
4749
"""Execute Sphinx 'linkcheck' target. """
48-
os.system('sphinx-build -b linkcheck -d build/doctrees . build/linkcheck')
50+
subprocess.call(
51+
'sphinx-build -b linkcheck -d build/doctrees . build/linkcheck',
52+
shell=True)
4953

5054

5155
# For generating PNGs of the top row of index.html:
@@ -102,9 +106,12 @@ def html(buildername='html'):
102106
options = ''
103107
if warnings_as_errors:
104108
options = options + ' -W'
105-
if os.system('sphinx-build -j %d %s -b %s -d build/doctrees . build/%s' % (
106-
n_proc, options, buildername, buildername)):
107-
raise SystemExit("Building HTML failed.")
109+
try:
110+
output = subprocess.check_output('sphinx-build -j %d %s -b %s -d build/doctrees . build/%s' % (
111+
n_proc, options, buildername, buildername), shell=True,
112+
stderr=subprocess.STDOUT)
113+
except subprocess.CalledProcessError as exc:
114+
raise SystemExit("Building HTML failed with %s." % exc.output)
108115

109116
# Clean out PDF files from the _images directory
110117
for filename in glob.glob('build/%s/_images/*.pdf' % buildername):
@@ -132,15 +139,20 @@ def latex():
132139
# figs()
133140
if sys.platform != 'win32':
134141
# LaTeX format.
135-
if os.system('sphinx-build -b latex -d build/doctrees . build/latex'):
136-
raise SystemExit("Building LaTeX failed.")
142+
try:
143+
output = subprocess.check_output('sphinx-build -b latex -d build/doctrees . build/latex',
144+
shell=True, stderr=subprocess.STDOUT)
145+
except subprocess.CalledProcessError as exc:
146+
raise SystemExit("Building LaTeX failed with %s." % exc.output)
137147

138148
# Produce pdf.
139149
os.chdir('build/latex')
140150

141151
# Call the makefile produced by sphinx...
142-
if os.system('make'):
143-
raise SystemExit("Rendering LaTeX failed.")
152+
try:
153+
output = subprocess.check_output('make', stderr=subprocess.STDOUT)
154+
except subprocess.CalledProcessError as exc:
155+
raise SystemExit("Rendering LaTeX failed with %s." % exc.output)
144156

145157
os.chdir('../..')
146158
else:
@@ -153,16 +165,20 @@ def texinfo():
153165
# figs()
154166
if sys.platform != 'win32':
155167
# Texinfo format.
156-
if os.system(
157-
'sphinx-build -b texinfo -d build/doctrees . build/texinfo'):
158-
raise SystemExit("Building Texinfo failed.")
168+
try:
169+
output = subprocess.check_output('sphinx-build -b texinfo -d build/doctrees . build/texinfo',
170+
shell=True, stderr=subprocess.STDOUT)
171+
except subprocess.CalledProcessError as exc:
172+
raise SystemExit("Building Texinfo failed with %s." % exc.output)
159173

160174
# Produce info file.
161175
os.chdir('build/texinfo')
162176

163177
# Call the makefile produced by sphinx...
164-
if os.system('make'):
165-
raise SystemExit("Rendering Texinfo failed.")
178+
try:
179+
output = subprocess.check_output('make', stderr=subprocess.STDOUT)
180+
except subprocess.CalledProcessError as exc:
181+
raise SystemExit("Rendering Texinfo failed with %s." % exc.output)
166182

167183
os.chdir('../..')
168184
else:

lib/matplotlib/backends/backend_ps.py

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import six
99
from six.moves import StringIO
1010

11-
import glob, math, os, shutil, sys, time, datetime
11+
import glob, math, os, shutil, sys, subprocess, time, datetime
1212
def _fn_name(): return sys._getframe(1).f_code.co_name
1313
import io
1414

@@ -1440,27 +1440,32 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
14401440
command = '%s cd "%s" && latex -interaction=nonstopmode "%s" > "%s"'\
14411441
%(precmd, tmpdir, latexfile, outfile)
14421442
verbose.report(command, 'debug')
1443-
exit_status = os.system(command)
1444-
1445-
with io.open(outfile, 'rb') as fh:
1446-
if exit_status:
1443+
try:
1444+
output = subprocess.check_output(command, shell=True,
1445+
stderr=subprocess.STDOUT)
1446+
except subprocess.CalledProcessError as exc:
1447+
with io.open(outfile, 'rb') as fh:
14471448
raise RuntimeError('LaTeX was not able to process your file:\
1448-
\nHere is the full report generated by LaTeX: \n\n%s'% fh.read())
1449-
else:
1449+
and failed with %s.\n\
1450+
\nHere is the full report generated by LaTeX: \n\n%s'% (output, fh.read()))
1451+
else:
1452+
with io.open(outfile, 'rb') as fh:
14501453
verbose.report(fh.read(), 'debug')
14511454
os.remove(outfile)
14521455

14531456
command = '%s cd "%s" && dvips -q -R0 -o "%s" "%s" > "%s"'%(precmd, tmpdir,
14541457
os.path.split(psfile)[-1], os.path.split(dvifile)[-1], outfile)
14551458
verbose.report(command, 'debug')
1456-
exit_status = os.system(command)
1457-
1458-
with io.open(outfile, 'rb') as fh:
1459-
if exit_status:
1459+
try:
1460+
output = subprocess.check_output(command, shell=True,
1461+
stderr=subprocess.STDOUT)
1462+
except subprocess.CalledProcessError as exc:
1463+
with io.open(outfile, 'rb') as fh:
14601464
raise RuntimeError('dvips was not able to \
1461-
process the following file:\n%s\nHere is the full report generated by dvips: \
1462-
\n\n'% dvifile + fh.read())
1463-
else:
1465+
process the following file:\n%s\nHere is the full report generated by dvips: \
1466+
\n\n'% dvifile + fh.read())
1467+
else:
1468+
with io.open(outfile, 'rb') as fh:
14641469
verbose.report(fh.read(), 'debug')
14651470
os.remove(outfile)
14661471
os.remove(epsfile)
@@ -1511,19 +1516,34 @@ def gs_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
15111516
paper_option, psfile, tmpfile, outfile)
15121517

15131518
verbose.report(command, 'debug')
1514-
exit_status = os.system(command)
1515-
1516-
with io.open(outfile, 'rb') as fh:
1517-
if exit_status:
1518-
output = fh.read()
1519-
m = "\n".join(["ghostscript was not able to process your image.",
1520-
"Here is the full report generated by ghostscript:",
1521-
"",
1522-
"%s"])
1523-
# use % to prevent problems with bytes
1524-
raise RuntimeError(m % output)
1525-
else:
1519+
try:
1520+
output = subprocess.check_output(command, shell=True,
1521+
stderr=subprocess.STDOUT)
1522+
except subprocess.CalledProcessError as exc:
1523+
with io.open(outfile, 'rb') as fh:
1524+
raise RuntimeError('dvips was not able to \
1525+
process the following file:\n%s\nHere is the full report generated by dvips: \
1526+
\n\n'% dvifile + fh.read())
1527+
else:
1528+
with io.open(outfile, 'rb') as fh:
15261529
verbose.report(fh.read(), 'debug')
1530+
1531+
try:
1532+
process_output = subprocess.check_output(command, shell=True,
1533+
stderr=subprocess.STDOUT)
1534+
except subprocess.CalledProcessError as exc:
1535+
with io.open(outfile, 'rb') as fh:
1536+
output = fh.read()
1537+
m = "\n".join(["ghostscript was not able to process your image.",
1538+
"It failed with %s.",
1539+
"Here is the full report generated by ghostscript:",
1540+
"",
1541+
"%s"])
1542+
# use % to prevent problems with bytes
1543+
raise RuntimeError(m % (process_output, output))
1544+
else:
1545+
with io.open(outfile, 'rb') as fh:
1546+
verbose.report(fh.read(), 'debug')
15271547
os.remove(outfile)
15281548
os.remove(tmpfile)
15291549
shutil.move(psfile, tmpfile)
@@ -1564,24 +1584,31 @@ def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
15641584
(paper_option, tmpfile, pdffile, outfile)
15651585
if sys.platform == 'win32': command = command.replace('=', '#')
15661586
verbose.report(command, 'debug')
1567-
exit_status = os.system(command)
1568-
with io.open(outfile, 'rb') as fh:
1569-
if exit_status:
1587+
1588+
try:
1589+
output = subprocess.check_output(command, shell=True,
1590+
stderr=subprocess.STDOUT)
1591+
except subprocess.CalledProcessError as exc:
1592+
with io.open(outfile, 'rb') as fh:
15701593
raise RuntimeError('ps2pdf was not able to process your \
1571-
image.\nHere is the report generated by ghostscript:\n\n' + fh.read())
1572-
else:
1594+
image and failed with %s.\n\Here is the report generated by ghostscript:\n\n' % (output) + fh.read())
1595+
else:
1596+
with io.open(outfile, 'rb') as fh:
15731597
verbose.report(fh.read(), 'debug')
15741598
os.remove(outfile)
15751599
command = 'pdftops -paper match -level2 "%s" "%s" > "%s"'% \
15761600
(pdffile, psfile, outfile)
15771601
verbose.report(command, 'debug')
1578-
exit_status = os.system(command)
15791602

1580-
with io.open(outfile, 'rb') as fh:
1581-
if exit_status:
1603+
try:
1604+
output = subprocess.check_output(command, shell=True,
1605+
stderr=subprocess.STDOUT)
1606+
except subprocess.CalledProcessError as exc:
1607+
with io.open(outfile, 'rb') as fh:
15821608
raise RuntimeError('pdftops was not able to process your \
1583-
image.\nHere is the full report generated by pdftops: \n\n' + fh.read())
1584-
else:
1609+
image and failed with %s.\nHere is the full report generated by pdftops: \n\n' % (output) + fh.read())
1610+
else:
1611+
with io.open(outfile, 'rb') as fh:
15851612
verbose.report(fh.read(), 'debug')
15861613
os.remove(outfile)
15871614
os.remove(tmpfile)

tools/subset.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import sys
2929
import getopt
3030
import os
31+
import subprocess
3132
import struct
3233

3334
def log_namelist(nam, unicode):
@@ -145,7 +146,7 @@ def subset_font_raw(font_in, font_out, unicodes, opts):
145146
if pe:
146147
print('Generate("' + font_out + '")', file=pe)
147148
pe.close()
148-
os.system("fontforge -script " + pe_fn)
149+
subprocess.call("fontforge -script " + pe_fn, shell=True)
149150
else:
150151
font.generate(font_out, flags = flags)
151152
font.close()

0 commit comments

Comments
 (0)
0