# HG changeset patch # User hodgestar # Date 1300439872 0 # Node ID 705727288d7e7c1e734622142e85dba3980538da # Parent 7c9ec79caedcd0f428d899912c8ade60cb8ebaf2 Merge r1143 from py3k: add support for python 3 to remaining genshi.template components: * minor changes to track encoding=None API change in core genshi modules. * genshi/template/directives: * slightly odd syntax changes to make the 2to3 .next() fixer pick up *stream.next() * minor test fix for change in behaviour of division (/) in Python 3. * genshi/template/loader: * add 'b' to file modes to ensure it's loaded as bytes in Python 3. * use not isinstance(s, unicode) instead of isinstance(s, str) since the former is correctly converted by 2to3. diff --git a/genshi/template/base.py b/genshi/template/base.py --- a/genshi/template/base.py +++ b/genshi/template/base.py @@ -15,9 +15,9 @@ from collections import deque import os -from StringIO import StringIO import sys +from genshi.compat import StringIO, BytesIO from genshi.core import Attrs, Stream, StreamEventKind, START, TEXT, _ensure from genshi.input import ParseError @@ -398,10 +398,11 @@ self._init_loader() self._prepared = False - if isinstance(source, basestring): - source = StringIO(source) - else: - source = source + if not isinstance(source, Stream) and not hasattr(source, 'read'): + if isinstance(source, unicode): + source = StringIO(source) + else: + source = BytesIO(source) try: self._stream = self._parse(source, encoding) except ParseError, e: diff --git a/genshi/template/directives.py b/genshi/template/directives.py --- a/genshi/template/directives.py +++ b/genshi/template/directives.py @@ -622,13 +622,13 @@ if not info: raise TemplateRuntimeError('"when" directives can only be used ' 'inside a "choose" directive', - self.filename, *stream.next()[2][1:]) + self.filename, *(stream.next())[2][1:]) if info[0]: return [] if not self.expr and not info[1]: raise TemplateRuntimeError('either "choose" or "when" directive ' 'must have a test expression', - self.filename, *stream.next()[2][1:]) + self.filename, *(stream.next())[2][1:]) if info[1]: value = info[2] if self.expr: @@ -661,7 +661,7 @@ if not info: raise TemplateRuntimeError('an "otherwise" directive can only be ' 'used inside a "choose" directive', - self.filename, *stream.next()[2][1:]) + self.filename, *(stream.next())[2][1:]) if info[0]: return [] info[0] = True diff --git a/genshi/template/loader.py b/genshi/template/loader.py --- a/genshi/template/loader.py +++ b/genshi/template/loader.py @@ -46,7 +46,7 @@ >>> import tempfile >>> fd, path = tempfile.mkstemp(suffix='.html', prefix='template') - >>> os.write(fd, '

$var

') + >>> os.write(fd, u'

$var

'.encode('utf-8')) 11 >>> os.close(fd) @@ -283,7 +283,7 @@ """ def _load_from_directory(filename): filepath = os.path.join(path, filename) - fileobj = open(filepath, 'U') + fileobj = open(filepath, 'rbU') mtime = os.path.getmtime(filepath) def _uptodate(): return mtime == os.path.getmtime(filepath) diff --git a/genshi/template/plugin.py b/genshi/template/plugin.py --- a/genshi/template/plugin.py +++ b/genshi/template/plugin.py @@ -44,7 +44,7 @@ options = {} self.options = options - self.default_encoding = options.get('genshi.default_encoding', 'utf-8') + self.default_encoding = options.get('genshi.default_encoding', None) auto_reload = options.get('genshi.auto_reload', '1') if isinstance(auto_reload, basestring): auto_reload = auto_reload.lower() in ('1', 'on', 'yes', 'true') diff --git a/genshi/template/tests/directives.py b/genshi/template/tests/directives.py --- a/genshi/template/tests/directives.py +++ b/genshi/template/tests/directives.py @@ -1137,8 +1137,8 @@ ${x} ${y} """) self.assertEqual("""
- 84 42 -
""", tmpl.generate(x=42).render(encoding=None)) + 84 %s + """ % (84 / 2), tmpl.generate(x=42).render(encoding=None)) def test_semicolon_escape(self): tmpl = MarkupTemplate("""
diff --git a/genshi/template/tests/loader.py b/genshi/template/tests/loader.py --- a/genshi/template/tests/loader.py +++ b/genshi/template/tests/loader.py @@ -347,7 +347,7 @@ assert 'tmpl2.html' not in loader._cache def test_load_with_default_encoding(self): - f = open(os.path.join(self.dirname, 'tmpl.html'), 'w') + f = open(os.path.join(self.dirname, 'tmpl.html'), 'wb') try: f.write(u'
\xf6
'.encode('iso-8859-1')) finally: @@ -356,7 +356,7 @@ loader.load('tmpl.html') def test_load_with_explicit_encoding(self): - f = open(os.path.join(self.dirname, 'tmpl.html'), 'w') + f = open(os.path.join(self.dirname, 'tmpl.html'), 'wb') try: f.write(u'
\xf6
'.encode('iso-8859-1')) finally: diff --git a/genshi/template/tests/markup.py b/genshi/template/tests/markup.py --- a/genshi/template/tests/markup.py +++ b/genshi/template/tests/markup.py @@ -15,11 +15,11 @@ import os import pickle import shutil -from StringIO import StringIO import sys import tempfile import unittest +from genshi.compat import BytesIO, StringIO from genshi.core import Markup from genshi.input import XML from genshi.template.base import BadDirectiveError, TemplateSyntaxError @@ -43,7 +43,7 @@ def test_pickle(self): stream = XML('$var') tmpl = MarkupTemplate(stream) - buf = StringIO() + buf = BytesIO() pickle.dump(tmpl, buf, 2) buf.seek(0) unpickled = pickle.load(buf) diff --git a/genshi/template/tests/plugin.py b/genshi/template/tests/plugin.py --- a/genshi/template/tests/plugin.py +++ b/genshi/template/tests/plugin.py @@ -30,7 +30,7 @@ def test_init_no_options(self): plugin = MarkupTemplateEnginePlugin() - self.assertEqual('utf-8', plugin.default_encoding) + self.assertEqual(None, plugin.default_encoding) self.assertEqual('html', plugin.default_format) self.assertEqual(None, plugin.default_doctype) @@ -165,7 +165,7 @@ def test_helper_functions(self): plugin = MarkupTemplateEnginePlugin() tmpl = plugin.load_template(PACKAGE + '.templates.functions') - output = plugin.render({'snippet': 'Foo'}, template=tmpl) + output = plugin.render({'snippet': u'Foo'}, template=tmpl) self.assertEqual("""
False bar @@ -178,7 +178,7 @@ def test_init_no_options(self): plugin = TextTemplateEnginePlugin() - self.assertEqual('utf-8', plugin.default_encoding) + self.assertEqual(None, plugin.default_encoding) self.assertEqual('text', plugin.default_format) self.assertEqual([], plugin.loader.search_path) diff --git a/genshi/template/text.py b/genshi/template/text.py --- a/genshi/template/text.py +++ b/genshi/template/text.py @@ -162,7 +162,7 @@ depth = 0 source = source.read() - if isinstance(source, str): + if not isinstance(source, unicode): source = source.decode(encoding or 'utf-8', 'replace') offset = 0 lineno = 1 @@ -279,7 +279,7 @@ depth = 0 source = source.read() - if isinstance(source, str): + if not isinstance(source, unicode): source = source.decode(encoding or 'utf-8', 'replace') offset = 0 lineno = 1