Skip to content
This repository
Browse code

Merge branch 'master' into maint

  • Loading branch information...
commit 62481b7d1d7ad698a05ffc35488b29f6b32df8e4 2 parents 4c64fb2 + 7b1c2ad
R. Tyler Croy authored
6  CHANGES
... ...
@@ -1,13 +1,15 @@
1 1
 
2  
-2.4.0 (October 15th, 2009)
  2
+2.4.0 (October 24th, 2009)
3 3
   - Fix a major performance regression in Template.__init__()
4 4
   - More graceful handling of unicode when calling .respond() to render a template
5 5
   - Minor code updates
  6
+  - Update the default filter (thanks mikeb!)
6 7
 
7  
-2.3.0 (October 15th, 2009) (loosely equivalent to 2.4.0)
  8
+2.3.0 (October 24th, 2009) (loosely equivalent to 2.4.0)
8 9
   - Fix a major performance regression in Template.__init__()
9 10
   - More graceful handling of unicode when calling .respond() to render a template
10 11
   - Minor code updates
  12
+  - Update the default filter (thanks mikeb!)
11 13
 
12 14
 2.2.2 (September 10th, 2009)
13 15
   - Prevent _namemapper.c from segfaulting when PyImport_ImportModule fails for some reason (Bogdano Arendartchuk <debogdano@gmail.com>)
2  MANIFEST.in
... ...
@@ -1,4 +1,4 @@
1  
-include MANIFEST.in *.py *.cfg TODO CHANGES LICENSE README examples docs bin
  1
+include MANIFEST.in *.py *.cfg TODO CHANGES LICENSE README.markdown examples docs bin
2 2
 recursive-include cheetah *.py *.tmpl *.txt
3 3
 recursive-include bin *
4 4
 recursive-include docs * 
35  cheetah/Filters.py
@@ -29,40 +29,19 @@ def filter(self, val, encoding=None, str=str, **kw):
29 29
         if val is None:
30 30
             return u''
31 31
         if isinstance(val, unicode):
32  
-            if encoding:
33  
-                return val.encode(encoding)
34  
-            else:
35  
-                return val
  32
+            # ignore the encoding and return the unicode object
  33
+            return val
36 34
         else:
37 35
             try:
38  
-                return str(val)
39  
-            except UnicodeEncodeError:
40 36
                 return unicode(val)
41  
-        return u''
  37
+            except UnicodeDecodeError:
  38
+                # we could put more fallbacks here, but we'll just pass the str
  39
+                # on and let DummyTransaction worry about it
  40
+                return str(val)
42 41
 
43 42
 RawOrEncodedUnicode = Filter
44 43
 
45  
-class EncodeUnicode(Filter):
46  
-    def filter(self, val,
47  
-               encoding='utf8',
48  
-               str=str,
49  
-               **kw):
50  
-        """Encode Unicode strings, by default in UTF-8.
51  
-
52  
-        >>> import Cheetah.Template
53  
-        >>> t = Cheetah.Template.Template('''
54  
-        ... $myvar
55  
-        ... ${myvar, encoding='utf16'}
56  
-        ... ''', searchList=[{'myvar': u'Asni\xe8res'}],
57  
-        ... filter='EncodeUnicode')
58  
-        >>> print t
59  
-        """
60  
-        if isinstance(val, unicode):
61  
-            return val.encode(encoding)
62  
-        if val is None:
63  
-            return ''
64  
-        return str(val)
65  
-
  44
+EncodeUnicode = Filter
66 45
 
67 46
 class Markdown(EncodeUnicode):
68 47
     '''
25  cheetah/Template.py
@@ -994,22 +994,45 @@ def _addCheetahPlumbingCodeToClass(klass, concreteTemplateClass):
994 994
             mainMethName = getattr(concreteTemplateClass,mainMethNameAttr, None)
995 995
             if mainMethName:
996 996
                 def __str__(self): 
  997
+                    rc = getattr(self, mainMethName)()
  998
+                    if isinstance(rc, unicode):
  999
+                        return rc.encode('utf-8')
  1000
+                    return rc
  1001
+                def __unicode__(self):
997 1002
                     return getattr(self, mainMethName)()
998 1003
             elif (hasattr(concreteTemplateClass, 'respond')
999 1004
                   and concreteTemplateClass.respond!=Servlet.respond):
1000 1005
                 def __str__(self):
  1006
+                    rc = self.respond()
  1007
+                    if isinstance(rc, unicode):
  1008
+                        return rc.encode('utf-8')
  1009
+                    return rc
  1010
+                def __unicode__(self):
1001 1011
                     return self.respond()
1002 1012
             else:
1003 1013
                 def __str__(self):
  1014
+                    rc = None
  1015
+                    if hasattr(self, mainMethNameAttr):
  1016
+                        rc = getattr(self,mainMethNameAttr)()
  1017
+                    elif hasattr(self, 'respond'):
  1018
+                        rc = self.respond()
  1019
+                    else:
  1020
+                        rc = super(self.__class__, self).__str__()
  1021
+                    if isinstance(rc, unicode):
  1022
+                        return rc.encode('utf-8')
  1023
+                    return rc
  1024
+                def __unicode__(self):
1004 1025
                     if hasattr(self, mainMethNameAttr):
1005 1026
                         return getattr(self,mainMethNameAttr)()
1006 1027
                     elif hasattr(self, 'respond'):
1007 1028
                         return self.respond()
1008 1029
                     else:
1009  
-                        return super(self.__class__, self).__str__()
  1030
+                        return super(self.__class__, self).__unicode__()
1010 1031
                     
1011 1032
             __str__ = new.instancemethod(__str__, None, concreteTemplateClass)
  1033
+            __unicode__ = new.instancemethod(__unicode__, None, concreteTemplateClass)
1012 1034
             setattr(concreteTemplateClass, '__str__', __str__)
  1035
+            setattr(concreteTemplateClass, '__unicode__', __unicode__)
1013 1036
                 
1014 1037
     _addCheetahPlumbingCodeToClass = classmethod(_addCheetahPlumbingCodeToClass)
1015 1038
 
16  cheetah/Tests/Unicode.py
@@ -150,6 +150,22 @@ def testDynamicCompile(self):
150 150
         a = unicode(template).encode("utf-8")
151 151
         self.assertEquals("Bébé", a)
152 152
 
  153
+class EncodeUnicodeCompatTest(unittest.TestCase):
  154
+    """
  155
+        Taken initially from Red Hat's bugzilla #529332
  156
+        https://bugzilla.redhat.com/show_bug.cgi?id=529332
  157
+    """
  158
+    def runTest(self):
  159
+        t = Template("""Foo ${var}""", filter='EncodeUnicode')
  160
+        t.var = u"Text with some non-ascii characters: åäö"
  161
+        
  162
+        rc = t.respond()
  163
+        assert isinstance(rc, unicode), ('Template.respond() should return unicode', rc)
  164
+        
  165
+        rc = str(t)
  166
+        assert isinstance(rc, str), ('Template.__str__() should return a UTF-8 encoded string', rc)
  167
+
  168
+
153 169
 class Unicode_in_SearchList_Test(CommandLineTest):
154 170
     def test_BasicASCII(self):
155 171
         source = '''This is $adjective'''

0 notes on commit 62481b7

Please sign in to comment.
Something went wrong with that request. Please try again.