54
54
from matplotlib import rcParams
55
55
from matplotlib ._png import read_png
56
56
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
58
58
import matplotlib .dviread as dviread
59
59
import re
60
60
@@ -398,30 +398,33 @@ def make_dvi(self, tex, fontsize):
398
398
if DEBUG or not os .path .exists (dvifile ):
399
399
texfile = self .make_tex (tex , fontsize )
400
400
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 )]
405
403
mpl .verbose .report (command , 'debug' )
406
404
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\n Here 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\n LaTeX 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.'
425
428
mpl .verbose .report (report , 'debug' )
426
429
for fname in glob .glob (basefile + '*' ):
427
430
if fname .endswith ('dvi' ):
@@ -452,25 +455,34 @@ def make_dvi_preview(self, tex, fontsize):
452
455
not os .path .exists (baselinefile )):
453
456
texfile = self .make_tex_preview (tex , fontsize )
454
457
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 )]
459
460
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\n LaTeX 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 ))
461
480
try :
462
481
with open (outfile ) as fh :
463
482
report = fh .read ()
464
-
465
483
except IOError :
466
484
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\n Here 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' )
474
486
475
487
# find the box extent information in the latex output
476
488
# file and store them in ".baseline" file
@@ -506,25 +518,35 @@ def make_png(self, tex, fontsize, dpi):
506
518
if DEBUG or not os .path .exists (pngfile ):
507
519
dvifile = self .make_dvi (tex , fontsize )
508
520
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 )]
514
524
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\n dvipng 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 ))
516
544
try :
517
545
with open (outfile ) as fh :
518
546
report = fh .read ()
519
547
except IOError :
520
548
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\n Here 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' )
528
550
try :
529
551
os .remove (outfile )
530
552
except OSError :
@@ -544,21 +566,26 @@ def make_ps(self, tex, fontsize):
544
566
if DEBUG or not os .path .exists (psfile ):
545
567
dvifile = self .make_dvi (tex , fontsize )
546
568
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 )]
552
572
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 :
556
581
raise RuntimeError (
557
- 'dvipng was not able to process the flowing '
558
- 'file:\n %s\n Here 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\n dvipng 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' )
562
589
os .remove (outfile )
563
590
564
591
return psfile
0 commit comments