[pypy-svn] r4808 - in pypy/trunk/src/pypy/objspace/std: . test
alex at codespeak.net
alex at codespeak.net
Tue Jun 1 16:15:49 CEST 2004
Author: alex
Date: Tue Jun 1 16:15:49 2004
New Revision: 4808
Modified:
pypy/trunk/src/pypy/objspace/std/stringobject.py
pypy/trunk/src/pypy/objspace/std/test/test_stringobject.py
Log:
a first dim beginning of string-formatting
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 16:15:49 2004
@@ -938,17 +938,47 @@
return space.wrap(ord(space.unwrap(w_str)))
def mod__String_ANY(space, w_str, w_item):
- return mod_str_tuple(space, w_str, space.newtuple([w_item]))
+ return mod__String_Tuple(space, w_str, space.newtuple([w_item]))
-def mod__String_ANY(space, w_str, w_tuple):
- return mod_str_tuple(space, w_str, w_tuple)
+def app_mod__String_Tuple(format, values):
+ l = list(values)
+ l.reverse()
+ pieces = []
+ start = 0
+ state = 0
+ for i in range(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
+ else:
+ if c=='%':
+ pieces.append('%')
+ elif c=='s':
+ pieces.append(str(l.pop()))
+ elif c=='d':
+ pieces.append(str(int(l.pop())))
+ elif c=='x':
+ pieces.append(hex(int(l.pop())))
+ elif c=='r':
+ pieces.append(repr(l.pop()))
+ else:
+ raise ValueError, "unsupported format character '%s' (%x) at index %d" % (
+ c, ord(c), i)
+ state = 0
+ start = i+1
-#def app_mod__String_Tuple(format, values):
-# l = list(values)
-# l.reverse()
-# for formatchars = list(format)
+ if state == 1:
+ raise ValueError, "incomplete format"
-#mod__String_Tuple = gateway.app2interp(app_mod__String_Tuple)
+ pieces.append(format[start:])
+ return ''.join(pieces)
+
+
+mod__String_Tuple = gateway.app2interp(app_mod__String_Tuple)
# register all methods
register_all(vars(), W_StringType)
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 Jun 1 16:15:49 2004
@@ -118,6 +118,9 @@
def setUp(self):
self.space = testit.objspace('std')
+ def test_format_wrongchar(self):
+ self.assertRaises(ValueError, 'a%Zb'.__mod__, ((23,),))
+
def test_split(self):
self.assertEquals("".split(), [])
self.assertEquals("a".split(), ['a'])
More information about the Pypy-commit
mailing list