[pypy-svn] r4810 - in pypy/trunk/src/pypy/objspace/std: . test
alex at codespeak.net
alex at codespeak.net
Tue Jun 1 16:30:04 CEST 2004
Author: alex
Date: Tue Jun 1 16:30:03 2004
New Revision: 4810
Modified:
pypy/trunk/src/pypy/objspace/std/stringobject.py
pypy/trunk/src/pypy/objspace/std/test/test_stringformat.py
Log:
better errors for too many or too few %'s
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:30:03 2004
@@ -955,24 +955,29 @@
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)
+ try:
+ 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)
+ except IndexError:
+ raise TypeError, 'not enough arguments for format string'
state = 0
start = i+1
if state == 1:
raise ValueError, "incomplete format"
+ if l:
+ raise TypeError, 'not all arguments converted during string formatting'
pieces.append(format[start:])
return ''.join(pieces)
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 16:30:03 2004
@@ -17,6 +17,12 @@
self.assertEquals('a%', 'a%%' % ())
self.assertEquals('%', '%%' % ())
+ def test_format_wronglength(self):
+ self.assertRaises(TypeError, '%s%s'.__mod__, ())
+ self.assertRaises(TypeError, '%s%s'.__mod__, (23,))
+ self.assertRaises(TypeError, '%s%s'.__mod__, (23,)*3)
+ self.assertRaises(TypeError, '%s%s'.__mod__, (23,)*4)
+
def test_format_wrongchar(self):
self.assertRaises(ValueError, 'a%Zb'.__mod__, ((23,),))
More information about the Pypy-commit
mailing list