Skip to content

Commit

Permalink
Reproduce and fix #11
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Wouts committed Jul 5, 2018
1 parent 4e43c80 commit 46d6a82
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 3 additions & 2 deletions nbrmd/nbrmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# -----------------------------------------------------------------------------

import os
import io
import re
from enum import Enum
from nbformat.v4.rwbase import NotebookReader, NotebookWriter
Expand Down Expand Up @@ -350,7 +351,7 @@ def readf(nb_file):
:return: the notebook
"""
file, ext = os.path.splitext(nb_file)
with open(nb_file) as fp:
with io.open(nb_file, encoding='utf-8') as fp:
if ext == '.Rmd':
return read(fp)
elif ext == '.md':
Expand All @@ -372,7 +373,7 @@ def writef(nb, nb_file):
"""

file, ext = os.path.splitext(nb_file)
with open(nb_file, 'w') as fp:
with io.open(nb_file, 'w', encoding='utf-8') as fp:
if ext == '.Rmd':
write(nb, fp)
elif ext == '.md':
Expand Down
19 changes: 19 additions & 0 deletions tests/test_unicode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# coding: utf-8
import nbrmd
import pytest
from .utils import list_all_notebooks


@pytest.mark.parametrize('nb_file', list_all_notebooks('.ipynb') +
list_all_notebooks('.Rmd'))
def test_notebook_contents_is_unicode(nb_file):
nb = nbrmd.readf(nb_file)

for cell in nb.cells:
assert cell.source == '' or isinstance(cell.source, unicode)


def test_write_non_ascii(tmpdir):
nb = nbrmd.reads(u'Non-ascii contênt')
nbrmd.writef(nb, str(tmpdir.join('notebook.Rmd')))
nbrmd.writef(nb, str(tmpdir.join('notebook.ipynb')))

0 comments on commit 46d6a82

Please sign in to comment.