Skip to content

Commit 5208d74

Browse files
committed
TexManager: Use subprocss instead of os.system
1 parent ffc993c commit 5208d74

File tree

1 file changed

+82
-44
lines changed

1 file changed

+82
-44
lines changed

lib/matplotlib/texmanager.py

Lines changed: 82 additions & 44 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

@@ -404,25 +405,41 @@ def make_dvi(self, tex, fontsize):
404405
(os.path.split(texfile)[-1], outfile))
405406
mpl.verbose.report(command, 'debug')
406407
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:
425-
mpl.verbose.report(report, 'debug')
408+
try:
409+
output = subprocess.check_output(command, shell=True,
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' % (exc.output,
422+
repr(tex.encode('unicode_escape'))) +
423+
report))
424+
try:
425+
with open(outfile) as fh:
426+
report = fh.read()
427+
except IOError:
428+
report = 'No latex error report available.'
429+
try:
430+
os.stat(dvifile)
431+
exists = True
432+
except OSError:
433+
exists = False
434+
if not exists:
435+
raise RuntimeError(
436+
('LaTeX was not able to process the following '
437+
'string:\n%s\n'
438+
'Here is the full report generated by LaTeX: '
439+
'\n\n' % repr(tex.encode('unicode_escape')) +
440+
report))
441+
else:
442+
mpl.verbose.report(report, 'debug')
426443
for fname in glob.glob(basefile + '*'):
427444
if fname.endswith('dvi'):
428445
pass
@@ -457,20 +474,28 @@ def make_dvi_preview(self, tex, fontsize):
457474
'latex -interaction=nonstopmode %s > "%s"' %
458475
(os.path.split(texfile)[-1], outfile))
459476
mpl.verbose.report(command, 'debug')
460-
exit_status = os.system(command)
477+
try:
478+
output = subprocess.check_output(command, shell=True,
479+
stderr=subprocess.STDOUT)
480+
except subprocess.CalledProcessError as exc:
481+
try:
482+
with open(outfile) as fh:
483+
report = fh.read()
484+
except IOError:
485+
report = 'No latex error report available.'
486+
raise RuntimeError(
487+
('LaTeX was not able to process the following '
488+
'string:\n%s\nLaTeX failed with: %s\n'
489+
'Here is the full report generated by LaTeX: '
490+
'\n\n' % (exc.output,
491+
repr(tex.encode('unicode_escape'))) +
492+
report))
461493
try:
462494
with open(outfile) as fh:
463495
report = fh.read()
464-
465496
except IOError:
466497
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')
498+
mpl.verbose.report(report, 'debug')
474499

475500
# find the box extent information in the latex output
476501
# file and store them in ".baseline" file
@@ -512,19 +537,28 @@ def make_png(self, tex, fontsize, dpi):
512537
(dpi, os.path.split(pngfile)[-1],
513538
os.path.split(dvifile)[-1], outfile))
514539
mpl.verbose.report(command, 'debug')
515-
exit_status = os.system(command)
540+
try:
541+
output = subprocess.check_output(command, shell=True,
542+
stderr=subprocess.STDOUT)
543+
except subprocess.CalledProcessError as exc:
544+
try:
545+
with open(outfile) as fh:
546+
report = fh.read()
547+
except IOError:
548+
report = 'No dvipng error report available.'
549+
raise RuntimeError(
550+
('dvipng was not able to process the following '
551+
'string:\n%s\ndvipng failed with: %s\n'
552+
'Here is the full report generated by dvipng: '
553+
'\n\n' % (exc.output,
554+
repr(tex.encode('unicode_escape'))) +
555+
report))
516556
try:
517557
with open(outfile) as fh:
518558
report = fh.read()
519559
except IOError:
520560
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')
561+
mpl.verbose.report(report, 'debug')
528562
try:
529563
os.remove(outfile)
530564
except OSError:
@@ -550,15 +584,19 @@ def make_ps(self, tex, fontsize):
550584
(os.path.split(psfile)[-1],
551585
os.path.split(dvifile)[-1], outfile))
552586
mpl.verbose.report(command, 'debug')
553-
exit_status = os.system(command)
554-
with open(outfile) as fh:
555-
if exit_status:
587+
try:
588+
output = subprocess.check_output(command, shell=True,
589+
stderr=subprocess.STDOUT)
590+
except subprocess.CalledProcessError as exc:
591+
with open(outfile) as fh:
556592
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')
593+
('dvipng was not able to process the following '
594+
'file:\n%s\ndvipng failed with: %s\n'
595+
'Here is the full report generated by dvipng: '
596+
'\n\n' % (dvifile, exc.output) +
597+
fh.read()))
598+
with open(outfile) as fh:
599+
mpl.verbose.report(fh.read(), 'debug')
562600
os.remove(outfile)
563601

564602
return psfile

0 commit comments

Comments
 (0)