From d8965dd84d039f4cff5578ade372f94806cb7578 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20W=C3=BCrtz?= Date: Thu, 8 Jan 2015 17:21:31 +0100 Subject: [PATCH 1/2] backend_pgf: Partial support of file-like streams without filenames (fixes #3982) --- lib/matplotlib/backends/backend_pgf.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/backends/backend_pgf.py b/lib/matplotlib/backends/backend_pgf.py index 915ad597bd6b..c246bf978c1f 100644 --- a/lib/matplotlib/backends/backend_pgf.py +++ b/lib/matplotlib/backends/backend_pgf.py @@ -431,7 +431,10 @@ def __init__(self, figure, fh, dummy=False): self.__dict__[m] = nop else: # if fh does not belong to a filename, deactivate draw_image - if not os.path.exists(fh.name): + if not hasattr(fh, 'name') or not os.path.exists(fh.name): + warnings.warn("streamed pgf-code does not support raster " + "graphics, consider using the pgf-to-pdf option", + UserWarning) self.__dict__["draw_image"] = lambda *args, **kwargs: None def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None): @@ -835,11 +838,8 @@ def print_pgf(self, fname_or_fh, *args, **kwargs): with codecs.open(fname_or_fh, "w", encoding="utf-8") as fh: self._print_pgf_to_fh(fh, *args, **kwargs) elif is_writable_file_like(fname_or_fh): - if not os.path.exists(fname_or_fh.name): - warnings.warn("streamed pgf-code does not support raster " - "graphics, consider using the pgf-to-pdf option", - UserWarning) - self._print_pgf_to_fh(fname_or_fh, *args, **kwargs) + fh = codecs.getwriter("utf-8")(fname_or_fh) + self._print_pgf_to_fh(fh, *args, **kwargs) else: raise ValueError("filename must be a path") From 9fcd0264177e273153456699980ee08726029165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20W=C3=BCrtz?= Date: Fri, 9 Jan 2015 03:52:39 +0100 Subject: [PATCH 2/2] Fixed writing pgf code to streams. --- lib/matplotlib/backends/backend_webagg_core.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/matplotlib/backends/backend_webagg_core.py b/lib/matplotlib/backends/backend_webagg_core.py index 7a4bec1a325a..8f971600d040 100644 --- a/lib/matplotlib/backends/backend_webagg_core.py +++ b/lib/matplotlib/backends/backend_webagg_core.py @@ -403,8 +403,7 @@ def get_javascript(cls, stream=None): for filetype, ext in sorted(FigureCanvasWebAggCore. get_supported_filetypes_grouped(). items()): - if not ext[0] == 'pgf': # pgf does not support BytesIO - extensions.append(ext[0]) + extensions.append(ext[0]) output.write("mpl.extensions = {0};\n\n".format( json.dumps(extensions)))