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

iko at codespeak.net iko at codespeak.net
Sun Oct 12 15:00:41 CEST 2008


Author: iko
Date: Sun Oct 12 15:00:40 2008
New Revision: 59029

Modified:
   pypy/trunk/pypy/objspace/std/test/test_unicodeobject.py
   pypy/trunk/pypy/objspace/std/unicodeobject.py
Log:
(iko, antocuni)
Make unicode expandtabs raise OverflowError if the string would become too large



Modified: pypy/trunk/pypy/objspace/std/test/test_unicodeobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/test/test_unicodeobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/test/test_unicodeobject.py	Sun Oct 12 15:00:40 2008
@@ -399,6 +399,12 @@
         assert u'xy'.expandtabs() =='xy'
         assert u''.expandtabs() ==''
 
+    def test_expandtabs_overflows_gracefully(self):
+        import sys
+        if sys.maxint > (1 << 32):
+            skip("Wrong platform")
+        raises(OverflowError, u't\tt\t'.expandtabs, sys.maxint)
+        
     def test_translate(self):
         assert u'bbbc' == u'abababc'.translate({ord('a'):None})
         assert u'iiic' == u'abababc'.translate({ord('a'):None, ord('b'):ord('i')})

Modified: pypy/trunk/pypy/objspace/std/unicodeobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/unicodeobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/unicodeobject.py	Sun Oct 12 15:00:40 2008
@@ -857,14 +857,20 @@
     parts = _split_with(self, u'\t')
     result = [parts[0]]
     prevsize = 0
+    totalsize = 0
     for ch in parts[0]:
         prevsize += 1
         if ch == u"\n" or ch ==  u"\r":
             prevsize = 0
+            
     for i in range(1, len(parts)):
         pad = tabsize - prevsize % tabsize
-        result.append(u' ' * pad)
         nextpart = parts[i]
+        try:
+            totalsize = ovfcheck(totalsize + pad + len(nextpart))
+            result.append(u' ' * pad)
+        except (OverflowError, MemoryError):
+            raise OperationError(space.w_OverflowError, space.wrap('new string is too long'))
         result.append(nextpart)
         prevsize = 0
         for ch in nextpart:



More information about the Pypy-commit mailing list