Skip to content

Commit b136e9e

Browse files
committed
TexManager: Use subprocess instead of os.system
1 parent 6348e10 commit b136e9e

File tree

1 file changed

+84
-61
lines changed

1 file changed

+84
-61
lines changed

lib/matplotlib/texmanager.py

Lines changed: 84 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import glob
4444
import os
4545
import shutil
46+
from matplotlib.compat.subprocess import subprocess
4647
import sys
4748
import warnings
4849

@@ -398,30 +399,29 @@ def make_dvi(self, tex, fontsize):
398399
if DEBUG or not os.path.exists(dvifile):
399400
texfile = self.make_tex(tex, fontsize)
400401
outfile = basefile + '.output'
401-
command = self._get_shell_cmd(
402-
'cd "%s"' % self.texcache,
403-
'latex -interaction=nonstopmode %s > "%s"' %
404-
(os.path.split(texfile)[-1], outfile))
402+
command = ["latex", "-interaction=nonstopmode",
403+
os.path.split(texfile)[-1]]
405404
mpl.verbose.report(command, 'debug')
406405
with Locked(self.texcache):
407-
exit_status = os.system(command)
408-
try:
409-
with open(outfile) as fh:
410-
report = fh.read()
411-
except IOError:
412-
report = 'No latex error report available.'
413-
try:
414-
os.stat(dvifile)
415-
exists = True
416-
except OSError:
417-
exists = False
418-
if exit_status or not exists:
419-
raise RuntimeError(
420-
('LaTeX was not able to process the following '
421-
'string:\n%s\nHere is the full report generated by '
422-
'LaTeX: \n\n' % repr(tex.encode('unicode_escape')) +
423-
report))
424-
else:
406+
try:
407+
with open(outfile, "w") as fout:
408+
subprocess.check_call(command,
409+
cwd=self.texcache,
410+
stdout=fout,
411+
stderr=subprocess.STDOUT)
412+
except subprocess.CalledProcessError as exc:
413+
try:
414+
with open(outfile) as fh:
415+
report = fh.read()
416+
except IOError:
417+
report = 'No latex error report available.'
418+
raise RuntimeError(
419+
('LaTeX was not able to process the following '
420+
'string:\n%s\nLaTeX failed with: %s\n'
421+
'Here is the full report generated by LaTeX: '
422+
'\n\n' % (exc.output,
423+
repr(tex.encode('unicode_escape'))) +
424+
report))
425425
mpl.verbose.report(report, 'debug')
426426
for fname in glob.glob(basefile + '*'):
427427
if fname.endswith('dvi'):
@@ -452,25 +452,34 @@ def make_dvi_preview(self, tex, fontsize):
452452
not os.path.exists(baselinefile)):
453453
texfile = self.make_tex_preview(tex, fontsize)
454454
outfile = basefile + '.output'
455-
command = self._get_shell_cmd(
456-
'cd "%s"' % self.texcache,
457-
'latex -interaction=nonstopmode %s > "%s"' %
458-
(os.path.split(texfile)[-1], outfile))
455+
command = ["latex", "-interaction=nonstopmode",
456+
os.path.split(texfile)[-1]]
459457
mpl.verbose.report(command, 'debug')
460-
exit_status = os.system(command)
458+
try:
459+
with open(outfile, "w") as fout:
460+
subprocess.check_call(command,
461+
cwd=self.texcache,
462+
stdout=fout,
463+
stderr=subprocess.STDOUT)
464+
except subprocess.CalledProcessError as exc:
465+
try:
466+
with open(outfile) as fh:
467+
report = fh.read()
468+
except IOError:
469+
report = 'No latex error report available.'
470+
raise RuntimeError(
471+
('LaTeX was not able to process the following '
472+
'string:\n%s\nLaTeX failed with: %s\n'
473+
'Here is the full report generated by LaTeX: '
474+
'\n\n' % (exc.output,
475+
repr(tex.encode('unicode_escape'))) +
476+
report))
461477
try:
462478
with open(outfile) as fh:
463479
report = fh.read()
464-
465480
except IOError:
466481
report = 'No latex error report available.'
467-
if exit_status:
468-
raise RuntimeError(
469-
('LaTeX was not able to process the following '
470-
'string:\n%s\nHere is the full report generated by '
471-
'LaTeX: \n\n' % repr(tex)) + report)
472-
else:
473-
mpl.verbose.report(report, 'debug')
482+
mpl.verbose.report(report, 'debug')
474483

475484
# find the box extent information in the latex output
476485
# file and store them in ".baseline" file
@@ -506,25 +515,35 @@ def make_png(self, tex, fontsize, dpi):
506515
if DEBUG or not os.path.exists(pngfile):
507516
dvifile = self.make_dvi(tex, fontsize)
508517
outfile = basefile + '.output'
509-
command = self._get_shell_cmd(
510-
'cd "%s"' % self.texcache,
511-
'dvipng -bg Transparent -D %s -T tight -o "%s" "%s" > "%s"' %
512-
(dpi, os.path.split(pngfile)[-1],
513-
os.path.split(dvifile)[-1], outfile))
518+
command = ["dvipng", "-bg", "Transparent", "-D", str(dpi),
519+
"-T", "tight", "-o", os.path.split(pngfile)[-1],
520+
os.path.split(dvifile)[-1]]
514521
mpl.verbose.report(command, 'debug')
515-
exit_status = os.system(command)
522+
try:
523+
with open(outfile, "w") as fout:
524+
subprocess.check_call(command,
525+
cwd=self.texcache,
526+
stdout=fout,
527+
stderr=subprocess.STDOUT)
528+
except subprocess.CalledProcessError as exc:
529+
try:
530+
with open(outfile) as fh:
531+
report = fh.read()
532+
except IOError:
533+
report = 'No dvipng error report available.'
534+
raise RuntimeError(
535+
('dvipng was not able to process the following '
536+
'string:\n%s\ndvipng failed with: %s\n'
537+
'Here is the full report generated by dvipng: '
538+
'\n\n' % (exc.output,
539+
repr(tex.encode('unicode_escape'))) +
540+
report))
516541
try:
517542
with open(outfile) as fh:
518543
report = fh.read()
519544
except IOError:
520545
report = 'No dvipng error report available.'
521-
if exit_status:
522-
raise RuntimeError(
523-
'dvipng was not able to process the following '
524-
'file:\n%s\nHere is the full report generated by '
525-
'dvipng: \n\n' % dvifile + report)
526-
else:
527-
mpl.verbose.report(report, 'debug')
546+
mpl.verbose.report(report, 'debug')
528547
try:
529548
os.remove(outfile)
530549
except OSError:
@@ -544,21 +563,25 @@ def make_ps(self, tex, fontsize):
544563
if DEBUG or not os.path.exists(psfile):
545564
dvifile = self.make_dvi(tex, fontsize)
546565
outfile = basefile + '.output'
547-
command = self._get_shell_cmd(
548-
'cd "%s"' % self.texcache,
549-
'dvips -q -E -o "%s" "%s" > "%s"' %
550-
(os.path.split(psfile)[-1],
551-
os.path.split(dvifile)[-1], outfile))
566+
command = ["dvips", "-q", "-E", "-o", os.path.split(psfile)[-1],
567+
os.path.split(dvifile)[-1]]
552568
mpl.verbose.report(command, 'debug')
553-
exit_status = os.system(command)
554-
with open(outfile) as fh:
555-
if exit_status:
569+
try:
570+
with open(outfile, "w") as fout:
571+
subprocess.check_call(command,
572+
cwd=self.texcache,
573+
stdout=fout,
574+
stderr=subprocess.STDOUT)
575+
except subprocess.CalledProcessError as exc:
576+
with open(outfile) as fh:
556577
raise RuntimeError(
557-
'dvipng was not able to process the flowing '
558-
'file:\n%s\nHere is the full report generated by '
559-
'dvipng: \n\n' % dvifile + fh.read())
560-
else:
561-
mpl.verbose.report(fh.read(), 'debug')
578+
('dvipng was not able to process the following '
579+
'file:\n%s\ndvipng failed with: %s\n'
580+
'Here is the full report generated by dvipng: '
581+
'\n\n' % (dvifile, exc.output) +
582+
fh.read()))
583+
with open(outfile) as fh:
584+
mpl.verbose.report(fh.read(), 'debug')
562585
os.remove(outfile)
563586

564587
return psfile

0 commit comments

Comments
 (0)