[pypy-svn] r78450 - pypy/trunk/pypy/objspace/std
arigo at codespeak.net
arigo at codespeak.net
Fri Oct 29 10:59:46 CEST 2010
Author: arigo
Date: Fri Oct 29 10:59:45 2010
New Revision: 78450
Modified:
pypy/trunk/pypy/objspace/std/inttype.py
pypy/trunk/pypy/objspace/std/longtype.py
pypy/trunk/pypy/objspace/std/strutil.py
Log:
Use the more efficient parse_digit_string() from rbigint.
Modified: pypy/trunk/pypy/objspace/std/inttype.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/inttype.py (original)
+++ pypy/trunk/pypy/objspace/std/inttype.py Fri Oct 29 10:59:45 2010
@@ -1,7 +1,7 @@
from pypy.interpreter import gateway
from pypy.interpreter.error import OperationError
from pypy.objspace.std.stdtypedef import StdTypeDef
-from pypy.objspace.std.strutil import (string_to_int, string_to_w_long,
+from pypy.objspace.std.strutil import (string_to_int, string_to_bigint,
ParseStringError,
ParseStringOverflowError)
from pypy.rlib.rarithmetic import r_uint
@@ -45,10 +45,12 @@
def retry_to_w_long(space, parser, base=0):
parser.rewind()
try:
- return string_to_w_long(space, None, base=base, parser=parser)
+ bigint = string_to_bigint(None, base=base, parser=parser)
except ParseStringError, e:
raise OperationError(space.w_ValueError,
space.wrap(e.msg))
+ from pypy.objspace.std.longobject import W_LongObject
+ return W_LongObject(bigint)
def descr__new__(space, w_inttype, w_x=0, w_base=gateway.NoneNotWrapped):
from pypy.objspace.std.intobject import W_IntObject
Modified: pypy/trunk/pypy/objspace/std/longtype.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/longtype.py (original)
+++ pypy/trunk/pypy/objspace/std/longtype.py Fri Oct 29 10:59:45 2010
@@ -1,7 +1,7 @@
from pypy.interpreter.error import OperationError
from pypy.interpreter import gateway
from pypy.objspace.std.stdtypedef import StdTypeDef
-from pypy.objspace.std.strutil import string_to_w_long, ParseStringError
+from pypy.objspace.std.strutil import string_to_bigint, ParseStringError
def descr__new__(space, w_longtype, w_x=0, w_base=gateway.NoneNotWrapped):
from pypy.objspace.std.longobject import W_LongObject
@@ -9,10 +9,10 @@
if w_base is None:
# check for easy cases
if type(w_value) is W_LongObject:
- pass
+ bigint = w_value.num
elif space.is_true(space.isinstance(w_value, space.w_str)):
try:
- w_value = string_to_w_long(space, space.str_w(w_value))
+ bigint = string_to_bigint(space.str_w(w_value))
except ParseStringError, e:
raise OperationError(space.w_ValueError,
space.wrap(e.msg))
@@ -22,7 +22,7 @@
from pypy.objspace.std.ropeunicodeobject import unicode_to_decimal_w
else:
from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
- w_value = string_to_w_long(space, unicode_to_decimal_w(space, w_value))
+ bigint = string_to_bigint(unicode_to_decimal_w(space, w_value))
except ParseStringError, e:
raise OperationError(space.w_ValueError,
space.wrap(e.msg))
@@ -35,10 +35,11 @@
if space.is_true(space.isinstance(w_obj, space.w_long)):
assert isinstance(w_obj, W_LongObject) # XXX this could fail!
# XXX find a way to do that even if w_obj is not a W_LongObject
- w_value = w_obj
+ bigint = w_obj.num
elif space.is_true(space.isinstance(w_obj, space.w_int)):
+ from pypy.rlib.rbigint import rbigint
intval = space.int_w(w_obj)
- w_value = W_LongObject.fromint(space, intval)
+ bigint = rbigint.fromint(intval)
else:
raise OperationError(space.w_ValueError,
space.wrap("value can't be converted to long"))
@@ -56,13 +57,13 @@
space.wrap("long() can't convert non-string "
"with explicit base"))
try:
- w_value = string_to_w_long(space, s, base)
+ bigint = string_to_bigint(s, base)
except ParseStringError, e:
raise OperationError(space.w_ValueError,
space.wrap(e.msg))
w_obj = space.allocate_instance(W_LongObject, w_longtype)
- W_LongObject.__init__(w_obj, w_value.num)
+ W_LongObject.__init__(w_obj, bigint)
return w_obj
# ____________________________________________________________
Modified: pypy/trunk/pypy/objspace/std/strutil.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/strutil.py (original)
+++ pypy/trunk/pypy/objspace/std/strutil.py Fri Oct 29 10:59:45 2010
@@ -4,6 +4,7 @@
from pypy.rlib.rarithmetic import ovfcheck, break_up_float, parts_to_float,\
INFINITY, NAN
+from pypy.rlib.rbigint import rbigint, parse_digit_string
from pypy.interpreter.error import OperationError
import math
@@ -91,9 +92,10 @@
return -1
def string_to_int(s, base=10):
- """Utility to converts a string to an integer (or possibly a long).
+ """Utility to converts a string to an integer.
If base is 0, the proper base is guessed based on the leading
characters of 's'. Raises ParseStringError in case of error.
+ Raises ParseStringOverflowError in case the result does not fit.
"""
s = literal = strip_spaces(s)
p = NumberStringParser(s, literal, base, 'int')
@@ -113,11 +115,9 @@
except OverflowError:
raise ParseStringOverflowError(p)
-def string_to_long(space, s, base=10, parser=None):
- return string_to_w_long(space, s, base, parser).longval()
-
-def string_to_w_long(space, s, base=10, parser=None):
- """As string_to_int(), but ignores an optional 'l' or 'L' suffix."""
+def string_to_bigint(s, base=10, parser=None):
+ """As string_to_int(), but ignores an optional 'l' or 'L' suffix
+ and returns an rbigint."""
if parser is None:
s = literal = strip_spaces(s)
if (s.endswith('l') or s.endswith('L')) and base < 22:
@@ -126,18 +126,7 @@
p = NumberStringParser(s, literal, base, 'long')
else:
p = parser
- w_base = space.newlong(p.base)
- w_result = space.newlong(0)
- while True:
- digit = p.next_digit()
- if digit == -1:
- if p.sign == -1:
- w_result = space.neg(w_result)
- # XXX grumble
- from pypy.objspace.std.longobject import W_LongObject
- assert isinstance(w_result, W_LongObject)
- return w_result
- w_result = space.add(space.mul(w_result,w_base), space.newlong(digit))
+ return parse_digit_string(p)
# Tim's comment:
# 57 bits are more than needed in any case.
More information about the Pypy-commit
mailing list