Skip to content

Commit

Permalink
Merge pull request #37 from sethrj/master
Browse files Browse the repository at this point in the history
Fix issue 35: v4 conversion failures with 'orig_nbformat_minor'
  • Loading branch information
minrk committed Jun 13, 2016
2 parents 2c9cadf + 0b4b23c commit 116bffd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
15 changes: 14 additions & 1 deletion nbformat/tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from ..converter import convert
from ..reader import read, get_version
from ..validator import isvalid, validate
from .. import current_nbformat


Expand Down Expand Up @@ -38,8 +39,20 @@ def test_upgrade_2_3(self):
self.assertEqual(major, 3)


def test_upgrade_downgrade_4_3_4(self):
"""Test that a v4 notebook downgraded to v3 and then upgraded to v4
passes validation tests"""
with self.fopen(u'test4.ipynb', u'r') as f:
nb = read(f)
validate(nb)
nb = convert(nb, 3)
validate(nb)
nb = convert(nb, 4)
self.assertEqual(isvalid(nb), True)


def test_open_current(self):
"""Can an old notebook be opened and converted to the current version
"""Can an old notebook be opened and converted to the current version
while remembering the original version of the notebook?"""

# Open a version 2 notebook and attempt to upgrade it to the current version
Expand Down
8 changes: 5 additions & 3 deletions nbformat/v4/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def upgrade(nb, from_version=3, from_minor=0):

# Mark the original nbformat so consumers know it has been converted
orig_nbformat = nb.pop('orig_nbformat', None)
orig_nbformat_minor = nb.pop('orig_nbformat_minor', None)
nb.metadata.orig_nbformat = orig_nbformat or 3
nb.metadata.orig_nbformat_minor = orig_nbformat_minor or 0

# Mark the new format
nb.nbformat = nbformat
Expand Down Expand Up @@ -243,11 +245,11 @@ def downgrade(nb):
cells = [ downgrade_cell(cell) for cell in nb.pop('cells') ]
nb.worksheets = [v3.new_worksheet(cells=cells)]
nb.metadata.setdefault('name', '')

# Validate the converted notebook before returning it
_warn_if_invalid(nb, v3.nbformat)

nb.orig_nbformat = nb.metadata.pop('orig_nbformat', nbformat)
nb.orig_nbformat_minor = nb.metadata.pop('orig_nbformat_minor', nbformat_minor)

return nb

0 comments on commit 116bffd

Please sign in to comment.