##// END OF EJS Templates
Backport PR #4163: Fix for incorrect default encoding on Windows....
Backport PR #4163: Fix for incorrect default encoding on Windows. Whilst trying out rendering notebooks in a flask app under Apache on Windows I got the below error when simply trying to import `SlidesExporter` ```python mod_wsgi (pid=6260): Exception occurred processing WSGI script 'flask_test.wsgi'. Traceback (most recent call last): File "flask_test.py", line 81, in render_notebook from IPython.nbconvert.exporters import SlidesExporter File "c:\\dev\\code\\ipython\\IPython\\__init__.py", line 47, in <module> from .terminal.embed import embed File "c:\\dev\\code\\ipython\\IPython\\terminal\\embed.py", line 32, in <module> from IPython.terminal.interactiveshell import TerminalInteractiveShell File "c:\\dev\\code\\ipython\\IPython\\terminal\\interactiveshell.py", line 25, in <module> from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC File "c:\\dev\\code\\ipython\\IPython\\core\\interactiveshell.py", line 59, in <module> from IPython.core.prompts import PromptManager File "c:\\dev\\code\\ipython\\IPython\\core\\prompts.py", line 138, in <module> HOME = py3compat.str_to_unicode(os.environ.get("HOME","//////:::::ZZZZZ,,,~~~")) File "c:\\dev\\code\\ipython\\IPython\\utils\\py3compat.py", line 18, in decode return s.decode(encoding, "replace") LookupError: unknown encoding: cp0 ``` A little bit of [googling](http://bugs.python.org/issue6501) suggests that Windows returns 'cp0' to indicate there is no code page. This fix simply looks for this invalid value and replaces it with something valid. With this change it works for me.

File last commit:

r7874:4a6836ce
r12463:516353d0
Show More
formattest.py
63 lines | 1.5 KiB | text/x-python | PythonLexer
MinRK
add NBFormatTestCase base class, to consolidate nbformat testing
r6209 # -*- coding: utf8 -*-
import io
import os
import shutil
import tempfile
pjoin = os.path.join
from ..nbbase import (
NotebookNode,
new_code_cell, new_text_cell, new_worksheet, new_notebook
)
from ..nbpy import reads, writes, read, write
from .nbexamples import nb0, nb0_py
MinRK
specify utf8 when calling io.open
r6212 def open_utf8(fname, mode):
return io.open(fname, mode=mode, encoding='utf-8')
MinRK
NBFormatTest is now a mixin, rather than a base class
r6476 class NBFormatTest:
"""Mixin for writing notebook format tests"""
MinRK
add NBFormatTestCase base class, to consolidate nbformat testing
r6209
# override with appropriate values in subclasses
nb0_ref = None
ext = None
mod = None
def setUp(self):
self.wd = tempfile.mkdtemp()
def tearDown(self):
shutil.rmtree(self.wd)
def assertNBEquals(self, nba, nbb):
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(nba, nbb)
MinRK
add NBFormatTestCase base class, to consolidate nbformat testing
r6209
def test_writes(self):
s = self.mod.writes(nb0)
if self.nb0_ref:
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(s, self.nb0_ref)
MinRK
add NBFormatTestCase base class, to consolidate nbformat testing
r6209
def test_reads(self):
s = self.mod.writes(nb0)
nb = self.mod.reads(s)
def test_roundtrip(self):
s = self.mod.writes(nb0)
self.assertNBEquals(self.mod.reads(s),nb0)
def test_write_file(self):
MinRK
specify utf8 when calling io.open
r6212 with open_utf8(pjoin(self.wd, "nb0.%s" % self.ext), 'w') as f:
MinRK
add NBFormatTestCase base class, to consolidate nbformat testing
r6209 self.mod.write(nb0, f)
def test_read_file(self):
MinRK
specify utf8 when calling io.open
r6212 with open_utf8(pjoin(self.wd, "nb0.%s" % self.ext), 'w') as f:
MinRK
add NBFormatTestCase base class, to consolidate nbformat testing
r6209 self.mod.write(nb0, f)
MinRK
specify utf8 when calling io.open
r6212 with open_utf8(pjoin(self.wd, "nb0.%s" % self.ext), 'r') as f:
MinRK
add NBFormatTestCase base class, to consolidate nbformat testing
r6209 nb = self.mod.read(f)
MinRK
NBFormatTest is now a mixin, rather than a base class
r6476