Mercurial > genshi > mirror
changeset 935:59463031d904 trunk
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.
author | hodgestar |
---|---|
date | Fri, 18 Mar 2011 09:17:52 +0000 |
parents | 31bbb6f9e87b |
children | a43c89d747d3 |
files | genshi/template/base.py genshi/template/directives.py genshi/template/loader.py genshi/template/plugin.py genshi/template/tests/directives.py genshi/template/tests/loader.py genshi/template/tests/markup.py genshi/template/tests/plugin.py genshi/template/text.py |
diffstat | 9 files changed, 23 insertions(+), 22 deletions(-) [+] |
line wrap: on
line diff
--- 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:
--- 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
--- 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, '<p>$var</p>') + >>> os.write(fd, u'<p>$var</p>'.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)
--- 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')
--- a/genshi/template/tests/directives.py +++ b/genshi/template/tests/directives.py @@ -1137,8 +1137,8 @@ <py:with vars="x = x * 2; y = x / 2;">${x} ${y}</py:with> </div>""") self.assertEqual("""<div> - 84 42 - </div>""", tmpl.generate(x=42).render(encoding=None)) + 84 %s + </div>""" % (84 / 2), tmpl.generate(x=42).render(encoding=None)) def test_semicolon_escape(self): tmpl = MarkupTemplate("""<div xmlns:py="http://genshi.edgewall.org/">
--- 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'<div>\xf6</div>'.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'<div>\xf6</div>'.encode('iso-8859-1')) finally:
--- 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('<root>$var</root>') tmpl = MarkupTemplate(stream) - buf = StringIO() + buf = BytesIO() pickle.dump(tmpl, buf, 2) buf.seek(0) unpickled = pickle.load(buf)
--- 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': '<b>Foo</b>'}, template=tmpl) + output = plugin.render({'snippet': u'<b>Foo</b>'}, template=tmpl) self.assertEqual("""<div> 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)
--- 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