Mercurial > genshi > genshi-test
diff genshi/core.py @ 915:9fafb35032a1 experimental-py3k
add support for python 3 to core genshi components (genshi.core, genshi.input and genshi.output):
* default input and output encodings changed from UTF-8 to None (i.e. unicode strings)
* Namespace and QName objects do not call stringrepr in __repr__ in Python 3 since repr() returns a unicode string there.
* track changes to expat parser in Python 3 (mostly it accepts bytes instead of strings)
author | hodgestar |
---|---|
date | Sun, 24 Oct 2010 22:08:11 +0000 |
parents | 2772fa7c10a3 |
children | f4bf11c716cc |
line wrap: on
line diff
--- a/genshi/core.py +++ b/genshi/core.py @@ -17,6 +17,7 @@ reduce # builtin in Python < 3 except NameError: from functools import reduce +import sys from itertools import chain import operator @@ -92,7 +93,7 @@ Assume the following stream produced by the `HTML` function: >>> from genshi.input import HTML - >>> html = HTML('''<p onclick="alert('Whoa')">Hello, world!</p>''') + >>> html = HTML('''<p onclick="alert('Whoa')">Hello, world!</p>''', encoding='utf-8') >>> print(html) <p onclick="alert('Whoa')">Hello, world!</p> @@ -153,7 +154,7 @@ """ return reduce(operator.or_, (self,) + filters) - def render(self, method=None, encoding='utf-8', out=None, **kwargs): + def render(self, method=None, encoding=None, out=None, **kwargs): """Return a string representation of the stream. Any additional keyword arguments are passed to the serializer, and thus @@ -187,7 +188,7 @@ XPath expression. >>> from genshi import HTML - >>> stream = HTML('<doc><elem>foo</elem><elem>bar</elem></doc>') + >>> stream = HTML('<doc><elem>foo</elem><elem>bar</elem></doc>', encoding='utf-8') >>> print(stream.select('elem')) <elem>foo</elem><elem>bar</elem> >>> print(stream.select('elem/text()')) @@ -667,8 +668,13 @@ def __hash__(self): return hash(self.uri) - def __repr__(self): - return '%s(%s)' % (type(self).__name__, stringrepr(self.uri)) + if sys.version_info[0] == 2: + # Only use stringrepr in python 2 + def __repr__(self): + return '%s(%s)' % (type(self).__name__, stringrepr(self.uri)) + else: + def __repr__(self): + return '%s(%r)' % (type(self).__name__, self.uri) def __str__(self): return self.uri.encode('utf-8') @@ -728,5 +734,10 @@ def __getnewargs__(self): return (self.lstrip('{'),) - def __repr__(self): - return '%s(%s)' % (type(self).__name__, stringrepr(self.lstrip('{'))) + if sys.version_info[0] == 2: + # Only use stringrepr in python 2 + def __repr__(self): + return '%s(%s)' % (type(self).__name__, stringrepr(self.lstrip('{'))) + else: + def __repr__(self): + return '%s(%r)' % (type(self).__name__, self.lstrip('{'))