[pypy-svn] r35715 - in pypy/dist/pypy: objspace/std objspace/std/test rlib rlib/test
pedronis at codespeak.net
pedronis at codespeak.net
Thu Dec 14 02:01:13 CET 2006
Author: pedronis
Date: Thu Dec 14 02:01:08 2006
New Revision: 35715
Modified:
pypy/dist/pypy/objspace/std/strutil.py
pypy/dist/pypy/objspace/std/test/test_strutil.py
pypy/dist/pypy/rlib/rarithmetic.py
pypy/dist/pypy/rlib/test/test_rarithmetic.py
Log:
move break_up_float to rarithmetic where parts_to_float already lives. After the move it raises ValueError!
Preparation for supporting in RPython directly float(string).
Modified: pypy/dist/pypy/objspace/std/strutil.py
==============================================================================
--- pypy/dist/pypy/objspace/std/strutil.py (original)
+++ pypy/dist/pypy/objspace/std/strutil.py Thu Dec 14 02:01:08 2006
@@ -2,7 +2,7 @@
Pure Python implementation of string utilities.
"""
-from pypy.rlib.rarithmetic import ovfcheck, parts_to_float
+from pypy.rlib.rarithmetic import ovfcheck, break_up_float, parts_to_float
from pypy.interpreter.error import OperationError
import math
@@ -139,58 +139,6 @@
return w_result
w_result = space.add(space.mul(w_result,w_base), space.newlong(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 literal for float()")
-
- i += 1
- if i == len(s):
- raise ParseStringError("invalid literal for float()")
-
- if s[i] in '-+':
- exponent += s[i]
- i += 1
-
- if i == len(s):
- raise ParseStringError("invalid literal for float()")
-
- while i < len(s) and s[i] in '0123456789':
- exponent += s[i]
- i += 1
-
- if i != len(s):
- raise ParseStringError("invalid literal for float()")
-
- return sign, before_point, after_point, exponent
-
-
def string_to_float(s):
"""
Conversion of string to float.
@@ -204,7 +152,10 @@
raise ParseStringError("empty string for float()")
# 1) parse the string into pieces.
- sign, before_point, after_point, exponent = break_up_float(s)
+ try:
+ sign, before_point, after_point, exponent = break_up_float(s)
+ except ValueError:
+ raise ParseStringError("invalid literal for float()")
if not before_point and not after_point:
raise ParseStringError("invalid literal for float()")
@@ -291,8 +242,11 @@
raise ParseStringError("empty string for float()")
# 1) parse the string into pieces.
- sign, before_point, after_point, exponent = break_up_float(s)
-
+ try:
+ sign, before_point, after_point, exponent = break_up_float(s)
+ except ValueError:
+ raise ParseStringError("invalid literal for float()")
+
digits = before_point + after_point
if digits:
raise ParseStringError("invalid literal for float()")
@@ -384,7 +338,10 @@
"empty string for float()"))
# 1) parse the string into pieces.
- sign, before_point, after_point, exponent = break_up_float(s)
+ try:
+ sign, before_point, after_point, exponent = break_up_float(s)
+ except ValueError:
+ raise ParseStringError("invalid literal for float()")
digits = before_point + after_point
if not digits:
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 Thu Dec 14 02:01:08 2006
@@ -127,27 +127,6 @@
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')
-
-
def test_string_to_float():
assert string_to_float('0') == 0.0
assert string_to_float('1') == 1.0
Modified: pypy/dist/pypy/rlib/rarithmetic.py
==============================================================================
--- pypy/dist/pypy/rlib/rarithmetic.py (original)
+++ pypy/dist/pypy/rlib/rarithmetic.py Thu Dec 14 02:01:08 2006
@@ -337,6 +337,60 @@
r_longlong = build_int('r_longlong', True, 64)
r_ulonglong = build_int('r_ulonglong', False, 64)
+
+# float as string -> sign, beforept, afterpt, exponent
+
+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 ValueError
+
+ i += 1
+ if i == len(s):
+ raise ValueError
+
+ if s[i] in '-+':
+ exponent += s[i]
+ i += 1
+
+ if i == len(s):
+ raise ValueError
+
+ while i < len(s) and s[i] in '0123456789':
+ exponent += s[i]
+ i += 1
+
+ if i != len(s):
+ raise ValueError
+
+ return sign, before_point, after_point, exponent
+
# string -> float helper
def parts_to_float(sign, beforept, afterpt, exponent):
Modified: pypy/dist/pypy/rlib/test/test_rarithmetic.py
==============================================================================
--- pypy/dist/pypy/rlib/test/test_rarithmetic.py (original)
+++ pypy/dist/pypy/rlib/test/test_rarithmetic.py Thu Dec 14 02:01:08 2006
@@ -267,6 +267,25 @@
assert type(abs(r_longlong(1))) is r_longlong
+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(ValueError, break_up_float, 'e')
+
class BaseTestRarithmetic(BaseRtypingTest):
def test_formatd(self):
from pypy.rlib.rarithmetic import formatd
@@ -275,6 +294,20 @@
res = self.ll_to_string(self.interpret(f, [10/3.0]))
assert res == '3.33'
+ def test_parts_to_float(self):
+ from pypy.rlib.rarithmetic import parts_to_float, break_up_float
+ def f(x):
+ if x == 0:
+ s = '1.0'
+ else:
+ s = '1e-100'
+ sign, beforept, afterpt, expt = break_up_float(s)
+ return parts_to_float(sign, beforept, afterpt, expt)
+ res = self.interpret(f, [0])
+ assert res == 1.0
+
+ res = self.interpret(f, [1])
+ assert res == 1e-100
class TestLLtype(BaseTestRarithmetic, LLRtypeMixin):
pass
More information about the Pypy-commit
mailing list