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

hpk at codespeak.net hpk at codespeak.net
Tue Jun 1 17:47:41 CEST 2004


Author: hpk
Date: Tue Jun  1 17:47:40 2004
New Revision: 4813

Modified:
   pypy/trunk/src/pypy/objspace/std/stringobject.py
   pypy/trunk/src/pypy/objspace/std/test/test_stringformat.py
Log:
stringformatting with dict and tuples - tests added 
and pass 



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 Jun  1 17:47:40 2004
@@ -940,50 +940,67 @@
 def mod__String_ANY(space, w_str, w_item):
     return mod__String_Tuple(space, w_str, space.newtuple([w_item]))
 
-def app_mod__String_Tuple(format, values):
-    l = list(values) 
-    l.reverse()
+def app_mod__String_ANY(format, values):
     pieces = []
     start = 0
     state = 0
-    for i in range(len(format)):
+    i = 0 
+    index = -1
+    len_format = len(format) 
+    while i < len_format: 
         c = format[i]
         if state == 0:
-            """ just copy constant-pieces of the format """
-            if c!='%':
-                continue
-            pieces.append(format[start:i])
-            state = 1
+            # just copy constant-pieces of the format
+            if c=='%':
+                pieces.append(format[start:i])
+                state = 1
         else:
-            try:
-                if c=='%':
-                    pieces.append('%')
-                elif c=='s':
-                    pieces.append(str(l.pop()))
+            if c=='%':
+                pieces.append('%')
+            else:
+                if c == '(':
+                    # read name 
+                    j = format.find(')', i+1)
+                    if j == -1:
+                        raise ValueError, "incomplete format string"
+                    if index != -1:
+                        raise TypeError, "format string mismatch"
+                    name = format[i+1:j]
+                    value = values[name]
+                    index = -2 
+                    i = j+1
+                    c = format[i]
+                else:
+                    index += 1
+                    if index < 0:
+                        raise TypeError, "format string mismatch"
+                    elif index == 0 and not isinstance(values, tuple):
+                        values = tuple([values])
+                    value = values[index]
+                 
+                if c=='s':
+                    pieces.append(str(value)) 
                 elif c=='d':
-                    pieces.append(str(int(l.pop())))
+                    pieces.append(str(value))
                 elif c=='x':
-                    pieces.append(hex(int(l.pop())))
+                    pieces.append(hex(int(value)))
                 elif c=='r':
-                    pieces.append(repr(l.pop()))
+                    pieces.append(repr(value))
                 else:
                     raise ValueError, "unsupported format character '%s' (%x) at index %d" % (
                             c, ord(c), i)
-            except IndexError:
-                raise TypeError, 'not enough arguments for format string'
             state = 0
             start = i+1
+        i += 1
 
     if state == 1:
         raise ValueError, "incomplete format"
-    if l:
+    if index >= 0 and index < len(values) - 1:
         raise TypeError, 'not all arguments converted during string formatting'
-
     pieces.append(format[start:])
     return ''.join(pieces)
 
-
-mod__String_Tuple = gateway.app2interp(app_mod__String_Tuple) 
+mod__String_ANY = gateway.app2interp(app_mod__String_ANY) 
 
 # register all methods 
 register_all(vars(), W_StringType)

Modified: pypy/trunk/src/pypy/objspace/std/test/test_stringformat.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/test/test_stringformat.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/test/test_stringformat.py	Tue Jun  1 17:47:40 2004
@@ -2,6 +2,21 @@
 from pypy.tool import testit
 
 
+class TestStringObjectWithDict(testit.AppTestCase):
+
+    def setUp(self):
+        self.space = testit.objspace('std')
+
+    def test_format_item(self):
+        d = {'i': 23, '':42}
+        self.assertEquals('a23b', 'a%(i)sb' % d)
+        self.assertEquals('23b', '%(i)sb' % d)
+        self.assertEquals('a23', 'a%(i)s' % d)
+        self.assertEquals('23', '%(i)s' % d)
+        self.assertEquals('a%b', 'a%%b' % d) 
+        self.assertEquals('42', '%()s' % d)
+        self.assertRaises(ValueError, 'a%()Zb'.__mod__, d) 
+
 class TestStringObject(testit.AppTestCase):
 
     def setUp(self):
@@ -42,6 +57,23 @@
 
     def test_format_wrongchar(self):
         self.assertRaises(ValueError, 'a%Zb'.__mod__, ((23,),))
+class TestStringObject(testit.AppTestCase):
+
+    def setUp(self):
+        self.space = testit.objspace('std')
+
+    def test_format_item(self):
+        self.assertEquals('a23b', 'a%sb' % 23)
+        self.assertEquals('23b', '%sb' % 23)
+        self.assertEquals('a23', 'a%s' % 23)
+        self.assertEquals('23', '%s' % 23)
+        self.assertEquals('a%b', 'a%%b' % ())
+        self.assertEquals('%b', '%%b' % ())
+        self.assertEquals('a%', 'a%%' % ())
+        self.assertEquals('%', '%%' % ())
+
+    def test_format_wrongchar(self):
+        self.assertRaises(ValueError, 'a%Zb'.__mod__, ((23,),))
 
 
 if __name__ == '__main__':



More information about the Pypy-commit mailing list