Skip to content

Commit

Permalink
Merge branch 'master' into maint
Browse files Browse the repository at this point in the history
  • Loading branch information
R. Tyler Ballance committed Oct 25, 2009
2 parents 4c64fb2 + 7b1c2ad commit 62481b7
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 32 deletions.
6 changes: 4 additions & 2 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@

2.4.0 (October 15th, 2009)
2.4.0 (October 24th, 2009)
- Fix a major performance regression in Template.__init__()
- More graceful handling of unicode when calling .respond() to render a template
- Minor code updates
- Update the default filter (thanks mikeb!)

2.3.0 (October 15th, 2009) (loosely equivalent to 2.4.0)
2.3.0 (October 24th, 2009) (loosely equivalent to 2.4.0)
- Fix a major performance regression in Template.__init__()
- More graceful handling of unicode when calling .respond() to render a template
- Minor code updates
- Update the default filter (thanks mikeb!)

2.2.2 (September 10th, 2009)
- Prevent _namemapper.c from segfaulting when PyImport_ImportModule fails for some reason (Bogdano Arendartchuk <debogdano@gmail.com>)
Expand Down
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include MANIFEST.in *.py *.cfg TODO CHANGES LICENSE README examples docs bin
include MANIFEST.in *.py *.cfg TODO CHANGES LICENSE README.markdown examples docs bin
recursive-include cheetah *.py *.tmpl *.txt
recursive-include bin *
recursive-include docs *
Expand Down
35 changes: 7 additions & 28 deletions cheetah/Filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,40 +29,19 @@ def filter(self, val, encoding=None, str=str, **kw):
if val is None:
return u''
if isinstance(val, unicode):
if encoding:
return val.encode(encoding)
else:
return val
# ignore the encoding and return the unicode object
return val
else:
try:
return str(val)
except UnicodeEncodeError:
return unicode(val)
return u''
except UnicodeDecodeError:
# we could put more fallbacks here, but we'll just pass the str
# on and let DummyTransaction worry about it
return str(val)

RawOrEncodedUnicode = Filter

class EncodeUnicode(Filter):
def filter(self, val,
encoding='utf8',
str=str,
**kw):
"""Encode Unicode strings, by default in UTF-8.
>>> import Cheetah.Template
>>> t = Cheetah.Template.Template('''
... $myvar
... ${myvar, encoding='utf16'}
... ''', searchList=[{'myvar': u'Asni\xe8res'}],
... filter='EncodeUnicode')
>>> print t
"""
if isinstance(val, unicode):
return val.encode(encoding)
if val is None:
return ''
return str(val)

EncodeUnicode = Filter

class Markdown(EncodeUnicode):
'''
Expand Down
25 changes: 24 additions & 1 deletion cheetah/Template.py
Original file line number Diff line number Diff line change
Expand Up @@ -994,22 +994,45 @@ def _addCheetahPlumbingCodeToClass(klass, concreteTemplateClass):
mainMethName = getattr(concreteTemplateClass,mainMethNameAttr, None)
if mainMethName:
def __str__(self):
rc = getattr(self, mainMethName)()
if isinstance(rc, unicode):
return rc.encode('utf-8')
return rc
def __unicode__(self):
return getattr(self, mainMethName)()
elif (hasattr(concreteTemplateClass, 'respond')
and concreteTemplateClass.respond!=Servlet.respond):
def __str__(self):
rc = self.respond()
if isinstance(rc, unicode):
return rc.encode('utf-8')
return rc
def __unicode__(self):
return self.respond()
else:
def __str__(self):
rc = None
if hasattr(self, mainMethNameAttr):
rc = getattr(self,mainMethNameAttr)()
elif hasattr(self, 'respond'):
rc = self.respond()
else:
rc = super(self.__class__, self).__str__()
if isinstance(rc, unicode):
return rc.encode('utf-8')
return rc
def __unicode__(self):
if hasattr(self, mainMethNameAttr):
return getattr(self,mainMethNameAttr)()
elif hasattr(self, 'respond'):
return self.respond()
else:
return super(self.__class__, self).__str__()
return super(self.__class__, self).__unicode__()

__str__ = new.instancemethod(__str__, None, concreteTemplateClass)
__unicode__ = new.instancemethod(__unicode__, None, concreteTemplateClass)
setattr(concreteTemplateClass, '__str__', __str__)
setattr(concreteTemplateClass, '__unicode__', __unicode__)

_addCheetahPlumbingCodeToClass = classmethod(_addCheetahPlumbingCodeToClass)

Expand Down
16 changes: 16 additions & 0 deletions cheetah/Tests/Unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@ def testDynamicCompile(self):
a = unicode(template).encode("utf-8")
self.assertEquals("Bébé", a)

class EncodeUnicodeCompatTest(unittest.TestCase):
"""
Taken initially from Red Hat's bugzilla #529332
https://bugzilla.redhat.com/show_bug.cgi?id=529332
"""
def runTest(self):
t = Template("""Foo ${var}""", filter='EncodeUnicode')
t.var = u"Text with some non-ascii characters: åäö"

rc = t.respond()
assert isinstance(rc, unicode), ('Template.respond() should return unicode', rc)

rc = str(t)
assert isinstance(rc, str), ('Template.__str__() should return a UTF-8 encoded string', rc)


class Unicode_in_SearchList_Test(CommandLineTest):
def test_BasicASCII(self):
source = '''This is $adjective'''
Expand Down

0 comments on commit 62481b7

Please sign in to comment.