|
18 | 18 |
|
19 | 19 | import numpy as np |
20 | 20 |
|
21 | | -from matplotlib import __version__, rcParams |
| 21 | +from matplotlib import cbook, __version__, rcParams |
22 | 22 | from matplotlib.backend_bases import ( |
23 | 23 | _Backend, FigureCanvasBase, FigureManagerBase, RendererBase) |
24 | 24 | from matplotlib.backends.backend_mixed import MixedModeRenderer |
25 | | -from matplotlib.cbook import is_writable_file_like |
26 | 25 | from matplotlib.colors import rgb2hex |
27 | 26 | from matplotlib.font_manager import findfont, get_font |
28 | 27 | from matplotlib.ft2font import LOAD_NO_HINTING |
@@ -1189,61 +1188,49 @@ class FigureCanvasSVG(FigureCanvasBase): |
1189 | 1188 | fixed_dpi = 72 |
1190 | 1189 |
|
1191 | 1190 | def print_svg(self, filename, *args, **kwargs): |
1192 | | - if isinstance(filename, six.string_types): |
1193 | | - with io.open(filename, 'w', encoding='utf-8') as svgwriter: |
1194 | | - return self._print_svg(filename, svgwriter, **kwargs) |
| 1191 | + with cbook.open_file_cm(filename, "w", encoding="utf-8") as fh: |
1195 | 1192 |
|
1196 | | - if not is_writable_file_like(filename): |
1197 | | - raise ValueError("filename must be a path or a file-like object") |
| 1193 | + filename = getattr(fh, 'name', '') |
| 1194 | + if not isinstance(filename, six.string_types): |
| 1195 | + filename = '' |
1198 | 1196 |
|
1199 | | - svgwriter = filename |
1200 | | - filename = getattr(svgwriter, 'name', '') |
1201 | | - if not isinstance(filename, six.string_types): |
1202 | | - filename = '' |
1203 | | - |
1204 | | - if not isinstance(svgwriter, io.TextIOBase): |
1205 | | - if six.PY3: |
1206 | | - svgwriter = io.TextIOWrapper(svgwriter, 'utf-8') |
| 1197 | + if cbook.file_requires_unicode(fh): |
| 1198 | + detach = False |
1207 | 1199 | else: |
1208 | | - svgwriter = codecs.getwriter('utf-8')(svgwriter) |
1209 | | - detach = True |
1210 | | - else: |
1211 | | - detach = False |
| 1200 | + if six.PY3: |
| 1201 | + fh = io.TextIOWrapper(fh, 'utf-8') |
| 1202 | + else: |
| 1203 | + fh = codecs.getwriter('utf-8')(fh) |
| 1204 | + detach = True |
1212 | 1205 |
|
1213 | | - result = self._print_svg(filename, svgwriter, **kwargs) |
| 1206 | + result = self._print_svg(filename, fh, **kwargs) |
1214 | 1207 |
|
1215 | | - # Detach underlying stream from wrapper so that it remains open in the |
1216 | | - # caller. |
1217 | | - if detach: |
1218 | | - if six.PY3: |
1219 | | - svgwriter.detach() |
1220 | | - else: |
1221 | | - svgwriter.reset() |
1222 | | - svgwriter.stream = io.BytesIO() |
| 1208 | + # Detach underlying stream from wrapper so that it remains open in |
| 1209 | + # the caller. |
| 1210 | + if detach: |
| 1211 | + if six.PY3: |
| 1212 | + fh.detach() |
| 1213 | + else: |
| 1214 | + fh.reset() |
| 1215 | + fh.stream = io.BytesIO() |
1223 | 1216 |
|
1224 | 1217 | return result |
1225 | 1218 |
|
1226 | 1219 | def print_svgz(self, filename, *args, **kwargs): |
1227 | | - if isinstance(filename, six.string_types): |
1228 | | - options = dict(filename=filename) |
1229 | | - elif is_writable_file_like(filename): |
1230 | | - options = dict(fileobj=filename) |
1231 | | - else: |
1232 | | - raise ValueError("filename must be a path or a file-like object") |
1233 | | - |
1234 | | - with gzip.GzipFile(mode='w', **options) as gzipwriter: |
| 1220 | + with cbook.open_file_cm(filename, "wb") as fh, \ |
| 1221 | + gzip.GzipFile(mode='w', fileobj=fh) as gzipwriter: |
1235 | 1222 | return self.print_svg(gzipwriter) |
1236 | 1223 |
|
1237 | | - def _print_svg(self, filename, svgwriter, **kwargs): |
| 1224 | + def _print_svg(self, filename, fh, **kwargs): |
1238 | 1225 | image_dpi = kwargs.pop("dpi", 72) |
1239 | 1226 | self.figure.set_dpi(72.0) |
1240 | 1227 | width, height = self.figure.get_size_inches() |
1241 | | - w, h = width*72, height*72 |
| 1228 | + w, h = width * 72, height * 72 |
1242 | 1229 |
|
1243 | 1230 | _bbox_inches_restore = kwargs.pop("bbox_inches_restore", None) |
1244 | 1231 | renderer = MixedModeRenderer( |
1245 | | - self.figure, |
1246 | | - width, height, image_dpi, RendererSVG(w, h, svgwriter, filename, image_dpi), |
| 1232 | + self.figure, width, height, image_dpi, |
| 1233 | + RendererSVG(w, h, fh, filename, image_dpi), |
1247 | 1234 | bbox_inches_restore=_bbox_inches_restore) |
1248 | 1235 |
|
1249 | 1236 | self.figure.draw(renderer) |
|
0 commit comments