Skip to content

Commit ffc993c

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 7eb728f commit ffc993c

File tree

3 files changed

+96
-51
lines changed

3 files changed

+96
-51
lines changed

doc/make.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88
import re
99
import argparse
10+
import subprocess
1011
import matplotlib
1112

1213
def copy_if_out_of_date(original, derived):
@@ -32,10 +33,14 @@ def check_build():
3233
pass
3334

3435
def doctest():
35-
os.system('sphinx-build -b doctest -d build/doctrees . build/doctest')
36+
subprocess.call(
37+
'sphinx-build -b doctest -d build/doctrees . build/doctest',
38+
shell=True)
3639

3740
def linkcheck():
38-
os.system('sphinx-build -b linkcheck -d build/doctrees . build/linkcheck')
41+
subprocess.call(
42+
'sphinx-build -b linkcheck -d build/doctrees . build/linkcheck',
43+
shell=True)
3944

4045
def html(buildername='html'):
4146
check_build()
@@ -52,9 +57,12 @@ def html(buildername='html'):
5257
options = ''
5358
if warnings_as_errors:
5459
options = options + ' -W'
55-
if os.system('sphinx-build -j %d %s -b %s -d build/doctrees . build/%s' % (
56-
n_proc, options, buildername, buildername)):
57-
raise SystemExit("Building HTML failed.")
60+
try:
61+
output = subprocess.check_output('sphinx-build -j %d %s -b %s -d build/doctrees . build/%s' % (
62+
n_proc, options, buildername, buildername), shell=True,
63+
stderr=subprocess.STDOUT)
64+
except subprocess.CalledProcessError as exc:
65+
raise SystemExit("Building HTML failed with %s." % exc.output)
5866

5967
# Clean out PDF files from the _images directory
6068
for filename in glob.glob('build/%s/_images/*.pdf' % buildername):
@@ -78,15 +86,20 @@ def latex():
7886
#figs()
7987
if sys.platform != 'win32':
8088
# LaTeX format.
81-
if os.system('sphinx-build -b latex -d build/doctrees . build/latex'):
82-
raise SystemExit("Building LaTeX failed.")
89+
try:
90+
output = subprocess.check_output('sphinx-build -b latex -d build/doctrees . build/latex',
91+
shell=True, stderr=subprocess.STDOUT)
92+
except subprocess.CalledProcessError as exc:
93+
raise SystemExit("Building LaTeX failed with %s." % exc.output)
8394

8495
# Produce pdf.
8596
os.chdir('build/latex')
8697

8798
# Call the makefile produced by sphinx...
88-
if os.system('make'):
89-
raise SystemExit("Rendering LaTeX failed.")
99+
try:
100+
output = subprocess.check_output('make', stderr=subprocess.STDOUT)
101+
except subprocess.CalledProcessError as exc:
102+
raise SystemExit("Rendering LaTeX failed with %s." % exc.output)
90103

91104
os.chdir('../..')
92105
else:
@@ -97,16 +110,20 @@ def texinfo():
97110
#figs()
98111
if sys.platform != 'win32':
99112
# Texinfo format.
100-
if os.system(
101-
'sphinx-build -b texinfo -d build/doctrees . build/texinfo'):
102-
raise SystemExit("Building Texinfo failed.")
113+
try:
114+
output = subprocess.check_output('sphinx-build -b texinfo -d build/doctrees . build/texinfo',
115+
shell=True, stderr=subprocess.STDOUT)
116+
except subprocess.CalledProcessError as exc:
117+
raise SystemExit("Building Texinfo failed with %s." % exc.output)
103118

104119
# Produce info file.
105120
os.chdir('build/texinfo')
106121

107122
# Call the makefile produced by sphinx...
108-
if os.system('make'):
109-
raise SystemExit("Rendering Texinfo failed.")
123+
try:
124+
output = subprocess.check_output('make', stderr=subprocess.STDOUT)
125+
except subprocess.CalledProcessError as exc:
126+
raise SystemExit("Rendering Texinfo failed with %s." % exc.output)
110127

111128
os.chdir('../..')
112129
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.\n\Here 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)