[pypy-svn] r43593 - in pypy/dist/pypy/rpython: lltypesystem ootypesystem test

antocuni at codespeak.net antocuni at codespeak.net
Thu May 24 13:37:27 CEST 2007


Author: antocuni
Date: Thu May 24 13:37:27 2007
New Revision: 43593

Modified:
   pypy/dist/pypy/rpython/lltypesystem/rstr.py
   pypy/dist/pypy/rpython/ootypesystem/rstr.py
   pypy/dist/pypy/rpython/test/test_rstr.py
Log:
test&fix for int('+') and int('+ 42')



Modified: pypy/dist/pypy/rpython/lltypesystem/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/rstr.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/rstr.py	Thu May 24 13:37:27 2007
@@ -650,8 +650,12 @@
             i += 1
         elif chars[i] == '+':
             i += 1;
+        # skip whitespaces between sign and digits
+        while i < strlen and chars[i] == ' ':
+            i += 1
         #now get digits
         val = 0
+        oldpos = i
         while i < strlen:
             c = ord(chars[i])
             if ord('a') <= c <= ord('z'):
@@ -666,6 +670,8 @@
                 break
             val = val * base + digit
             i += 1
+        if i == oldpos:
+            raise ValueError # catch strings like '+' and '+  '
         #skip trailing whitespace
         while i < strlen and chars[i] == ' ':
             i += 1

Modified: pypy/dist/pypy/rpython/ootypesystem/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/rstr.py	(original)
+++ pypy/dist/pypy/rpython/ootypesystem/rstr.py	Thu May 24 13:37:27 2007
@@ -172,8 +172,12 @@
             i += 1
         elif s.ll_stritem_nonneg(i) == '+':
             i += 1;
+        # skip whitespaces between sign and digits
+        while i < strlen and s.ll_stritem_nonneg(i) == ' ':
+            i += 1
         #now get digits
         val = 0
+        oldpos = i
         while i < strlen:
             c = ord(s.ll_stritem_nonneg(i))
             if ord('a') <= c <= ord('z'):
@@ -188,6 +192,8 @@
                 break
             val = val * base + digit
             i += 1
+        if i == oldpos:
+            raise ValueError # catch strings like '+' and '+  '
         #skip trailing whitespace
         while i < strlen and s.ll_stritem_nonneg(i) == ' ':
             i += 1

Modified: pypy/dist/pypy/rpython/test/test_rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rstr.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rstr.py	Thu May 24 13:37:27 2007
@@ -577,7 +577,7 @@
         raises(TyperError, self.interpret, fn, ())
 
     def test_int(self):
-        s1 = [ '42', '01001', 'abc', 'ABC', '4aBc', ' 12ef ', '+42', 'foo', '42foo', '42.1', '']
+        s1 = [ '42', '01001', 'abc', 'ABC', '4aBc', ' 12ef ', '+42', 'foo', '42foo', '42.1', '', '+ 42']
         def fn(i, base):
             s = s1[i]
             res = int(s, base)
@@ -593,7 +593,7 @@
                     assert res == expected
 
     def test_int_valueerror(self):
-        s1 = ['42g', '?']
+        s1 = ['42g', '?', '+', '+ ']
         def fn(i):
             try:
                 return int(s1[i])
@@ -603,6 +603,8 @@
         assert res == -654
         res = self.interpret(fn, [1])
         assert res == -654
+        res = self.interpret(fn, [2])
+        assert res == -654
 
     def test_float(self):
         f = ['', '    ', '0', '1', '-1.5', '1.5E2', '2.5e-1', ' 0 ', '?']


More information about the pypy-svn mailing list