From 0895a9b606f82f0af10730a63267d53e38b60779 2012-04-02 18:42:00 From: MinRK Date: 2012-04-02 18:42:00 Subject: [PATCH] preserve trailing newlines in ipynb Principally important for distinguishing a sequence of `print n,` messages from a sequence of `print n` messages. --- diff --git a/IPython/nbformat/v3/rwbase.py b/IPython/nbformat/v3/rwbase.py index fc9d4d4..6386108 100644 --- a/IPython/nbformat/v3/rwbase.py +++ b/IPython/nbformat/v3/rwbase.py @@ -86,17 +86,17 @@ def split_lines(nb): for cell in ws.cells: if cell.cell_type == 'code': if 'input' in cell and isinstance(cell.input, basestring): - cell.input = cell.input.splitlines() + cell.input = (cell.input + '\n').splitlines() for output in cell.outputs: for key in _multiline_outputs: item = output.get(key, None) if isinstance(item, basestring): - output[key] = item.splitlines() + output[key] = (item + '\n').splitlines() else: # text, heading cell for key in ['source', 'rendered']: item = cell.get(key, None) if isinstance(item, basestring): - cell[key] = item.splitlines() + cell[key] = (item + '\n').splitlines() return nb # b64 encode/decode are never actually used, because all bytes objects in diff --git a/IPython/nbformat/v3/tests/nbexamples.py b/IPython/nbformat/v3/tests/nbexamples.py index 625c573..16a54c4 100644 --- a/IPython/nbformat/v3/tests/nbexamples.py +++ b/IPython/nbformat/v3/tests/nbexamples.py @@ -49,6 +49,14 @@ ws.cells.append(new_code_cell( prompt_number=2, collapsed=True )) +ws.cells.append(new_code_cell( + input='a = 10\nb = 5\n', + prompt_number=3, +)) +ws.cells.append(new_code_cell( + input='a = 10\nb = 5', + prompt_number=4, +)) ws.cells.append(new_code_cell( input=u'print "ünîcødé"', @@ -122,6 +130,16 @@ a = numpy.random.rand(100) # +a = 10 +b = 5 + +# + +a = 10 +b = 5 + +# + print "ünîcødé" """ % nbformat diff --git a/IPython/nbformat/v3/tests/test_nbpy.py b/IPython/nbformat/v3/tests/test_nbpy.py index ca8f56b..1b70d1d 100644 --- a/IPython/nbformat/v3/tests/test_nbpy.py +++ b/IPython/nbformat/v3/tests/test_nbpy.py @@ -28,6 +28,11 @@ class TestPy(formattest.NBFormatTestCase): for a,b in zip(da, db): self.assertSubset(a,b) else: + if isinstance(da, basestring) and isinstance(db, basestring): + # pyfile is not sensitive to preserving leading/trailing + # newlines in blocks through roundtrip + da = da.strip('\n') + db = db.strip('\n') self.assertEquals(da, db) return True