[Zope-Checkins] SVN: Zope/branches/yuppie-tal-backports/lib/python/DocumentTemplate/ made sure custom strings like MessageID are not converted

Yvo Schubbe y.2005- at wcm-solutions.de
Mon Aug 1 04:06:12 EDT 2005


Log message for revision 37613:
  made sure custom strings like MessageID are not converted

Changed:
  UU  Zope/branches/yuppie-tal-backports/lib/python/DocumentTemplate/tests/testustr.py
  UU  Zope/branches/yuppie-tal-backports/lib/python/DocumentTemplate/ustr.py

-=-
Modified: Zope/branches/yuppie-tal-backports/lib/python/DocumentTemplate/tests/testustr.py
===================================================================
--- Zope/branches/yuppie-tal-backports/lib/python/DocumentTemplate/tests/testustr.py	2005-08-01 07:32:24 UTC (rev 37612)
+++ Zope/branches/yuppie-tal-backports/lib/python/DocumentTemplate/tests/testustr.py	2005-08-01 08:06:12 UTC (rev 37613)
@@ -7,21 +7,19 @@
 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE
+# FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-"""Document Template Tests
+"""ustr unit tests.
+
+$Id$
 """
 
-__rcs_id__='$Id$'
-__version__='$Revision: 1.3 $'[11:-2]
-
-import sys, os
 import unittest
 
 from DocumentTemplate.ustr import ustr
-from ExtensionClass import Base
 
+
 class force_str:
     # A class whose string representation is not always a plain string:
     def __init__(self,s):
@@ -29,8 +27,19 @@
     def __str__(self):
         return self.s
 
-class UnicodeTests (unittest.TestCase):
 
+class Foo(str):
+
+    pass
+
+
+class Bar(unicode):
+
+    pass
+
+
+class UnicodeTests(unittest.TestCase):
+
     def testPlain(self):
         a = ustr('hello')
         assert a=='hello', `a`
@@ -59,13 +68,17 @@
         a = ustr(ValueError(unichr(200)))
         assert a==unichr(200), `a`
 
+    def testCustomStrings(self):
+        a = ustr(Foo('foo'))
+        self.failUnlessEqual(type(a), Foo)
+        a = ustr(Bar('bar'))
+        self.failUnlessEqual(type(a), Bar)
+
+
 def test_suite():
     suite = unittest.TestSuite()
     suite.addTest( unittest.makeSuite( UnicodeTests ) )
     return suite
 
-def main():
-    unittest.TextTestRunner().run(test_suite())
-
 if __name__ == '__main__':
-    main()
+    unittest.main(defaultTest='test_suite')


Property changes on: Zope/branches/yuppie-tal-backports/lib/python/DocumentTemplate/tests/testustr.py
___________________________________________________________________
Name: cvs2svn:cvs-rev
   - 1.3

Modified: Zope/branches/yuppie-tal-backports/lib/python/DocumentTemplate/ustr.py
===================================================================
--- Zope/branches/yuppie-tal-backports/lib/python/DocumentTemplate/ustr.py	2005-08-01 07:32:24 UTC (rev 37612)
+++ Zope/branches/yuppie-tal-backports/lib/python/DocumentTemplate/ustr.py	2005-08-01 08:06:12 UTC (rev 37613)
@@ -7,11 +7,13 @@
 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE
+# FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
+"""ustr function.
 
-from types import StringType, UnicodeType, InstanceType
+$Id$
+"""
 
 nasty_exception_str = Exception.__str__.im_func
 
@@ -20,8 +22,7 @@
     minimising the chance of raising a UnicodeError. This
     even works with uncooperative objects like Exceptions
     """
-    string_types = (StringType,UnicodeType)
-    if type(v) in string_types:
+    if isinstance(v, basestring):
         return v
     else:
         fn = getattr(v,'__str__',None)
@@ -41,7 +42,7 @@
             else:
                 # Trust the object to do this right
                 v = fn()
-                if type(v) in string_types:
+                if isinstance(v, basestring):
                     return v
                 else:
                     raise ValueError('__str__ returned wrong type')
@@ -59,4 +60,3 @@
         else:
             return str(exc.args)
     return str(exc)
-


Property changes on: Zope/branches/yuppie-tal-backports/lib/python/DocumentTemplate/ustr.py
___________________________________________________________________
Name: cvs2svn:cvs-rev
   - 1.4
Name: svn:keywords
   + Id



More information about the Zope-Checkins mailing list