Save notebook as script using unicode file handle. #1353

Merged
merged 2 commits into from Feb 8, 2012

Projects

None yet

3 participants

@takluyver
Member

Using io.open(), which is the Python 3 open() in 2.6 and above.

Closes gh-1345

@ellisonbg
Member

A few other places we probably need to track this down:

  • There are other calls to open in the notebookmanager class.
  • In a variety of places in nbformat.v2 and nbformat.v3 (this PR should wait until #1331 has been merged - it defines the v2 nbformat).
@minrk
Member
minrk commented Jan 30, 2012

Actually, this should be fixed at a lower level. json.dumps, which is used by the other writes returns str. The writes method for python scripts should also return str by using py3compat.unicode_to_str so the two (and all future) writes methods match in their return type.

@takluyver
Member

Actually, I think this way is better than unicode_to_str. On Python 3, that will be a no-op, and it will be encoded by the text-mode file handle, which has a platform dependent default encoding (docs). But we hardcode the encoding comment to utf-8, so we should ensure that we're always encoding with utf-8.

The alternative would be to open a binary-mode file handle and encode it ourselves, but that lacks elegance. Conceptually, Python code is text, so it makes sense to write text.

There are probably other places that would benefit from using io.open - I wasn't aware of it when I wrote py3compat.

@minrk
Member
minrk commented Jan 30, 2012

That's a fair point, but we should ensure that both the writes methods do return the same type, which is the real bug here. One or the other should change.

@ellisonbg
Member

I am fine with this approach - let's just make sure the other calls to open in the notebook writing code get updated as well.

@minrk minrk merged commit 8e8b2a9 into ipython:master Feb 8, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment