@@ -90,7 +90,11 @@ def gs_version(self):
90
90
from subprocess import Popen , PIPE
91
91
pipe = Popen (self .gs_exe + " --version" ,
92
92
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 ("." )))
94
98
95
99
self ._cached ["gs_version" ] = gs_version
96
100
return gs_version
@@ -1198,7 +1202,10 @@ def write(self, *kl, **kwargs):
1198
1202
1199
1203
self ._pswriter = NullWriter ()
1200
1204
else :
1201
- self ._pswriter = StringIO ()
1205
+ if sys .version_info [0 ] >= 3 :
1206
+ self ._pswriter = io .StringIO ()
1207
+ else :
1208
+ self ._pswriter = cStringIO .StringIO ()
1202
1209
1203
1210
1204
1211
# mixed mode rendering
@@ -1219,7 +1226,11 @@ def write(self, *kl, **kwargs):
1219
1226
1220
1227
# write to a temp file, we'll move it to outfile when done
1221
1228
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 :
1223
1234
# write the Encapsulated PostScript headers
1224
1235
print ("%!PS-Adobe-3.0 EPSF-3.0" , file = fh )
1225
1236
if title : print ("%%Title: " + title , file = fh )
@@ -1298,7 +1309,15 @@ def write(self, *kl, **kwargs):
1298
1309
else : gs_distill (tmpfile , isEPSF , ptype = papertype , bbox = bbox ,
1299
1310
rotated = psfrag_rotated )
1300
1311
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 :
1302
1321
with open (tmpfile , 'rb' ) as fh :
1303
1322
outfile .write (fh .read ())
1304
1323
else :
@@ -1355,12 +1374,12 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
1355
1374
paperWidth , paperHeight ,
1356
1375
'\n ' .join (psfrags ), angle , os .path .split (epsfile )[- 1 ])
1357
1376
1358
- with io .open (latexfile , 'w' , encoding = 'ascii ' ) as latexh :
1377
+ with io .open (latexfile , 'wb ' ) as latexh :
1359
1378
if rcParams ['text.latex.unicode' ]:
1360
1379
latexh .write (s .encode ('utf8' ))
1361
1380
else :
1362
1381
try :
1363
- latexh .write (s )
1382
+ latexh .write (s . encode ( 'ascii' ) )
1364
1383
except UnicodeEncodeError :
1365
1384
verbose .report ("You are using unicode and latex, but have "
1366
1385
"not enabled the matplotlib 'text.latex.unicode' "
@@ -1375,7 +1394,7 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
1375
1394
% (precmd , tmpdir , latexfile , outfile )
1376
1395
verbose .report (command , 'debug' )
1377
1396
exit_status = os .system (command )
1378
-
1397
+
1379
1398
with io .open (outfile , 'rb' ) as fh :
1380
1399
if exit_status :
1381
1400
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):
1447
1466
verbose .report (command , 'debug' )
1448
1467
exit_status = os .system (command )
1449
1468
1450
- with io .open (outfile , 'rb' ):
1469
+ with io .open (outfile , 'rb' ) as fh :
1451
1470
if exit_status :
1452
1471
raise RuntimeError ('ghostscript was not able to process \
1453
1472
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):
1597
1616
bbox_info , rotate = None , None
1598
1617
1599
1618
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 :
1602
1622
line = tmph .readline ()
1603
1623
# Modify the header:
1604
1624
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 " )
1607
1627
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 ' )
1620
1640
if rotate :
1621
- print (rotate , file = epsh )
1641
+ write (rotate . encode ( 'ascii' ) + b' \n ' )
1622
1642
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' )):
1627
1647
pass
1628
1648
else :
1629
- epsh . write (line )
1649
+ write (line )
1630
1650
line = tmph .readline ()
1631
1651
# Now rewrite the rest of the file, and modify the trailer.
1632
1652
# This is done in a second loop such that the header of the embedded
1633
1653
# eps file is not modified.
1634
1654
line = tmph .readline ()
1635
1655
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 ' )
1642
1662
if rcParams ['ps.usedistiller' ] == 'xpdf' :
1643
1663
# remove extraneous "end" operator:
1644
1664
line = tmph .readline ()
1645
- elif line .startswith ('%%PageBoundingBox' ):
1665
+ elif line .startswith (b '%%PageBoundingBox' ):
1646
1666
pass
1647
1667
else :
1648
- epsh . write (line )
1668
+ write (line )
1649
1669
line = tmph .readline ()
1650
1670
1651
1671
os .remove (tmpfile )
0 commit comments