Commit 3a960197 by Chris Jerdonek

Fixed issue #65: "Loader should accept decode_errors like Renderer"

parent 10e867c9
......@@ -8,12 +8,13 @@ This module provides a Loader class.
import os
import sys
DEFAULT_DECODE_ERRORS = 'strict'
DEFAULT_EXTENSION = 'mustache'
class Loader(object):
def __init__(self, search_dirs=None, encoding=None, extension=None):
def __init__(self, search_dirs=None, extension=None, encoding=None, decode_errors=None):
"""
Construct a template loader.
......@@ -32,8 +33,14 @@ class Loader(object):
argument to the built-in function unicode(). Defaults to the
encoding name returned by sys.getdefaultencoding().
decode_errors: the string to pass as the "errors" argument to the
built-in function unicode() when converting file contents to
unicode. Defaults to "strict".
"""
if decode_errors is None:
decode_errors = DEFAULT_DECODE_ERRORS
if encoding is None:
encoding = sys.getdefaultencoding()
......@@ -46,6 +53,7 @@ class Loader(object):
if isinstance(search_dirs, basestring):
search_dirs = [search_dirs]
self.decode_errors = decode_errors
self.search_dirs = search_dirs
self.template_encoding = encoding
self.template_extension = extension
......@@ -88,6 +96,6 @@ class Loader(object):
finally:
f.close()
template = unicode(template, self.template_encoding)
template = unicode(template, self.template_encoding, self.decode_errors)
return template
ascii: abc
\ No newline at end of file
non-ascii: é
\ No newline at end of file
# encoding: utf-8
import os
import sys
import unittest
from pystache.loader import Loader
DATA_DIR = 'tests/data'
class LoaderTestCase(unittest.TestCase):
search_dirs = 'examples'
def _loader(self):
return Loader(search_dirs=DATA_DIR)
def test_init__search_dirs(self):
# Test the default value.
loader = Loader()
......@@ -17,18 +23,15 @@ class LoaderTestCase(unittest.TestCase):
loader = Loader(search_dirs=['foo'])
self.assertEquals(loader.search_dirs, ['foo'])
def test_init__extension(self):
def test_init__decode_errors(self):
# Test the default value.
loader = Loader()
self.assertEquals(loader.template_extension, 'mustache')
self.assertEquals(loader.decode_errors, 'strict')
loader = Loader(extension='txt')
self.assertEquals(loader.template_extension, 'txt')
loader = Loader(extension=False)
self.assertTrue(loader.template_extension is False)
loader = Loader(decode_errors='replace')
self.assertEquals(loader.decode_errors, 'replace')
def test_init__loader(self):
def test_init__encoding(self):
# Test the default value.
loader = Loader()
self.assertEquals(loader.template_encoding, sys.getdefaultencoding())
......@@ -36,6 +39,17 @@ class LoaderTestCase(unittest.TestCase):
loader = Loader(encoding='foo')
self.assertEquals(loader.template_encoding, 'foo')
def test_init__extension(self):
# Test the default value.
loader = Loader()
self.assertEquals(loader.template_extension, 'mustache')
loader = Loader(extension='txt')
self.assertEquals(loader.template_extension, 'txt')
loader = Loader(extension=False)
self.assertTrue(loader.template_extension is False)
def test_make_file_name(self):
loader = Loader()
......@@ -72,12 +86,42 @@ class LoaderTestCase(unittest.TestCase):
loader.template_extension = False
self.assertEquals(loader.get('extensionless'), "No file extension: {{foo}}")
def test_get__load_template__unicode_return_value(self):
def test_get(self):
"""
Check that load_template() returns unicode strings.
Test get().
"""
loader = Loader(search_dirs=self.search_dirs)
template = loader.get('simple')
loader = self._loader()
self.assertEquals(loader.get('ascii'), 'ascii: abc')
def test_get__unicode_return_value(self):
"""
Test that get() returns unicode strings.
"""
loader = self._loader()
actual = loader.get('ascii')
self.assertEqual(type(actual), unicode)
def test_get__encoding(self):
"""
Test get(): encoding attribute respected.
"""
loader = self._loader()
self.assertRaises(UnicodeDecodeError, loader.get, 'nonascii')
loader.template_encoding = 'utf-8'
self.assertEquals(loader.get('nonascii'), u'non-ascii: é')
def test_get__decode_errors(self):
"""
Test get(): decode_errors attribute.
"""
loader = self._loader()
self.assertRaises(UnicodeDecodeError, loader.get, 'nonascii')
loader.decode_errors = 'replace'
self.assertEquals(loader.get('nonascii'), u'non-ascii: \ufffd\ufffd')
self.assertEqual(type(template), unicode)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment