From 57b3812dd5ddef3007f66f42f11802e28bc0e527 2014-03-28 23:50:55 From: MinRK Date: 2014-03-28 23:50:55 Subject: [PATCH] print_figure returns unicode for svg --- diff --git a/IPython/core/pylabtools.py b/IPython/core/pylabtools.py index 9565e89..265958e 100644 --- a/IPython/core/pylabtools.py +++ b/IPython/core/pylabtools.py @@ -96,7 +96,10 @@ def figsize(sizex, sizey): def print_figure(fig, fmt='png', bbox_inches='tight', **kwargs): - """Print a figure to an image, and return the resulting bytes + """Print a figure to an image, and return the resulting file data + + Returned data will be bytes unless ``fmt='svg'``, + in which case it will be unicode. Any keyword args are passed to fig.canvas.print_figure, such as ``quality`` or ``bbox_inches``. @@ -125,7 +128,10 @@ def print_figure(fig, fmt='png', bbox_inches='tight', **kwargs): bytes_io = BytesIO() fig.canvas.print_figure(bytes_io, **kw) - return bytes_io.getvalue() + data = bytes_io.getvalue() + if fmt == 'svg': + data = data.decode('utf-8') + return data def retina_figure(fig, **kwargs): """format a figure as a pixel-doubled (retina) PNG""" diff --git a/IPython/core/tests/test_pylabtools.py b/IPython/core/tests/test_pylabtools.py index aed981f..317e41f 100644 --- a/IPython/core/tests/test_pylabtools.py +++ b/IPython/core/tests/test_pylabtools.py @@ -58,7 +58,7 @@ def test_figure_to_svg(): ax.plot([1,2,3]) plt.draw() svg = pt.print_figure(fig, 'svg')[:100].lower() - nt.assert_in(b'doctype svg', svg) + nt.assert_in(u'doctype svg', svg) def _check_pil_jpeg_bytes(): """Skip if PIL can't write JPEGs to BytesIO objects"""