Skip to content

Commit 68a6cc7

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

File tree

1 file changed

+72
-49
lines changed

1 file changed

+72
-49
lines changed

lib/matplotlib/texmanager.py

Lines changed: 72 additions & 49 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,36 @@ 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+
os.stat(dvifile)
426+
exists = True
427+
except OSError:
428+
exists = False
429+
if not exists:
430+
raise RuntimeError(
431+
('LaTeX was not able to process the following '
432+
'string:\n%s\n'
433+
'Here is the full report generated by LaTeX: '
434+
'\n\n' % repr(tex.encode('unicode_escape')) +
435+
report))
436+
else:
437+
mpl.verbose.report(report, 'debug')
426438
for fname in glob.glob(basefile + '*'):
427439
if fname.endswith('dvi'):
428440
pass
@@ -457,20 +469,23 @@ def make_dvi_preview(self, tex, fontsize):
457469
'latex -interaction=nonstopmode %s > "%s"' %
458470
(os.path.split(texfile)[-1], outfile))
459471
mpl.verbose.report(command, 'debug')
460-
exit_status = os.system(command)
461472
try:
462-
with open(outfile) as fh:
463-
report = fh.read()
464-
465-
except IOError:
466-
report = 'No latex error report available.'
467-
if exit_status:
473+
output = subprocess.check_output(command, shell=True,
474+
stderr=subprocess.STDOUT)
475+
except subprocess.CalledProcessError as exc:
476+
try:
477+
with open(outfile) as fh:
478+
report = fh.read()
479+
except IOError:
480+
report = 'No latex error report available.'
468481
raise RuntimeError(
469482
('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')
483+
'string:\n%s\nLaTeX failed with: %s\n'
484+
'Here is the full report generated by LaTeX: '
485+
'\n\n' % (exc.output,
486+
repr(tex.encode('unicode_escape'))) +
487+
report))
488+
mpl.verbose.report(report, 'debug')
474489

475490
# find the box extent information in the latex output
476491
# file and store them in ".baseline" file
@@ -512,19 +527,23 @@ def make_png(self, tex, fontsize, dpi):
512527
(dpi, os.path.split(pngfile)[-1],
513528
os.path.split(dvifile)[-1], outfile))
514529
mpl.verbose.report(command, 'debug')
515-
exit_status = os.system(command)
516530
try:
517-
with open(outfile) as fh:
518-
report = fh.read()
519-
except IOError:
520-
report = 'No dvipng error report available.'
521-
if exit_status:
531+
output = subprocess.check_output(command, shell=True,
532+
stderr=subprocess.STDOUT)
533+
except subprocess.CalledProcessError as exc:
534+
try:
535+
with open(outfile) as fh:
536+
report = fh.read()
537+
except IOError:
538+
report = 'No dvipng error report available.'
522539
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')
540+
('dvipng was not able to process the following '
541+
'string:\n%s\ndvipng failed with: %s\n'
542+
'Here is the full report generated by dvipng: '
543+
'\n\n' % (exc.output,
544+
repr(tex.encode('unicode_escape'))) +
545+
report))
546+
mpl.verbose.report(report, 'debug')
528547
try:
529548
os.remove(outfile)
530549
except OSError:
@@ -550,15 +569,19 @@ def make_ps(self, tex, fontsize):
550569
(os.path.split(psfile)[-1],
551570
os.path.split(dvifile)[-1], outfile))
552571
mpl.verbose.report(command, 'debug')
553-
exit_status = os.system(command)
554-
with open(outfile) as fh:
555-
if exit_status:
572+
try:
573+
output = subprocess.check_output(command, shell=True,
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)