From 395fdae26642edac33590ff01127bd4e483718ff 2014-11-01 23:41:01
From: MinRK <benjaminrk@gmail.com>
Date: 2014-11-01 23:41:01
Subject: [PATCH] strip transient values to/from nb files

orig_nbformat isn’t part of the file format

---

diff --git a/IPython/nbformat/v4/nbformat.v4.schema.json b/IPython/nbformat/v4/nbformat.v4.schema.json
index e34b0a7..ace80c0 100644
--- a/IPython/nbformat/v4/nbformat.v4.schema.json
+++ b/IPython/nbformat/v4/nbformat.v4.schema.json
@@ -34,7 +34,7 @@
                     "type": "string"
                 },
                 "orig_nbformat": {
-                    "description": "Original notebook format (major number) before converting the notebook between versions.",
+                    "description": "Original notebook format (major number) before converting the notebook between versions. This should never be written to a file.",
                     "type": "integer",
                     "minimum": 1
                 }
diff --git a/IPython/nbformat/v4/nbjson.py b/IPython/nbformat/v4/nbjson.py
index 1c5fb05..fdc8a7a 100644
--- a/IPython/nbformat/v4/nbjson.py
+++ b/IPython/nbformat/v4/nbjson.py
@@ -10,7 +10,7 @@ from IPython.utils import py3compat
 
 from .nbbase import from_dict
 from .rwbase import (
-    NotebookReader, NotebookWriter, rejoin_lines, split_lines
+    NotebookReader, NotebookWriter, rejoin_lines, split_lines, strip_transient
 )
 
 
@@ -30,7 +30,9 @@ class JSONReader(NotebookReader):
         return nb
 
     def to_notebook(self, d, **kwargs):
-        return rejoin_lines(from_dict(d))
+        nb = rejoin_lines(from_dict(d))
+        nb = strip_transient(nb)
+        return nb
 
 
 class JSONWriter(NotebookWriter):
@@ -40,8 +42,11 @@ class JSONWriter(NotebookWriter):
         kwargs['indent'] = 1
         kwargs['sort_keys'] = True
         kwargs['separators'] = (',',': ')
+        # don't modify in-memory dict
+        nb = copy.deepcopy(nb)
         if kwargs.pop('split_lines', True):
-            nb = split_lines(copy.deepcopy(nb))
+            nb = split_lines(nb)
+        nb = strip_transient(nb)
         return py3compat.str_to_unicode(json.dumps(nb, **kwargs), 'utf-8')
 
 
diff --git a/IPython/nbformat/v4/rwbase.py b/IPython/nbformat/v4/rwbase.py
index ec885cd..beae9ea 100644
--- a/IPython/nbformat/v4/rwbase.py
+++ b/IPython/nbformat/v4/rwbase.py
@@ -75,6 +75,18 @@ def split_lines(nb):
     return nb
 
 
+def strip_transient(nb):
+    """Strip transient values that shouldn't be stored in files.
+
+    This should be called in *both* read and write.
+    """
+    nb.metadata.pop('orig_nbformat', None)
+    nb.metadata.pop('orig_nbformat_minor', None)
+    for cell in nb.cells:
+        cell.metadata.pop('trusted', None)
+    return nb
+
+
 class NotebookReader(object):
     """A class for reading notebooks."""