diff --git a/IPython/nbformat/v3/rwbase.py b/IPython/nbformat/v3/rwbase.py index 7566b33..fc9d4d4 100644 --- a/IPython/nbformat/v3/rwbase.py +++ b/IPython/nbformat/v3/rwbase.py @@ -19,7 +19,9 @@ Authors: from base64 import encodestring, decodestring import pprint -from IPython.utils.py3compat import str_to_bytes +from IPython.utils import py3compat + +str_to_bytes = py3compat.str_to_bytes #----------------------------------------------------------------------------- # Code @@ -147,7 +149,10 @@ class NotebookReader(object): def read(self, fp, **kwargs): """Read a notebook from a file like object""" - return self.read(fp.read(), **kwargs) + nbs = fp.read() + if not py3compat.PY3 and not isinstance(nbs, unicode): + nbs = py3compat.str_to_unicode(nbs) + return self.reads(nbs, **kwargs) class NotebookWriter(object): @@ -159,7 +164,11 @@ class NotebookWriter(object): def write(self, nb, fp, **kwargs): """Write a notebook to a file like object""" - return fp.write(self.writes(nb,**kwargs)) + nbs = self.writes(nb,**kwargs) + if not py3compat.PY3 and not isinstance(nbs, unicode): + # this branch is likely only taken for JSON on Python 2 + nbs = py3compat.str_to_unicode(nbs) + return fp.write(nbs) diff --git a/IPython/nbformat/v3/tests/nbexamples.py b/IPython/nbformat/v3/tests/nbexamples.py index e2a9350..625c573 100644 --- a/IPython/nbformat/v3/tests/nbexamples.py +++ b/IPython/nbformat/v3/tests/nbexamples.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + import os from base64 import encodestring @@ -49,7 +51,7 @@ ws.cells.append(new_code_cell( )) ws.cells.append(new_code_cell( - input='print a', + input=u'print "ünîcødé"', prompt_number=3, collapsed=False, outputs=[new_output( @@ -91,7 +93,7 @@ nb0 = new_notebook( metadata=md ) -nb0_py = """# -*- coding: utf-8 -*- +nb0_py = u"""# -*- coding: utf-8 -*- # %i # @@ -120,7 +122,7 @@ a = numpy.random.rand(100) # -print a +print "ünîcødé" """ % nbformat