Skip to content

Commit 3e94f67

Browse files
committed
TexManager: Use subprocess instead of os.system
1 parent 620c9cd commit 3e94f67

File tree

1 file changed

+89
-62
lines changed

1 file changed

+89
-62
lines changed

lib/matplotlib/texmanager.py

Lines changed: 89 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
from matplotlib import rcParams
5555
from matplotlib._png import read_png
5656
from matplotlib.cbook import mkdirs, Locked
57-
from matplotlib.compat.subprocess import Popen, PIPE, STDOUT
57+
from matplotlib.compat.subprocess import subprocess, Popen, PIPE, STDOUT
5858
import matplotlib.dviread as dviread
5959
import re
6060

@@ -398,30 +398,33 @@ def make_dvi(self, tex, fontsize):
398398
if DEBUG or not os.path.exists(dvifile):
399399
texfile = self.make_tex(tex, fontsize)
400400
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))
401+
command = [str("latex"), "-interaction=nonstopmode",
402+
os.path.basename(texfile)]
405403
mpl.verbose.report(command, 'debug')
406404
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:
405+
try:
406+
with open(outfile, "w") as fout:
407+
subprocess.check_call(command,
408+
cwd=self.texcache,
409+
stdout=fout,
410+
stderr=subprocess.STDOUT)
411+
except subprocess.CalledProcessError as exc:
412+
try:
413+
with open(outfile) as fh:
414+
report = fh.read()
415+
except IOError:
416+
report = 'No latex error report available.'
417+
raise RuntimeError(
418+
'LaTeX was not able to process the following '
419+
'string:\n%s\nLaTeX failed with: %s\n'
420+
'Here is the full report generated by LaTeX: '
421+
'\n\n' % (repr(tex.encode('unicode_escape')),
422+
report))
423+
try:
424+
with open(outfile) as fh:
425+
report = fh.read()
426+
except IOError:
427+
report = 'No latex error report available.'
425428
mpl.verbose.report(report, 'debug')
426429
for fname in glob.glob(basefile + '*'):
427430
if fname.endswith('dvi'):
@@ -452,25 +455,34 @@ def make_dvi_preview(self, tex, fontsize):
452455
not os.path.exists(baselinefile)):
453456
texfile = self.make_tex_preview(tex, fontsize)
454457
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))
458+
command = [str("latex"), "-interaction=nonstopmode",
459+
os.path.basename(texfile)]
459460
mpl.verbose.report(command, 'debug')
460-
exit_status = os.system(command)
461+
try:
462+
with open(outfile, "w") as fout:
463+
subprocess.check_call(command,
464+
cwd=self.texcache,
465+
stdout=fout,
466+
stderr=subprocess.STDOUT)
467+
except subprocess.CalledProcessError as exc:
468+
try:
469+
with open(outfile) as fh:
470+
report = fh.read()
471+
except IOError:
472+
report = 'No latex error report available.'
473+
raise RuntimeError(
474+
('LaTeX was not able to process the following '
475+
'string:\n%s\nLaTeX failed with: %s\n'
476+
'Here is the full report generated by LaTeX: '
477+
'\n\n' % (exc.output,
478+
repr(tex.encode('unicode_escape'))) +
479+
report))
461480
try:
462481
with open(outfile) as fh:
463482
report = fh.read()
464-
465483
except IOError:
466484
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')
485+
mpl.verbose.report(report, 'debug')
474486

475487
# find the box extent information in the latex output
476488
# file and store them in ".baseline" file
@@ -506,25 +518,35 @@ def make_png(self, tex, fontsize, dpi):
506518
if DEBUG or not os.path.exists(pngfile):
507519
dvifile = self.make_dvi(tex, fontsize)
508520
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))
521+
command = [str("dvipng"), "-bg", "Transparent", "-D", str(dpi),
522+
"-T", "tight", "-o", os.path.basename(pngfile),
523+
os.path.basename(dvifile)]
514524
mpl.verbose.report(command, 'debug')
515-
exit_status = os.system(command)
525+
try:
526+
with open(outfile, "w") as fout:
527+
subprocess.check_call(command,
528+
cwd=self.texcache,
529+
stdout=fout,
530+
stderr=subprocess.STDOUT)
531+
except subprocess.CalledProcessError as exc:
532+
try:
533+
with open(outfile) as fh:
534+
report = fh.read()
535+
except IOError:
536+
report = 'No dvipng error report available.'
537+
raise RuntimeError(
538+
('dvipng was not able to process the following '
539+
'string:\n%s\ndvipng failed with: %s\n'
540+
'Here is the full report generated by dvipng: '
541+
'\n\n' % (exc.output,
542+
repr(tex.encode('unicode_escape'))) +
543+
report))
516544
try:
517545
with open(outfile) as fh:
518546
report = fh.read()
519547
except IOError:
520548
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')
549+
mpl.verbose.report(report, 'debug')
528550
try:
529551
os.remove(outfile)
530552
except OSError:
@@ -544,21 +566,26 @@ def make_ps(self, tex, fontsize):
544566
if DEBUG or not os.path.exists(psfile):
545567
dvifile = self.make_dvi(tex, fontsize)
546568
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))
569+
command = [str("dvips"), "-q", "-E", "-o",
570+
os.path.basename(psfile),
571+
os.path.basename(dvifile)]
552572
mpl.verbose.report(command, 'debug')
553-
exit_status = os.system(command)
554-
with open(outfile) as fh:
555-
if exit_status:
573+
try:
574+
with open(outfile, "w") as fout:
575+
subprocess.check_call(command,
576+
cwd=self.texcache,
577+
stdout=fout,
578+
stderr=subprocess.STDOUT)
579+
except subprocess.CalledProcessError as exc:
580+
with open(outfile) as fh:
556581
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')
582+
('dvipng was not able to process the following '
583+
'file:\n%s\ndvipng failed with: %s\n'
584+
'Here is the full report generated by dvipng: '
585+
'\n\n' % (dvifile, exc.output) +
586+
fh.read()))
587+
with open(outfile) as fh:
588+
mpl.verbose.report(fh.read(), 'debug')
562589
os.remove(outfile)
563590

564591
return psfile

0 commit comments

Comments
 (0)