@@ -90,7 +90,11 @@ def gs_version(self):
9090 from subprocess import Popen , PIPE
9191 pipe = Popen (self .gs_exe + " --version" ,
9292 shell = True , stdout = PIPE ).stdout
93- gs_version = tuple (map (int , pipe .read ().strip ().split ("." )))
93+ if sys .version_info [0 ] >= 3 :
94+ ver = pipe .read ().decode ('ascii' )
95+ else :
96+ ver = pipe .read ()
97+ gs_version = tuple (map (int , ver .strip ().split ("." )))
9498
9599 self ._cached ["gs_version" ] = gs_version
96100 return gs_version
@@ -1198,7 +1202,10 @@ def write(self, *kl, **kwargs):
11981202
11991203 self ._pswriter = NullWriter ()
12001204 else :
1201- self ._pswriter = StringIO ()
1205+ if sys .version_info [0 ] >= 3 :
1206+ self ._pswriter = io .StringIO ()
1207+ else :
1208+ self ._pswriter = cStringIO .StringIO ()
12021209
12031210
12041211 # mixed mode rendering
@@ -1219,7 +1226,11 @@ def write(self, *kl, **kwargs):
12191226
12201227 # write to a temp file, we'll move it to outfile when done
12211228 fd , tmpfile = mkstemp ()
1222- with io .fdopen (fd , 'w' , encoding = 'ascii' ) as fh :
1229+ if sys .version_info [0 ] >= 3 :
1230+ fh = io .open (fd , 'w' , encoding = 'ascii' )
1231+ else :
1232+ fh = io .open (fd , 'wb' )
1233+ with fh :
12231234 # write the Encapsulated PostScript headers
12241235 print ("%!PS-Adobe-3.0 EPSF-3.0" , file = fh )
12251236 if title : print ("%%Title: " + title , file = fh )
@@ -1298,7 +1309,15 @@ def write(self, *kl, **kwargs):
12981309 else : gs_distill (tmpfile , isEPSF , ptype = papertype , bbox = bbox ,
12991310 rotated = psfrag_rotated )
13001311
1301- if isinstance (outfile , file ):
1312+ is_file = False
1313+ if sys .version_info [0 ] >= 3 :
1314+ if isinstance (outfile , io .IOBase ):
1315+ is_file = True
1316+ else :
1317+ if isinstance (outfile , file ):
1318+ is_file = True
1319+
1320+ if is_file :
13021321 with open (tmpfile , 'rb' ) as fh :
13031322 outfile .write (fh .read ())
13041323 else :
@@ -1355,12 +1374,12 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
13551374 paperWidth , paperHeight ,
13561375 '\n ' .join (psfrags ), angle , os .path .split (epsfile )[- 1 ])
13571376
1358- with io .open (latexfile , 'w' , encoding = 'ascii ' ) as latexh :
1377+ with io .open (latexfile , 'wb ' ) as latexh :
13591378 if rcParams ['text.latex.unicode' ]:
13601379 latexh .write (s .encode ('utf8' ))
13611380 else :
13621381 try :
1363- latexh .write (s )
1382+ latexh .write (s . encode ( 'ascii' ) )
13641383 except UnicodeEncodeError :
13651384 verbose .report ("You are using unicode and latex, but have "
13661385 "not enabled the matplotlib 'text.latex.unicode' "
@@ -1375,7 +1394,7 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
13751394 % (precmd , tmpdir , latexfile , outfile )
13761395 verbose .report (command , 'debug' )
13771396 exit_status = os .system (command )
1378-
1397+
13791398 with io .open (outfile , 'rb' ) as fh :
13801399 if exit_status :
13811400 raise RuntimeError ('LaTeX was not able to process your file:\
@@ -1447,7 +1466,7 @@ def gs_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
14471466 verbose .report (command , 'debug' )
14481467 exit_status = os .system (command )
14491468
1450- with io .open (outfile , 'rb' ):
1469+ with io .open (outfile , 'rb' ) as fh :
14511470 if exit_status :
14521471 raise RuntimeError ('ghostscript was not able to process \
14531472 your image.\n Here is the full report generated by ghostscript:\n \n ' + fh .read ())
@@ -1597,55 +1616,56 @@ def pstoeps(tmpfile, bbox=None, rotated=False):
15971616 bbox_info , rotate = None , None
15981617
15991618 epsfile = tmpfile + '.eps'
1600- with io .open (epsfile , 'w' , encoding = 'ascii' ) as epsh :
1601- with io .open (tmpfile , 'r' , encoding = 'ascii' ) as tmph :
1619+ with io .open (epsfile , 'wb' ) as epsh :
1620+ write = epsh .write
1621+ with io .open (tmpfile , 'rb' ) as tmph :
16021622 line = tmph .readline ()
16031623 # Modify the header:
16041624 while line :
1605- if line .startswith ('%!PS' ):
1606- print ( "%!PS-Adobe-3.0 EPSF-3.0" , file = epsh )
1625+ if line .startswith (b '%!PS' ):
1626+ write ( b "%!PS-Adobe-3.0 EPSF-3.0\n " )
16071627 if bbox :
1608- print (bbox_info , file = epsh )
1609- elif line .startswith ('%%EndComments' ):
1610- epsh . write (line )
1611- print ( '%%BeginProlog' , file = epsh )
1612- print ( 'save' , file = epsh )
1613- print ( 'countdictstack' , file = epsh )
1614- print ( 'mark' , file = epsh )
1615- print ( 'newpath' , file = epsh )
1616- print ( '/showpage {} def' , file = epsh )
1617- print ( '/setpagedevice {pop} def' , file = epsh )
1618- print ( '%%EndProlog' , file = epsh )
1619- print ( '%%Page 1 1' , file = epsh )
1628+ write (bbox_info . encode ( 'ascii' ) + b' \n ' )
1629+ elif line .startswith (b '%%EndComments' ):
1630+ write (line )
1631+ write ( b '%%BeginProlog\n ' )
1632+ write ( b 'save\n ' )
1633+ write ( b 'countdictstack\n ' )
1634+ write ( b 'mark\n ' )
1635+ write ( b 'newpath\n ' )
1636+ write ( b '/showpage {} def\n ' )
1637+ write ( b '/setpagedevice {pop} def\n ' )
1638+ write ( b '%%EndProlog\n ' )
1639+ write ( b '%%Page 1 1\n ' )
16201640 if rotate :
1621- print (rotate , file = epsh )
1641+ write (rotate . encode ( 'ascii' ) + b' \n ' )
16221642 break
1623- elif bbox and (line .startswith ('%%Bound' ) \
1624- or line .startswith ('%%HiResBound' ) \
1625- or line .startswith ('%%DocumentMedia' ) \
1626- or line .startswith ('%%Pages' )):
1643+ elif bbox and (line .startswith (b '%%Bound' ) \
1644+ or line .startswith (b '%%HiResBound' ) \
1645+ or line .startswith (b '%%DocumentMedia' ) \
1646+ or line .startswith (b '%%Pages' )):
16271647 pass
16281648 else :
1629- epsh . write (line )
1649+ write (line )
16301650 line = tmph .readline ()
16311651 # Now rewrite the rest of the file, and modify the trailer.
16321652 # This is done in a second loop such that the header of the embedded
16331653 # eps file is not modified.
16341654 line = tmph .readline ()
16351655 while line :
1636- if line .startswith ('%%Trailer' ):
1637- print ( '%%Trailer' , file = epsh )
1638- print ( 'cleartomark' , file = epsh )
1639- print ( 'countdictstack' , file = epsh )
1640- print ( 'exch sub { end } repeat' , file = epsh )
1641- print ( 'restore' , file = epsh )
1656+ if line .startswith (b '%%Trailer' ):
1657+ write ( b '%%Trailer\n ' )
1658+ write ( b 'cleartomark\n ' )
1659+ write ( b 'countdictstack\n ' )
1660+ write ( b 'exch sub { end } repeat\n ' )
1661+ write ( b 'restore\n ' )
16421662 if rcParams ['ps.usedistiller' ] == 'xpdf' :
16431663 # remove extraneous "end" operator:
16441664 line = tmph .readline ()
1645- elif line .startswith ('%%PageBoundingBox' ):
1665+ elif line .startswith (b '%%PageBoundingBox' ):
16461666 pass
16471667 else :
1648- epsh . write (line )
1668+ write (line )
16491669 line = tmph .readline ()
16501670
16511671 os .remove (tmpfile )
0 commit comments