5
5
# PY3KTODO: Get rid of "print >>fh" syntax
6
6
7
7
from __future__ import division , print_function
8
- import contextlib
9
8
import glob , math , os , shutil , sys , time
10
9
def _fn_name (): return sys ._getframe (1 ).f_code .co_name
11
10
import io
@@ -974,11 +973,6 @@ def print_ps(self, outfile, *args, **kwargs):
974
973
def print_eps (self , outfile , * args , ** kwargs ):
975
974
return self ._print_ps (outfile , 'eps' , * args , ** kwargs )
976
975
977
-
978
-
979
-
980
-
981
-
982
976
def _print_ps (self , outfile , format , * args , ** kwargs ):
983
977
papertype = kwargs .pop ("papertype" , rcParams ['ps.papersize' ])
984
978
papertype = papertype .lower ()
@@ -1106,21 +1100,7 @@ def write(self, *kl, **kwargs):
1106
1100
self .figure .set_facecolor (origfacecolor )
1107
1101
self .figure .set_edgecolor (origedgecolor )
1108
1102
1109
- if rcParams ['ps.usedistiller' ]:
1110
- # We are going to use an external program to process the output.
1111
- # Write to a temporary file.
1112
- fd , tmpfile = mkstemp ()
1113
- context_manager = io .open (fd , 'wb' )
1114
- else :
1115
- # Write directly to outfile.
1116
- if passed_in_file_object :
1117
- @contextlib .contextmanager
1118
- def null_context (value ):
1119
- yield value
1120
- context_manager = null_context (outfile )
1121
- else :
1122
- context_manager = open (outfile , 'wb' )
1123
- with context_manager as raw_fh :
1103
+ def print_figure_impl ():
1124
1104
if sys .version_info [0 ] >= 3 :
1125
1105
fh = io .TextIOWrapper (raw_fh , encoding = "ascii" )
1126
1106
else :
@@ -1195,6 +1175,24 @@ def null_context(value):
1195
1175
if not isEPSF : print ("%%EOF" , file = fh )
1196
1176
fh .flush ()
1197
1177
1178
+ if sys .version_info [0 ] >= 3 :
1179
+ fh .detach ()
1180
+
1181
+ if rcParams ['ps.usedistiller' ]:
1182
+ # We are going to use an external program to process the output.
1183
+ # Write to a temporary file.
1184
+ fd , tmpfile = mkstemp ()
1185
+ with io .open (fd , 'wb' ) as raw_fh :
1186
+ print_figure_impl ()
1187
+ else :
1188
+ # Write directly to outfile.
1189
+ if passed_in_file_object :
1190
+ raw_fh = outfile
1191
+ print_figure_impl ()
1192
+ else :
1193
+ with open (outfile , 'wb' ) as raw_fh :
1194
+ print_figure_impl ()
1195
+
1198
1196
if rcParams ['ps.usedistiller' ]:
1199
1197
if rcParams ['ps.usedistiller' ] == 'ghostscript' :
1200
1198
gs_distill (tmpfile , isEPSF , ptype = papertype , bbox = bbox )
0 commit comments