66 unicode_literals )
77
88import six
9+ from six .moves import StringIO
910
1011import glob , math , os , shutil , sys , time
1112def _fn_name (): return sys ._getframe (1 ).f_code .co_name
@@ -24,7 +25,7 @@ def _fn_name(): return sys._getframe(1).f_code.co_name
2425 FigureManagerBase , FigureCanvasBase
2526
2627from matplotlib .cbook import is_string_like , get_realpath_and_stat , \
27- is_writable_file_like , maxdict
28+ is_writable_file_like , maxdict , file_requires_unicode
2829from matplotlib .mlab import quad2cubic
2930from matplotlib .figure import Figure
3031
@@ -1085,7 +1086,7 @@ def write(self, *kl, **kwargs):
10851086
10861087 self ._pswriter = NullWriter ()
10871088 else :
1088- self ._pswriter = six . moves . cStringIO ()
1089+ self ._pswriter = io . StringIO ()
10891090
10901091
10911092 # mixed mode rendering
@@ -1105,8 +1106,6 @@ def write(self, *kl, **kwargs):
11051106 self .figure .set_edgecolor (origedgecolor )
11061107
11071108 def print_figure_impl ():
1108- fh = io .TextIOWrapper (raw_fh , encoding = "ascii" )
1109-
11101109 # write the PostScript headers
11111110 if isEPSF : print ("%!PS-Adobe-3.0 EPSF-3.0" , file = fh )
11121111 else : print ("%!PS-Adobe-3.0" , file = fh )
@@ -1157,7 +1156,7 @@ def print_figure_impl():
11571156 fh .flush ()
11581157 convert_ttf_to_ps (
11591158 font_filename .encode (sys .getfilesystemencoding ()),
1160- raw_fh , fonttype , glyph_ids )
1159+ fh , fonttype , glyph_ids )
11611160 print ("end" , file = fh )
11621161 print ("%%EndProlog" , file = fh )
11631162
@@ -1181,22 +1180,31 @@ def print_figure_impl():
11811180 if not isEPSF : print ("%%EOF" , file = fh )
11821181 fh .flush ()
11831182
1184- if six .PY3 :
1185- fh .detach ()
1186-
11871183 if rcParams ['ps.usedistiller' ]:
11881184 # We are going to use an external program to process the output.
11891185 # Write to a temporary file.
11901186 fd , tmpfile = mkstemp ()
1191- with io .open (fd , 'wb' ) as raw_fh :
1187+ with io .open (fd , 'w' , encoding = 'ascii' ) as fh :
11921188 print_figure_impl ()
11931189 else :
11941190 # Write directly to outfile.
11951191 if passed_in_file_object :
1196- raw_fh = outfile
1192+ requires_unicode = file_requires_unicode (outfile )
1193+
1194+ if (not requires_unicode and
1195+ (six .PY3 or not isinstance (outfile , StringIO ))):
1196+ fh = io .TextIOWrapper (outfile , encoding = "ascii" )
1197+ # Prevent the io.TextIOWrapper from closing the
1198+ # underlying file
1199+ def do_nothing ():
1200+ pass
1201+ fh .close = do_nothing
1202+ else :
1203+ fh = outfile
1204+
11971205 print_figure_impl ()
11981206 else :
1199- with io .open (outfile , 'wb' ) as raw_fh :
1207+ with io .open (outfile , 'w' , encoding = 'ascii' ) as fh :
12001208 print_figure_impl ()
12011209
12021210 if rcParams ['ps.usedistiller' ]:
@@ -1206,8 +1214,12 @@ def print_figure_impl():
12061214 xpdf_distill (tmpfile , isEPSF , ptype = papertype , bbox = bbox )
12071215
12081216 if passed_in_file_object :
1209- with io .open (tmpfile , 'rb' ) as fh :
1210- outfile .write (fh .read ())
1217+ if file_requires_unicode (outfile ):
1218+ with io .open (tmpfile , 'rb' ) as fh :
1219+ outfile .write (fh .read ().decode ('ascii' ))
1220+ else :
1221+ with io .open (tmpfile , 'rb' ) as fh :
1222+ outfile .write (fh .read ())
12111223 else :
12121224 with io .open (outfile , 'w' ) as fh :
12131225 pass
@@ -1224,7 +1236,12 @@ def _print_figure_tex(self, outfile, format, dpi, facecolor, edgecolor,
12241236 package. These files are processed to yield the final ps or eps file.
12251237 """
12261238 isEPSF = format == 'eps'
1227- title = outfile
1239+ if is_string_like (outfile ):
1240+ title = outfile
1241+ elif is_writable_file_like (outfile ):
1242+ title = None
1243+ else :
1244+ raise ValueError ("outfile must be a path or a file-like object" )
12281245
12291246 self .figure .dpi = 72 # ignore the dpi kwarg
12301247 width , height = self .figure .get_size_inches ()
@@ -1252,7 +1269,7 @@ def write(self, *kl, **kwargs):
12521269
12531270 self ._pswriter = NullWriter ()
12541271 else :
1255- self ._pswriter = six . moves . cStringIO ()
1272+ self ._pswriter = io . StringIO ()
12561273
12571274
12581275 # mixed mode rendering
@@ -1273,11 +1290,7 @@ def write(self, *kl, **kwargs):
12731290
12741291 # write to a temp file, we'll move it to outfile when done
12751292 fd , tmpfile = mkstemp ()
1276- if six .PY3 :
1277- fh = io .open (fd , 'w' , encoding = 'ascii' )
1278- else :
1279- fh = io .open (fd , 'wb' )
1280- with fh :
1293+ with io .open (fd , 'w' , encoding = 'ascii' ) as fh :
12811294 # write the Encapsulated PostScript headers
12821295 print ("%!PS-Adobe-3.0 EPSF-3.0" , file = fh )
12831296 if title : print ("%%Title: " + title , file = fh )
@@ -1357,17 +1370,13 @@ def write(self, *kl, **kwargs):
13571370 else : gs_distill (tmpfile , isEPSF , ptype = papertype , bbox = bbox ,
13581371 rotated = psfrag_rotated )
13591372
1360- is_file = False
1361- if six .PY3 :
1362- if isinstance (outfile , io .IOBase ):
1363- is_file = True
1364- else :
1365- if isinstance (outfile , file ):
1366- is_file = True
1367-
1368- if is_file :
1369- with io .open (tmpfile , 'rb' ) as fh :
1370- outfile .write (fh .read ())
1373+ if is_writable_file_like (outfile ):
1374+ if file_requires_unicode (outfile ):
1375+ with io .open (tmpfile , 'rb' ) as fh :
1376+ outfile .write (fh .read ().decode ('ascii' ))
1377+ else :
1378+ with io .open (tmpfile , 'rb' ) as fh :
1379+ outfile .write (fh .read ())
13711380 else :
13721381 with io .open (outfile , 'wb' ) as fh :
13731382 pass
0 commit comments