From 3d8ee2579229eeefc210db6a874f15fe6a2f3ee3 2012-03-08 22:53:10
From: MinRK <benjaminrk@gmail.com>
Date: 2012-03-08 22:53:10
Subject: [PATCH] test and fix %notebook magic

---

diff --git a/IPython/core/magic.py b/IPython/core/magic.py
index 4996c20..6481961 100644
--- a/IPython/core/magic.py
+++ b/IPython/core/magic.py
@@ -20,6 +20,7 @@ import __future__
 import bdb
 import inspect
 import imp
+import io
 import os
 import sys
 import shutil
@@ -3682,7 +3683,7 @@ Defaulting color scheme to 'NoColor'"""
                 cells.append(current.new_code_cell(prompt_number=prompt_number, input=input))
             worksheet = current.new_worksheet(cells=cells)
             nb = current.new_notebook(name=name,worksheets=[worksheet])
-            with open(fname, 'w') as f:
+            with io.open(fname, 'w') as f:
                 current.write(nb, f, format);
         elif args.format is not None:
             old_fname, old_name, old_format = current.parse_filename(args.filename)
@@ -3696,13 +3697,9 @@ Defaulting color scheme to 'NoColor'"""
                 new_fname = old_name + u'.py'
             else:
                 raise ValueError('Invalid notebook format: %s' % new_format)
-            with open(old_fname, 'r') as f:
-                s = f.read()
-                try:
-                    nb = current.reads(s, old_format)
-                except:
-                    nb = current.reads(s, u'xml')
-            with open(new_fname, 'w') as f:
+            with io.open(old_fname, 'r') as f:
+                nb = current.read(f, old_format)
+            with io.open(new_fname, 'w') as f:
                 current.write(nb, f, new_format)
 
     def magic_config(self, s):
diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py
index e3a2e38..01081eb 100644
--- a/IPython/core/tests/test_magic.py
+++ b/IPython/core/tests/test_magic.py
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
 """Tests for various magic functions.
 
 Needs to be run by nose (to make ipython session available).
@@ -8,10 +9,13 @@ from __future__ import absolute_import
 # Imports
 #-----------------------------------------------------------------------------
 
+import io
 import os
 
 import nose.tools as nt
 
+from IPython.nbformat.v3.tests.nbexamples import nb0
+from IPython.nbformat import current
 from IPython.testing import decorators as dec
 from IPython.testing import tools as tt
 from IPython.utils import py3compat
@@ -21,6 +25,7 @@ from IPython.utils.tempdir import TemporaryDirectory
 # Test functions begin
 #-----------------------------------------------------------------------------
 
+
 def test_rehashx():
     # clear up everything
     _ip = get_ipython()
@@ -412,3 +417,34 @@ def test_extension():
     finally:
         _ip.ipython_dir = orig_ipython_dir
         
+def test_notebook_export_json():
+    with TemporaryDirectory() as td:
+        outfile = os.path.join(td, "nb.ipynb")
+        _ip.ex(py3compat.u_format("u = {u}'héllo'"))
+        _ip.magic("notebook -e %s" % outfile)
+
+def test_notebook_export_py():
+    with TemporaryDirectory() as td:
+        outfile = os.path.join(td, "nb.py")
+        _ip.ex(py3compat.u_format("u = {u}'héllo'"))
+        _ip.magic("notebook -e %s" % outfile)
+
+def test_notebook_reformat_py():
+    with TemporaryDirectory() as td:
+        infile = os.path.join(td, "nb.ipynb")
+        with io.open(infile, 'w') as f:
+            current.write(nb0, f, 'json')
+            
+        _ip.ex(py3compat.u_format("u = {u}'héllo'"))
+        _ip.magic("notebook -f py %s" % infile)
+
+def test_notebook_reformat_json():
+    with TemporaryDirectory() as td:
+        infile = os.path.join(td, "nb.py")
+        with io.open(infile, 'w') as f:
+            current.write(nb0, f, 'py')
+            
+        _ip.ex(py3compat.u_format("u = {u}'héllo'"))
+        _ip.magic("notebook -f ipynb %s" % infile)
+        _ip.magic("notebook -f json %s" % infile)
+