@@ -23,6 +23,7 @@ def _fn_name(): return sys._getframe(1).f_code.co_name
2323
2424from matplotlib .cbook import is_string_like , get_realpath_and_stat , \
2525 is_writable_file_like , maxdict , file_requires_unicode
26+ from matplotlib .compat .subprocess import subprocess
2627from matplotlib .figure import Figure
2728
2829from matplotlib .font_manager import findfont , is_opentype_cff_font , get_font
@@ -83,8 +84,7 @@ def gs_version(self):
8384 pass
8485
8586 from matplotlib .compat .subprocess import Popen , PIPE
86- s = Popen (self .gs_exe + " --version" ,
87- shell = True , stdout = PIPE )
87+ s = Popen ([self .gs_exe , "--version" ], stdout = PIPE )
8888 pipe , stderr = s .communicate ()
8989 if six .PY3 :
9090 ver = pipe .decode ('ascii' )
@@ -1469,27 +1469,32 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
14691469 command = '%s cd "%s" && latex -interaction=nonstopmode "%s" > "%s"' \
14701470 % (precmd , tmpdir , latexfile , outfile )
14711471 verbose .report (command , 'debug' )
1472- exit_status = os .system (command )
1473-
1474- with io .open (outfile , 'rb' ) as fh :
1475- if exit_status :
1476- raise RuntimeError ('LaTeX was not able to process your file:\
1477- \n Here is the full report generated by LaTeX: \n \n %s'% fh .read ())
1478- else :
1472+ try :
1473+ output = subprocess .check_output (command , shell = True ,
1474+ stderr = subprocess .STDOUT )
1475+ except subprocess .CalledProcessError as exc :
1476+ with io .open (outfile , 'rb' ) as fh :
1477+ raise RuntimeError ('LaTeX was not able to process your file: '
1478+ 'Here is the full report generated by LaTeX'
1479+ '\n \n %s' % (fh .read ()))
1480+ else :
1481+ with io .open (outfile , 'rb' ) as fh :
14791482 verbose .report (fh .read (), 'debug' )
14801483 os .remove (outfile )
14811484
14821485 command = '%s cd "%s" && dvips -q -R0 -o "%s" "%s" > "%s"' % (precmd , tmpdir ,
14831486 os .path .split (psfile )[- 1 ], os .path .split (dvifile )[- 1 ], outfile )
14841487 verbose .report (command , 'debug' )
1485- exit_status = os .system (command )
1486-
1487- with io .open (outfile , 'rb' ) as fh :
1488- if exit_status :
1489- raise RuntimeError ('dvips was not able to \
1490- process the following file:\n %s\n Here is the full report generated by dvips: \
1491- \n \n '% dvifile + fh .read ())
1492- else :
1488+ try :
1489+ output = subprocess .check_output (command , shell = True ,
1490+ stderr = subprocess .STDOUT )
1491+ except subprocess .CalledProcessError as exc :
1492+ with io .open (outfile , 'rb' ) as fh :
1493+ raise RuntimeError ('dvips was not able to process the following '
1494+ 'file:\n %s\n Here is the full report generated '
1495+ 'by dvips: \n \n ' % dvifile + fh .read ())
1496+ else :
1497+ with io .open (outfile , 'rb' ) as fh :
14931498 verbose .report (fh .read (), 'debug' )
14941499 os .remove (outfile )
14951500 os .remove (epsfile )
@@ -1528,30 +1533,27 @@ def gs_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
15281533 outfile = tmpfile + '.output'
15291534 dpi = rcParams ['ps.distiller.res' ]
15301535
1531-
15321536 gs_exe = ps_backend_helper .gs_exe
15331537 if ps_backend_helper .supports_ps2write : # gs version >= 9
15341538 device_name = "ps2write"
15351539 else :
15361540 device_name = "pswrite"
15371541
1538- command = '%s -dBATCH -dNOPAUSE -r%d -sDEVICE=%s %s -sOutputFile="%s" \
1539- "%s" > "%s"'% (gs_exe , dpi , device_name ,
1540- paper_option , psfile , tmpfile , outfile )
1541-
1542+ command = [gs_exe , "-dBATCH" , "-dNOPAUSE" , "-r%d" % dpi ,
1543+ "-sDEVICE=%s" % device_name , paper_option ,
1544+ "-sOutputFile=%s" % psfile , tmpfile ]
15421545 verbose .report (command , 'debug' )
1543- exit_status = os .system (command )
1544-
1545- with io .open (outfile , 'rb' ) as fh :
1546- if exit_status :
1547- output = fh .read ()
1548- m = "\n " .join (["ghostscript was not able to process your image." ,
1549- "Here is the full report generated by ghostscript:" ,
1550- "" ,
1551- "%s" ])
1552- # use % to prevent problems with bytes
1553- raise RuntimeError (m % output )
1554- else :
1546+ try :
1547+ with open (outfile , "w" ) as fout :
1548+ subprocess .check_call (command ,
1549+ stdout = fout , stderr = subprocess .STDOUT )
1550+ except subprocess .CalledProcessError as exc :
1551+ with io .open (outfile , 'rb' ) as fh :
1552+ raise RuntimeError ('ghostscript was not able to process your '
1553+ 'image.\n Here is the full report generated by '
1554+ 'ghostscript: %s\n \n ' % fh .read ())
1555+ else :
1556+ with io .open (outfile , 'rb' ) as fh :
15551557 verbose .report (fh .read (), 'debug' )
15561558 os .remove (outfile )
15571559 os .remove (tmpfile )
@@ -1584,33 +1586,55 @@ def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
15841586 psfile = tmpfile + '.ps'
15851587 outfile = tmpfile + '.output'
15861588
1587- if eps : paper_option = "-dEPSCrop"
1588- else : paper_option = "-sPAPERSIZE=%s" % ptype
1589-
1590- command = 'ps2pdf -dAutoFilterColorImages=false \
1591- -dAutoFilterGrayImages=false -sGrayImageFilter=FlateEncode \
1592- -sColorImageFilter=FlateEncode %s "%s" "%s" > "%s"'% \
1593- (paper_option , tmpfile , pdffile , outfile )
1594- if sys .platform == 'win32' : command = command .replace ('=' , '#' )
1595- verbose .report (command , 'debug' )
1596- exit_status = os .system (command )
1597- with io .open (outfile , 'rb' ) as fh :
1598- if exit_status :
1599- raise RuntimeError ('ps2pdf was not able to process your \
1600- image.\n Here is the report generated by ghostscript:\n \n ' + fh .read ())
1589+ if eps :
1590+ paper_option = "-dEPSCrop"
1591+ else :
1592+ if sys .platform == "win32" :
1593+ paper_option = "-sPAPERSIZE#%s" % ptype
16011594 else :
1595+ paper_option = "-sPAPERSIZE=%s" % ptype
1596+
1597+ if sys .platform == "win32" :
1598+ command = ["ps2pdf" , "-dAutoFilterColorImages#false" ,
1599+ "-dAutoFilterGrayImages#false" ,
1600+ "-sGrayImageFilter#FlateEncode" ,
1601+ "-sColorImageFilter#FlateEncode" , paper_option , tmpfile ,
1602+ pdffile ]
1603+ else :
1604+ command = ["ps2pdf" , "-dAutoFilterColorImages=false" ,
1605+ "-dAutoFilterGrayImages=false" ,
1606+ "-sGrayImageFilter=FlateEncode" ,
1607+ "-sColorImageFilter=FlateEncode" , paper_option , tmpfile ,
1608+ pdffile ]
1609+ verbose .report (command , 'debug' )
1610+
1611+ try :
1612+ with open (outfile , "w" ) as fout :
1613+ subprocess .check_call (command ,
1614+ stdout = fout , stderr = subprocess .STDOUT )
1615+ except subprocess .CalledProcessError as exc :
1616+ with io .open (outfile , 'rb' ) as fh :
1617+ raise RuntimeError ('ps2pdf was not able to process your image.\n '
1618+ 'Here is the report generated by ps2pdf: '
1619+ '%s\n \n ' % fh .read ())
1620+ else :
1621+ with io .open (outfile , 'rb' ) as fh :
16021622 verbose .report (fh .read (), 'debug' )
16031623 os .remove (outfile )
1604- command = 'pdftops -paper match -level2 "%s" "%s" > "%s"' % \
1605- (pdffile , psfile , outfile )
1606- verbose .report (command , 'debug' )
1607- exit_status = os .system (command )
16081624
1609- with io .open (outfile , 'rb' ) as fh :
1610- if exit_status :
1611- raise RuntimeError ('pdftops was not able to process your \
1612- image.\n Here is the full report generated by pdftops: \n \n ' + fh .read ())
1613- else :
1625+ command = ["pdftops" , "-paper" , "match" , "-level2" , pdffile , psfile ]
1626+ verbose .report (command , 'debug' )
1627+ try :
1628+ with open (outfile , "w" ) as fout :
1629+ subprocess .check_call (command ,
1630+ stdout = fout , stderr = subprocess .STDOUT )
1631+ except subprocess .CalledProcessError as exc :
1632+ with io .open (outfile , 'rb' ) as fh :
1633+ raise RuntimeError ('pdftops was not able to process your image.\n '
1634+ 'Here is the full report generated by pdftops: '
1635+ '%s\n \n ' % fh .read ())
1636+ else :
1637+ with io .open (outfile , 'rb' ) as fh :
16141638 verbose .report (fh .read (), 'debug' )
16151639 os .remove (outfile )
16161640 os .remove (tmpfile )
0 commit comments