Author: Manuel Jacob
Branch: remove-num-smm
Changeset: r64214:f5b1545aad25
Date: 2013-05-15 21:39 +0200
http://bitbucket.org/pypy/pypy/changeset/f5b1545aad25/
Log: Remove inttype.py, move typedef to intobject.py, update references.
diff --git a/pypy/objspace/std/booltype.py b/pypy/objspace/std/booltype.py
--- a/pypy/objspace/std/booltype.py
+++ b/pypy/objspace/std/booltype.py
@@ -1,6 +1,6 @@
from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault
from pypy.objspace.std.stdtypedef import StdTypeDef
-from pypy.objspace.std.inttype import int_typedef
+from pypy.objspace.std.intobject import int_typedef
@unwrap_spec(w_obj = WrappedDefault(False))
def descr__new__(space, w_booltype, w_obj):
diff --git a/pypy/objspace/std/bytearrayobject.py
b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -7,7 +7,7 @@
from pypy.objspace.std.bytearraytype import (
getbytevalue, makebytearraydata_w, new_bytearray)
from pypy.objspace.std.intobject import W_IntObject
-from pypy.objspace.std.inttype import wrapint
+from pypy.objspace.std.intobject import wrapint
from pypy.objspace.std.listobject import get_list_index, get_positive_index
from pypy.objspace.std.model import W_Object, registerimplementation
from pypy.objspace.std.multimethod import FailedToImplement
diff --git a/pypy/objspace/std/intobject.py b/pypy/objspace/std/intobject.py
--- a/pypy/objspace/std/intobject.py
+++ b/pypy/objspace/std/intobject.py
@@ -1,11 +1,19 @@
+from pypy.interpreter import typedef
+from pypy.interpreter.buffer import Buffer
from pypy.interpreter.error import OperationError
+from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault,\
+ interpindirect2app
from pypy.objspace.std import newformat
-from pypy.objspace.std.inttype import wrapint, W_AbstractIntObject
from pypy.objspace.std.model import registerimplementation, W_Object
from pypy.objspace.std.multimethod import FailedToImplementArgs
from pypy.objspace.std.noneobject import W_NoneObject
from pypy.objspace.std.register_all import register_all
+from pypy.objspace.std.stdtypedef import StdTypeDef
+from pypy.objspace.std.strutil import (string_to_int, string_to_bigint,
+ ParseStringError,
+ ParseStringOverflowError)
from rpython.rlib import jit
+from rpython.rlib.objectmodel import instantiate
from rpython.rlib.rarithmetic import ovfcheck, LONG_BIT, r_uint, is_valid_int
from rpython.rlib.rbigint import rbigint
@@ -16,12 +24,31 @@
something CPython does not do anymore.
"""
+class W_AbstractIntObject(W_Object):
+ __slots__ = ()
+
+ def is_w(self, space, w_other):
+ if not isinstance(w_other, W_AbstractIntObject):
+ return False
+ if self.user_overridden_class or w_other.user_overridden_class:
+ return self is w_other
+ return space.int_w(self) == space.int_w(w_other)
+
+ def immutable_unique_id(self, space):
+ if self.user_overridden_class:
+ return None
+ from pypy.objspace.std.model import IDTAG_INT as tag
+ b = space.bigint_w(self)
+ b = b.lshift(3).or_(rbigint.fromint(tag))
+ return space.newlong_from_rbigint(b)
+
+ def int(self, space):
+ raise NotImplementedError
+
class W_IntObject(W_AbstractIntObject):
__slots__ = 'intval'
_immutable_fields_ = ['intval']
- from pypy.objspace.std.inttype import int_typedef as typedef
-
def __init__(w_self, intval):
assert is_valid_int(intval)
w_self.intval = intval
@@ -57,6 +84,195 @@
a = self.intval
return wrapint(space, a)
+# ____________________________________________________________
+
+def descr_conjugate(space, w_int):
+ "Returns self, the complex conjugate of any int."
+ return space.int(w_int)
+
+def descr_bit_length(space, w_int):
+ """int.bit_length() -> int
+
+ Number of bits necessary to represent self in binary.
+ >>> bin(37)
+ '0b100101'
+ >>> (37).bit_length()
+ 6
+ """
+ val = space.int_w(w_int)
+ if val < 0:
+ val = -val
+ bits = 0
+ while val:
+ bits += 1
+ val >>= 1
+ return space.wrap(bits)
+
+
+def wrapint(space, x):
+ if space.config.objspace.std.withprebuiltint:
+ from pypy.objspace.std.intobject import W_IntObject
+ lower = space.config.objspace.std.prebuiltintfrom
+ upper = space.config.objspace.std.prebuiltintto
+ # use r_uint to perform a single comparison (this whole function
+ # is getting inlined into every caller so keeping the branching
+ # to a minimum is a good idea)
+ index = r_uint(x - lower)
+ if index >= r_uint(upper - lower):
+ w_res = instantiate(W_IntObject)
+ else:
+ w_res = W_IntObject.PREBUILT[index]
+ # obscure hack to help the CPU cache: we store 'x' even into
+ # a prebuilt integer's intval. This makes sure that the intval
+ # field is present in the cache in the common case where it is
+ # quickly reused. (we could use a prefetch hint if we had that)
+ w_res.intval = x
+ return w_res
+ else:
+ from pypy.objspace.std.intobject import W_IntObject
+ return W_IntObject(x)
+
+# ____________________________________________________________
+
+def string_to_int_or_long(space, string, base=10):
+ w_longval = None
+ value = 0
+ try:
+ value = string_to_int(string, base)
+ except ParseStringError, e:
+ raise OperationError(space.w_ValueError,
+ space.wrap(e.msg))
+ except ParseStringOverflowError, e:
+ w_longval = retry_to_w_long(space, e.parser)
+ return value, w_longval
+
+def retry_to_w_long(space, parser, base=0):
+ parser.rewind()
+ try:
+ 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 newlong
+ return newlong(space, bigint)
+
+@unwrap_spec(w_x = WrappedDefault(0))
+def descr__new__(space, w_inttype, w_x, w_base=None):
+ from pypy.objspace.std.intobject import W_IntObject
+ w_longval = None
+ w_value = w_x # 'x' is the keyword argument name in CPython
+ value = 0
+ if w_base is None:
+ ok = False
+ # check for easy cases
+ if type(w_value) is W_IntObject:
+ value = w_value.intval
+ ok = True
+ elif space.isinstance_w(w_value, space.w_str):
+ value, w_longval = string_to_int_or_long(space,
space.str_w(w_value))
+ ok = True
+ elif space.isinstance_w(w_value, space.w_unicode):
+ from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
+ string = unicode_to_decimal_w(space, w_value)
+ value, w_longval = string_to_int_or_long(space, string)
+ ok = True
+ else:
+ # If object supports the buffer interface
+ try:
+ w_buffer = space.buffer(w_value)
+ except OperationError, e:
+ if not e.match(space, space.w_TypeError):
+ raise
+ else:
+ buf = space.interp_w(Buffer, w_buffer)
+ value, w_longval = string_to_int_or_long(space, buf.as_str())
+ ok = True
+
+ if not ok:
+ # otherwise, use the __int__() or the __trunc__() methods
+ w_obj = w_value
+ if space.lookup(w_obj, '__int__') is None:
+ w_obj = space.trunc(w_obj)
+ w_obj = space.int(w_obj)
+ # 'int(x)' should return what x.__int__() returned, which should
+ # be an int or long or a subclass thereof.
+ if space.is_w(w_inttype, space.w_int):
+ return w_obj
+ # int_w is effectively what we want in this case,
+ # we cannot construct a subclass of int instance with an
+ # an overflowing long
+ try:
+ value = space.int_w(w_obj)
+ except OperationError, e:
+ if e.match(space, space.w_TypeError):
+ raise OperationError(space.w_ValueError,
+ space.wrap("value can't be converted to int"))
+ raise e
+ else:
+ base = space.int_w(w_base)
+
+ if space.isinstance_w(w_value, space.w_unicode):
+ from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
+ s = unicode_to_decimal_w(space, w_value)
+ else:
+ try:
+ s = space.str_w(w_value)
+ except OperationError, e:
+ raise OperationError(space.w_TypeError,
+ space.wrap("int() can't convert
non-string "
+ "with explicit base"))
+
+ value, w_longval = string_to_int_or_long(space, s, base)
+
+ if w_longval is not None:
+ if not space.is_w(w_inttype, space.w_int):
+ raise OperationError(space.w_OverflowError,
+ space.wrap(
+ "long int too large to convert to int"))
+ return w_longval
+ elif space.is_w(w_inttype, space.w_int):
+ # common case
+ return wrapint(space, value)
+ else:
+ w_obj = space.allocate_instance(W_IntObject, w_inttype)
+ W_IntObject.__init__(w_obj, value)
+ return w_obj
+
+def descr_get_numerator(space, w_obj):
+ return space.int(w_obj)
+
+def descr_get_denominator(space, w_obj):
+ return space.wrap(1)
+
+def descr_get_real(space, w_obj):
+ return space.int(w_obj)
+
+def descr_get_imag(space, w_obj):
+ return space.wrap(0)
+
+# ____________________________________________________________
+
+W_IntObject.typedef = StdTypeDef("int",
+ __doc__ = '''int(x[, base]) -> integer
+
+Convert a string or number to an integer, if possible. A floating point
+argument will be truncated towards zero (this does not include a string
+representation of a floating point number!) When converting a string, use
+the optional base. It is an error to supply a base when converting a
+non-string. If the argument is outside the integer range a long object
+will be returned instead.''',
+ __new__ = interp2app(descr__new__),
+ conjugate = interp2app(descr_conjugate),
+ bit_length = interp2app(descr_bit_length),
+ numerator = typedef.GetSetProperty(descr_get_numerator),
+ denominator = typedef.GetSetProperty(descr_get_denominator),
+ real = typedef.GetSetProperty(descr_get_real),
+ imag = typedef.GetSetProperty(descr_get_imag),
+ __int__ = interpindirect2app(W_AbstractIntObject.int),
+)
+int_typedef = W_IntObject.typedef
+int_typedef.registermethods(globals())
+
registerimplementation(W_IntObject)
# NB: This code is shared by smallintobject.py, and thus no other Int
diff --git a/pypy/objspace/std/inttype.py b/pypy/objspace/std/inttype.py
deleted file mode 100644
--- a/pypy/objspace/std/inttype.py
+++ /dev/null
@@ -1,223 +0,0 @@
-from pypy.interpreter import typedef
-from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault,\
- interpindirect2app
-from pypy.interpreter.error import OperationError, operationerrfmt
-from pypy.interpreter.buffer import Buffer
-from pypy.objspace.std.register_all import register_all
-from pypy.objspace.std.stdtypedef import StdTypeDef, SMM
-from pypy.objspace.std.strutil import (string_to_int, string_to_bigint,
- ParseStringError,
- ParseStringOverflowError)
-from pypy.objspace.std.model import W_Object
-from rpython.rlib.rarithmetic import r_uint
-from rpython.rlib.objectmodel import instantiate
-from rpython.rlib.rbigint import rbigint
-
-# ____________________________________________________________
-
-def descr_conjugate(space, w_int):
- "Returns self, the complex conjugate of any int."
- return space.int(w_int)
-
-def descr_bit_length(space, w_int):
- """int.bit_length() -> int
-
- Number of bits necessary to represent self in binary.
- >>> bin(37)
- '0b100101'
- >>> (37).bit_length()
- 6
- """
- val = space.int_w(w_int)
- if val < 0:
- val = -val
- bits = 0
- while val:
- bits += 1
- val >>= 1
- return space.wrap(bits)
-
-
-def wrapint(space, x):
- if space.config.objspace.std.withprebuiltint:
- from pypy.objspace.std.intobject import W_IntObject
- lower = space.config.objspace.std.prebuiltintfrom
- upper = space.config.objspace.std.prebuiltintto
- # use r_uint to perform a single comparison (this whole function
- # is getting inlined into every caller so keeping the branching
- # to a minimum is a good idea)
- index = r_uint(x - lower)
- if index >= r_uint(upper - lower):
- w_res = instantiate(W_IntObject)
- else:
- w_res = W_IntObject.PREBUILT[index]
- # obscure hack to help the CPU cache: we store 'x' even into
- # a prebuilt integer's intval. This makes sure that the intval
- # field is present in the cache in the common case where it is
- # quickly reused. (we could use a prefetch hint if we had that)
- w_res.intval = x
- return w_res
- else:
- from pypy.objspace.std.intobject import W_IntObject
- return W_IntObject(x)
-
-# ____________________________________________________________
-
-def string_to_int_or_long(space, string, base=10):
- w_longval = None
- value = 0
- try:
- value = string_to_int(string, base)
- except ParseStringError, e:
- raise OperationError(space.w_ValueError,
- space.wrap(e.msg))
- except ParseStringOverflowError, e:
- w_longval = retry_to_w_long(space, e.parser)
- return value, w_longval
-
-def retry_to_w_long(space, parser, base=0):
- parser.rewind()
- try:
- 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 newlong
- return newlong(space, bigint)
-
-@unwrap_spec(w_x = WrappedDefault(0))
-def descr__new__(space, w_inttype, w_x, w_base=None):
- from pypy.objspace.std.intobject import W_IntObject
- w_longval = None
- w_value = w_x # 'x' is the keyword argument name in CPython
- value = 0
- if w_base is None:
- ok = False
- # check for easy cases
- if type(w_value) is W_IntObject:
- value = w_value.intval
- ok = True
- elif space.isinstance_w(w_value, space.w_str):
- value, w_longval = string_to_int_or_long(space,
space.str_w(w_value))
- ok = True
- elif space.isinstance_w(w_value, space.w_unicode):
- from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
- string = unicode_to_decimal_w(space, w_value)
- value, w_longval = string_to_int_or_long(space, string)
- ok = True
- else:
- # If object supports the buffer interface
- try:
- w_buffer = space.buffer(w_value)
- except OperationError, e:
- if not e.match(space, space.w_TypeError):
- raise
- else:
- buf = space.interp_w(Buffer, w_buffer)
- value, w_longval = string_to_int_or_long(space, buf.as_str())
- ok = True
-
- if not ok:
- # otherwise, use the __int__() or the __trunc__() methods
- w_obj = w_value
- if space.lookup(w_obj, '__int__') is None:
- w_obj = space.trunc(w_obj)
- w_obj = space.int(w_obj)
- # 'int(x)' should return what x.__int__() returned, which should
- # be an int or long or a subclass thereof.
- if space.is_w(w_inttype, space.w_int):
- return w_obj
- # int_w is effectively what we want in this case,
- # we cannot construct a subclass of int instance with an
- # an overflowing long
- try:
- value = space.int_w(w_obj)
- except OperationError, e:
- if e.match(space, space.w_TypeError):
- raise OperationError(space.w_ValueError,
- space.wrap("value can't be converted to int"))
- raise e
- else:
- base = space.int_w(w_base)
-
- if space.isinstance_w(w_value, space.w_unicode):
- from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
- s = unicode_to_decimal_w(space, w_value)
- else:
- try:
- s = space.str_w(w_value)
- except OperationError, e:
- raise OperationError(space.w_TypeError,
- space.wrap("int() can't convert
non-string "
- "with explicit base"))
-
- value, w_longval = string_to_int_or_long(space, s, base)
-
- if w_longval is not None:
- if not space.is_w(w_inttype, space.w_int):
- raise OperationError(space.w_OverflowError,
- space.wrap(
- "long int too large to convert to int"))
- return w_longval
- elif space.is_w(w_inttype, space.w_int):
- # common case
- return wrapint(space, value)
- else:
- w_obj = space.allocate_instance(W_IntObject, w_inttype)
- W_IntObject.__init__(w_obj, value)
- return w_obj
-
-def descr_get_numerator(space, w_obj):
- return space.int(w_obj)
-
-def descr_get_denominator(space, w_obj):
- return space.wrap(1)
-
-def descr_get_real(space, w_obj):
- return space.int(w_obj)
-
-def descr_get_imag(space, w_obj):
- return space.wrap(0)
-
-# ____________________________________________________________
-
-class W_AbstractIntObject(W_Object):
- __slots__ = ()
-
- def is_w(self, space, w_other):
- if not isinstance(w_other, W_AbstractIntObject):
- return False
- if self.user_overridden_class or w_other.user_overridden_class:
- return self is w_other
- return space.int_w(self) == space.int_w(w_other)
-
- def immutable_unique_id(self, space):
- if self.user_overridden_class:
- return None
- from pypy.objspace.std.model import IDTAG_INT as tag
- b = space.bigint_w(self)
- b = b.lshift(3).or_(rbigint.fromint(tag))
- return space.newlong_from_rbigint(b)
-
- def int(self, space):
- raise NotImplementedError
-
-int_typedef = StdTypeDef("int",
- __doc__ = '''int(x[, base]) -> integer
-
-Convert a string or number to an integer, if possible. A floating point
-argument will be truncated towards zero (this does not include a string
-representation of a floating point number!) When converting a string, use
-the optional base. It is an error to supply a base when converting a
-non-string. If the argument is outside the integer range a long object
-will be returned instead.''',
- __new__ = interp2app(descr__new__),
- conjugate = interp2app(descr_conjugate),
- bit_length = interp2app(descr_bit_length),
- numerator = typedef.GetSetProperty(descr_get_numerator),
- denominator = typedef.GetSetProperty(descr_get_denominator),
- real = typedef.GetSetProperty(descr_get_real),
- imag = typedef.GetSetProperty(descr_get_imag),
- __int__ = interpindirect2app(W_AbstractIntObject.int),
-)
-int_typedef.registermethods(globals())
diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py
--- a/pypy/objspace/std/listobject.py
+++ b/pypy/objspace/std/listobject.py
@@ -3,7 +3,7 @@
from pypy.objspace.std.multimethod import FailedToImplement
from pypy.interpreter.error import OperationError, operationerrfmt
from pypy.interpreter.generator import GeneratorIterator
-from pypy.objspace.std.inttype import wrapint
+from pypy.objspace.std.intobject import wrapint
from pypy.objspace.std.sliceobject import W_SliceObject, normalize_simple_slice
from pypy.objspace.std import slicetype
from pypy.interpreter.gateway import WrappedDefault, unwrap_spec, applevel,\
diff --git a/pypy/objspace/std/model.py b/pypy/objspace/std/model.py
--- a/pypy/objspace/std/model.py
+++ b/pypy/objspace/std/model.py
@@ -35,7 +35,7 @@
class result:
from pypy.objspace.std.objecttype import object_typedef
from pypy.objspace.std.booltype import bool_typedef
- from pypy.objspace.std.inttype import int_typedef
+ from pypy.objspace.std.intobject import int_typedef
from pypy.objspace.std.floattype import float_typedef
from pypy.objspace.std.complextype import complex_typedef
from pypy.objspace.std.tupletype import tuple_typedef
diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py
--- a/pypy/objspace/std/objspace.py
+++ b/pypy/objspace/std/objspace.py
@@ -32,7 +32,7 @@
from pypy.objspace.std.typeobject import W_TypeObject
# types
-from pypy.objspace.std.inttype import wrapint
+from pypy.objspace.std.intobject import wrapint
from pypy.objspace.std.stringtype import wrapstr
from pypy.objspace.std.unicodetype import wrapunicode
diff --git a/pypy/objspace/std/smalltupleobject.py
b/pypy/objspace/std/smalltupleobject.py
--- a/pypy/objspace/std/smalltupleobject.py
+++ b/pypy/objspace/std/smalltupleobject.py
@@ -1,7 +1,7 @@
from pypy.interpreter.error import OperationError
from pypy.objspace.std.model import registerimplementation, W_Object
from pypy.objspace.std.register_all import register_all
-from pypy.objspace.std.inttype import wrapint
+from pypy.objspace.std.intobject import wrapint
from pypy.objspace.std.multimethod import FailedToImplement
from rpython.rlib.rarithmetic import intmask
from pypy.objspace.std.sliceobject import W_SliceObject, normalize_simple_slice
diff --git a/pypy/objspace/std/stringobject.py
b/pypy/objspace/std/stringobject.py
--- a/pypy/objspace/std/stringobject.py
+++ b/pypy/objspace/std/stringobject.py
@@ -4,7 +4,7 @@
from pypy.interpreter.error import OperationError, operationerrfmt
from pypy.objspace.std import newformat, slicetype
from pypy.objspace.std.formatting import mod_format
-from pypy.objspace.std.inttype import wrapint
+from pypy.objspace.std.intobject import wrapint
from pypy.objspace.std.model import W_Object, registerimplementation
from pypy.objspace.std.multimethod import FailedToImplement
from pypy.objspace.std.noneobject import W_NoneObject
diff --git a/pypy/objspace/std/tupleobject.py b/pypy/objspace/std/tupleobject.py
--- a/pypy/objspace/std/tupleobject.py
+++ b/pypy/objspace/std/tupleobject.py
@@ -1,7 +1,7 @@
from pypy.interpreter.error import OperationError
from pypy.objspace.std.model import registerimplementation, W_Object
from pypy.objspace.std.register_all import register_all
-from pypy.objspace.std.inttype import wrapint
+from pypy.objspace.std.intobject import wrapint
from pypy.objspace.std.multimethod import FailedToImplement
from rpython.rlib.rarithmetic import intmask
from pypy.objspace.std.sliceobject import W_SliceObject, normalize_simple_slice
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit