[pypy-svn] r5712 - in pypy/trunk/src/pypy/objspace/std: . test

mwh at codespeak.net mwh at codespeak.net
Tue Jul 27 14:22:41 CEST 2004


Author: mwh
Date: Tue Jul 27 14:22:40 2004
New Revision: 5712

Added:
   pypy/trunk/src/pypy/objspace/std/test/test_unicodestring.py
Modified:
   pypy/trunk/src/pypy/objspace/std/fake.py
   pypy/trunk/src/pypy/objspace/std/stringobject.py
   pypy/trunk/src/pypy/objspace/std/test/test_stringobject.py
   pypy/trunk/src/pypy/objspace/std/unicodeobject.py
Log:
Sundry changes to strings, unicode and the interaction betwixt
the two.

In particular:

Robustify str.join against non-strings in the passed list.  DTRT
when unicode is found.

Implement 2.3 semantics for str.__contains__.

Unbreak string->unicode delegation.  Make mixing strings and
unicode in addition work.  Also make '' in u'' and u'' in ''
work.

Tests for the above.

All this meant I needed to special case unicode in fake.py a
little more again.  Oh well..


Modified: pypy/trunk/src/pypy/objspace/std/fake.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/fake.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/fake.py	Tue Jul 27 14:22:40 2004
@@ -36,7 +36,8 @@
     print 'faking %r'%(cpy_type,)
     kw = {}
     for s, v in cpy_type.__dict__.items():
-        kw[s] = v
+        if cpy_type is not unicode or s not in ['__add__', '__contains__']:
+            kw[s] = v
     def fake__new__(space, w_type, *args_w):
         args = [space.unwrap(w_arg) for w_arg in args_w]
         try:
@@ -60,7 +61,7 @@
     def fake_unwrap(space, w_obj):
         return w_obj.val
     StdObjSpace.unwrap.register(fake_unwrap, W_Fake)
-    W_Fake.__name__ = 'W_Fake(%s)'%(cpy_type.__name__)
+    W_Fake.__name__ = 'W_Fake%s'%(cpy_type.__name__.capitalize())
     W_Fake.typedef.fakedcpytype = cpy_type
     # XXX obviously this entire file is something of a hack, but it
     # manages to get worse here:

Modified: pypy/trunk/src/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/stringobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/stringobject.py	Tue Jul 27 14:22:40 2004
@@ -328,10 +328,18 @@
         self = u(w_self)
         firstelem = 1
         listlen = 0
-        reslen = 0 
+        reslen = 0
         #compute the length of the resulting string 
-        for w_item in list:
-            reslen = reslen + len(u(w_item))
+        for i in range(len(list)):
+            if not space.is_true(space.isinstance(list[i], space.w_str)):
+                if space.is_true(space.isinstance(list[i], space.w_unicode)):
+                    w_u = space.call_function(space.w_unicode, w_self)
+                    return space.call_method(w_u, "join", w_list)
+                raise OperationError(
+                    space.w_TypeError,
+                    space.wrap("sequence item %d: expected string, %s "
+                               "found"%(i, space.type(list[i]).name)))
+            reslen = reslen + len(u(list[i]))
             listlen = listlen + 1
 
         reslen = reslen + (listlen - 1) * len(self)
@@ -346,8 +354,8 @@
             if firstelem:
                 for i in range(len(item)):
                     res[i+pos] = item[i]
-                firstelem = 0
                 pos = pos + len(item)
+                firstelem = 0
             else:
                 for i in range(len(self)):
                     res[i+pos] = self[i]
@@ -931,6 +939,10 @@
     from pypy.objspace.std import iterobject
     return iterobject.W_SeqIterObject(space, w_list)
 
+def app_contains__String_String(self, sub):
+    return self.find(sub) >= 0
+
+contains__String_String = gateway.app2interp(app_contains__String_String)
 
 def app_repr__String(s):
     quote = "'"

Modified: pypy/trunk/src/pypy/objspace/std/test/test_stringobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/test/test_stringobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/test/test_stringobject.py	Tue Jul 27 14:22:40 2004
@@ -366,6 +366,9 @@
         self.assertEquals(", ".join(['a', 'b', 'c']), "a, b, c")
         self.assertEquals("".join([]), "")
         self.assertEquals("-".join(['a', 'b']), 'a-b')
+        self.assertRaises(TypeError, ''.join, 1)
+        self.assertRaises(TypeError, ''.join, [1])
+        self.assertRaises(TypeError, ''.join, [[1]])
 
     def test_lower(self):
         self.assertEquals("aaa AAA".lower(), "aaa aaa")
@@ -472,6 +475,13 @@
         self.assertEquals(repr("'''\"") ,'\'\\\'\\\'\\\'"\'')
         self.assertEquals(repr(chr(19)) ,"'\\x13'")
         self.assertEquals(repr(chr(2))  ,"'\\x02'")
+
+    def test_contains(self):
+        self.failUnless('' in 'abc')
+        self.failUnless('a' in 'abc')
+        self.failUnless('ab' in 'abc')
+        self.failIf('d' in 'abc')
+        self.assertRaises(TypeError, 'a'.__contains__, 1)
     
 
 if __name__ == '__main__':

Added: pypy/trunk/src/pypy/objspace/std/test/test_unicodestring.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/objspace/std/test/test_unicodestring.py	Tue Jul 27 14:22:40 2004
@@ -0,0 +1,37 @@
+# test the integration of unicode and strings (even though we don't
+# really implement unicode yet).
+
+import autopath
+from pypy.tool import testit
+
+
+class TestUnicodeString(testit.AppTestCase):
+    def test_compares(self):
+        self.assertEqual(u'a', 'a')
+        self.assertEqual('a', u'a')
+        self.assertNotEqual(u'a', 'b')
+        self.assertNotEqual('a', u'b')
+
+    def test_addition(self):
+        def check(a, b):
+            self.assertEqual(a, b)
+            self.assertEqual(type(a), type(b))
+        check(u'a' + 'b', u'ab')
+        check('a' + u'b', u'ab')
+
+    def test_join(self):
+        def check(a, b):
+            self.assertEqual(a, b)
+            self.assertEqual(type(a), type(b))
+        check(', '.join([u'a']), u'a')
+        check(', '.join(['a', u'b']), u'a, b')
+        check(u', '.join(['a', 'b']), u'a, b')
+
+    def test_contains(self):
+        self.failUnless(u'' in 'abc')
+        self.failUnless(u'a' in 'abc')
+        self.failUnless('a' in u'abc')
+        
+
+if __name__ == '__main__':
+    testit.main()

Modified: pypy/trunk/src/pypy/objspace/std/unicodeobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/unicodeobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/unicodeobject.py	Tue Jul 27 14:22:40 2004
@@ -6,7 +6,7 @@
 
 # string-to-unicode delegation
 def delegate__String(space, w_str):
-    return W_Unicode(space, unicode(w_str))
+    return W_UnicodeObject(space, unicode(space.unwrap(w_str)))
 delegate__String.result_class = W_UnicodeObject
 delegate__String.priority = PRIORITY_CHANGE_TYPE
 delegate__String.can_fail = True
@@ -28,6 +28,14 @@
         return space.wrap(ord(w_uni.val))
     except:
         wrap_exception(space)
-    
+
+def add__Unicode_Unicode(space, w_left, w_right):
+    return space.wrap(space.unwrap(w_left) + space.unwrap(w_right))
+
+def contains__String_Unicode(space, w_left, w_right):
+    return space.wrap(space.unwrap(w_right) in space.unwrap(w_left))
+
+def contains__Unicode_Unicode(space, w_left, w_right):
+    return space.wrap(space.unwrap(w_right) in space.unwrap(w_left))
 
 register_all(vars())



More information about the Pypy-commit mailing list