[pypy-svn] r13771 - in pypy/dist/pypy/objspace/std: . test

mwh at codespeak.net mwh at codespeak.net
Fri Jun 24 12:53:53 CEST 2005


Author: mwh
Date: Fri Jun 24 12:53:51 2005
New Revision: 13771

Modified:
   pypy/dist/pypy/objspace/std/inttype.py
   pypy/dist/pypy/objspace/std/strutil.py
   pypy/dist/pypy/objspace/std/test/test_strutil.py
   pypy/dist/pypy/objspace/std/unicodeobject.py
Log:
some grotty float parsing code, not much tested yet (armin has written
tests), nor hooked up yet.

also removed an unused space argument to string_to_int and some
unused imports in unicodeobject.py


Modified: pypy/dist/pypy/objspace/std/inttype.py
==============================================================================
--- pypy/dist/pypy/objspace/std/inttype.py	(original)
+++ pypy/dist/pypy/objspace/std/inttype.py	Fri Jun 24 12:53:51 2005
@@ -22,7 +22,7 @@
             value = w_value.intval
         elif space.is_true(space.isinstance(w_value, space.w_str)):
             try:
-                value = string_to_int(space, space.str_w(w_value))
+                value = string_to_int(space.str_w(w_value))
             except ParseStringError, e:
                 raise OperationError(space.w_ValueError,
                                      space.wrap(e.msg))
@@ -32,7 +32,7 @@
             from unicodeobject import unicode_to_decimal_w
             string = unicode_to_decimal_w(space, w_value)
             try:
-                value = string_to_int(space, string)
+                value = string_to_int(string)
             except ParseStringError, e:
                 raise OperationError(space.w_ValueError,
                                      space.wrap(e.msg))
@@ -68,7 +68,7 @@
                                      space.wrap("int() can't convert non-string "
                                                 "with explicit base"))
         try:
-            value = string_to_int(space, s, base)
+            value = string_to_int(s, base)
         except ParseStringError, e:
             raise OperationError(space.w_ValueError,
                                  space.wrap(e.msg))

Modified: pypy/dist/pypy/objspace/std/strutil.py
==============================================================================
--- pypy/dist/pypy/objspace/std/strutil.py	(original)
+++ pypy/dist/pypy/objspace/std/strutil.py	Fri Jun 24 12:53:51 2005
@@ -86,7 +86,7 @@
         else:
             return -1
 
-def string_to_int(space, s, base=10):
+def string_to_int(s, base=10):
     """Utility to converts a string to an integer (or possibly a long).
     If base is 0, the proper base is guessed based on the leading
     characters of 's'.  Raises ParseStringError in case of error.
@@ -134,3 +134,88 @@
                 return w_result
         w_result = space.add(space.mul(w_result,w_base),space.newlong(r_uint(digit)))
 
+def break_up_float(s):
+    i = 0
+
+    sign = ''
+    before_point = ''
+    after_point = ''
+    exponent = ''
+
+    if s[i] in '+-':
+        sign = s[i]
+        i += 1
+
+    while i < len(s) and s[i] in '0123456789':
+        before_point += s[i]
+        i += 1
+
+    if i == len(s):
+        return sign, before_point, after_point, exponent
+
+    if s[i] == '.':
+        i += 1
+        while i < len(s) and s[i] in '0123456789':
+            after_point += s[i]
+            i += 1
+            
+        if i == len(s):
+            return sign, before_point, after_point, exponent
+
+    if s[i] not in  'eE':
+        raise ParseStringError("invalid string literal for float()")
+
+    i += 1
+    if i == len(s):
+        raise ParseStringError("invalid string literal for float()")
+
+    if s[i] in '-+':
+        exponent += s[i]
+        i += 1
+
+    if i == len(s):
+        raise ParseStringError("invalid string literal for float()")
+    
+    while i < len(s) and s[i] in '0123456789':
+        exponent += s[i]
+        i += 1
+
+    if i != len(s):
+        raise ParseStringError("invalid string literal for float()")
+
+    return sign, before_point, after_point, exponent
+
+
+def string_to_float(s):
+    s = strip_space(s)
+
+    if not s:
+        raise ParseStringError("empty string for float()")
+
+    sign, before_point, after_point, exponent = break_up_float(s)
+    
+    if not before_point and not after_point:
+        raise ParseStringError("invalid string literal for float()")
+
+    r = 0.0
+    i = len(before_point) - 1
+    j = 0
+    while i >= 0:
+        d = float(ord(before_point[i]) - ord('0'))
+        r += d * (10.0 ** j)
+        i -= 1
+        j += 1
+
+    i = 0
+    while i < len(after_point):
+        d = float(ord(after_point[i]) - ord('0'))
+        r += d * (10.0 ** (-i-1))
+
+    if exponent:
+        e = string_to_int(None, exponent)
+        r *= 10.0 ** e
+
+    return r
+        
+
+    

Modified: pypy/dist/pypy/objspace/std/test/test_strutil.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_strutil.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_strutil.py	Fri Jun 24 12:53:51 2005
@@ -1,6 +1,8 @@
 import autopath
 from pypy.objspace.std.strutil import *
 
+import py
+
 objspacename = 'std'
 
 class TestStrUtil:
@@ -24,7 +26,7 @@
                  ('  -123456789 ', -123456789),
                  ]
         for s, expected in cases:
-            assert string_to_int(space, s) == expected
+            assert string_to_int(s) == expected
             assert string_to_w_long(space, s).longval() == expected
 
     def test_string_to_int_base(self):
@@ -54,12 +56,12 @@
                  ('0X',  16, 0),    #     "           "
                  ]
         for s, base, expected in cases:
-            assert string_to_int(space, s, base) == expected
-            assert string_to_int(space, '+'+s, base) == expected
-            assert string_to_int(space, '-'+s, base) == -expected
-            assert string_to_int(space, s+'\n', base) == expected
-            assert string_to_int(space, '  +'+s, base) == expected
-            assert string_to_int(space, '-'+s+'  ', base) == -expected
+            assert string_to_int(s, base) == expected
+            assert string_to_int('+'+s, base) == expected
+            assert string_to_int('-'+s, base) == -expected
+            assert string_to_int(s+'\n', base) == expected
+            assert string_to_int('  +'+s, base) == expected
+            assert string_to_int('-'+s+'  ', base) == -expected
 
     def test_string_to_int_error(self):
         space = self.space
@@ -78,16 +80,16 @@
                  '@',
                  ]
         for s in cases:
-            raises(ParseStringError, string_to_int, space, s)
-            raises(ParseStringError, string_to_int, space, '  '+s)
-            raises(ParseStringError, string_to_int, space, s+'  ')
-            raises(ParseStringError, string_to_int, space, '+'+s)
-            raises(ParseStringError, string_to_int, space, '-'+s)
+            raises(ParseStringError, string_to_int, s)
+            raises(ParseStringError, string_to_int, '  '+s)
+            raises(ParseStringError, string_to_int, s+'  ')
+            raises(ParseStringError, string_to_int, '+'+s)
+            raises(ParseStringError, string_to_int, '-'+s)
 
     def test_string_to_int_overflow(self):
         import sys
         space = self.space
-        raises(ParseStringOverflowError, string_to_int, space,
+        raises(ParseStringOverflowError, string_to_int,
                str(sys.maxint*17))
 
     def test_string_to_int_base_error(self):
@@ -107,11 +109,11 @@
                  ('12.3', 16),
                  ]
         for s, base in cases:
-            raises(ParseStringError, string_to_int, space, s, base)
-            raises(ParseStringError, string_to_int, space, '  '+s, base)
-            raises(ParseStringError, string_to_int, space, s+'  ', base)
-            raises(ParseStringError, string_to_int, space, '+'+s, base)
-            raises(ParseStringError, string_to_int, space, '-'+s, base)
+            raises(ParseStringError, string_to_int, s, base)
+            raises(ParseStringError, string_to_int, '  '+s, base)
+            raises(ParseStringError, string_to_int, s+'  ', base)
+            raises(ParseStringError, string_to_int, '+'+s, base)
+            raises(ParseStringError, string_to_int, '-'+s, base)
 
     def test_string_to_w_long(self):
         space = self.space
@@ -124,3 +126,23 @@
         assert string_to_w_long(space, '123L', 22).longval() == 10648 + 968 + 66 + 21
         assert string_to_w_long(space, '123L', 21).longval() == 441 + 42 + 3
         assert string_to_w_long(space, '1891234174197319').longval() == 1891234174197319
+
+
+def test_break_up_float():
+    assert break_up_float('1') == ('', '1', '', '')
+    assert break_up_float('+1') == ('+', '1', '', '')
+    assert break_up_float('-1') == ('-', '1', '', '')
+
+    assert break_up_float('.5') == ('', '', '5', '')
+    
+    assert break_up_float('1.2e3') == ('', '1', '2', '3')
+    assert break_up_float('1.2e+3') == ('', '1', '2', '+3')
+    assert break_up_float('1.2e-3') == ('', '1', '2', '-3')
+
+    # some that will get thrown out on return:
+    assert break_up_float('.') == ('', '', '', '')
+    assert break_up_float('+') == ('+', '', '', '')
+    assert break_up_float('-') == ('-', '', '', '')
+    assert break_up_float('e1') == ('', '', '', '1')
+
+    py.test.raises(ParseStringError, break_up_float, 'e')

Modified: pypy/dist/pypy/objspace/std/unicodeobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/unicodeobject.py	(original)
+++ pypy/dist/pypy/objspace/std/unicodeobject.py	Fri Jun 24 12:53:51 2005
@@ -4,7 +4,6 @@
 from pypy.objspace.std.noneobject import W_NoneObject
 from pypy.objspace.std.sliceobject import W_SliceObject
 from pypy.objspace.std import slicetype
-from pypy.objspace.std.strutil import string_to_int, string_to_long, ParseStringError
 from pypy.rpython.rarithmetic import intmask
 from pypy.module.unicodedata import unicodedb
 



More information about the Pypy-commit mailing list