[pypy-commit] pypy kill-unary-multimethods: remove int_w, bigint_w and uint_w as multimethods
fijal
noreply at buildbot.pypy.org
Thu Sep 22 15:10:32 CEST 2011
Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: kill-unary-multimethods
Changeset: r47392:514091a77a3b
Date: 2011-09-21 10:38 +0200
http://bitbucket.org/pypy/pypy/changeset/514091a77a3b/
Log: remove int_w, bigint_w and uint_w as multimethods
diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -196,6 +196,11 @@
raise OperationError(space.w_TypeError,
typed_unwrap_error_msg(space, "unicode", self))
+ def int_w(self, space):
+ raise OperationError(space.w_TypeError,
+ typed_unwrap_error_msg(space, "integer", self))
+ uint_w = int_w
+ bigint_w = int_w
class Wrappable(W_Root):
@@ -1224,6 +1229,15 @@
def str_w(self, w_obj):
return w_obj.str_w(self)
+ def int_w(self, w_obj):
+ return w_obj.int_w(self)
+
+ def uint_w(self, w_obj):
+ return w_obj.uint_w(self)
+
+ def bigint_w(self, w_obj):
+ return w_obj.bigint_w(self)
+
def realstr_w(self, w_obj):
# Like str_w, but only works if w_obj is really of type 'str'.
if not self.is_true(self.isinstance(w_obj, self.w_str)):
diff --git a/pypy/objspace/std/boolobject.py b/pypy/objspace/std/boolobject.py
--- a/pypy/objspace/std/boolobject.py
+++ b/pypy/objspace/std/boolobject.py
@@ -1,8 +1,10 @@
+from pypy.rlib.rbigint import rbigint
+from pypy.rlib.rarithmetic import r_uint
+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.intobject import W_IntObject
-
class W_BoolObject(W_Object):
from pypy.objspace.std.booltype import bool_typedef as typedef
_immutable_fields_ = ['boolval']
@@ -19,6 +21,19 @@
def unwrap(w_self, space):
return w_self.boolval
+ int_w = unwrap
+
+ def uint_w(w_self, space):
+ intval = w_self.intval
+ if intval < 0:
+ raise OperationError(space.w_ValueError,
+ space.wrap("cannot convert negative integer to unsigned"))
+ else:
+ return r_uint(intval)
+
+ def bigint_w(w_self, space):
+ return rbigint.fromint(w_self.intval)
+
registerimplementation(W_BoolObject)
diff --git a/pypy/objspace/std/default.py b/pypy/objspace/std/default.py
--- a/pypy/objspace/std/default.py
+++ b/pypy/objspace/std/default.py
@@ -9,20 +9,8 @@
def init__ANY(space, w_obj, __args__):
pass
-def int_w__ANY(space,w_obj):
- raise OperationError(space.w_TypeError,
- typed_unwrap_error_msg(space, "integer", w_obj))
-
def float_w__ANY(space,w_obj):
raise OperationError(space.w_TypeError,
typed_unwrap_error_msg(space, "float", w_obj))
-def uint_w__ANY(space,w_obj):
- raise OperationError(space.w_TypeError,
- typed_unwrap_error_msg(space, "integer", w_obj))
-
-def bigint_w__ANY(space,w_obj):
- raise OperationError(space.w_TypeError,
- typed_unwrap_error_msg(space, "integer", w_obj))
-
register_all(vars())
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
@@ -30,7 +30,18 @@
def unwrap(w_self, space):
return int(w_self.intval)
+ int_w = unwrap
+ def uint_w(w_self, space):
+ intval = w_self.intval
+ if intval < 0:
+ raise OperationError(space.w_ValueError,
+ space.wrap("cannot convert negative integer to unsigned"))
+ else:
+ return r_uint(intval)
+
+ def bigint_w(w_self, space):
+ return rbigint.fromint(w_self.intval)
registerimplementation(W_IntObject)
@@ -39,20 +50,6 @@
# alias and then teach copy_multimethods in smallintobject.py to override
# it. See int__Int for example.
-def int_w__Int(space, w_int1):
- return int(w_int1.intval)
-
-def uint_w__Int(space, w_int1):
- intval = w_int1.intval
- if intval < 0:
- raise OperationError(space.w_ValueError,
- space.wrap("cannot convert negative integer to unsigned"))
- else:
- return r_uint(intval)
-
-def bigint_w__Int(space, w_int1):
- return rbigint.fromint(w_int1.intval)
-
def repr__Int(space, w_int1):
a = w_int1.intval
res = str(a)
diff --git a/pypy/objspace/std/longobject.py b/pypy/objspace/std/longobject.py
--- a/pypy/objspace/std/longobject.py
+++ b/pypy/objspace/std/longobject.py
@@ -45,6 +45,26 @@
fromrarith_int._annspecialcase_ = "specialize:argtype(0)"
fromrarith_int = staticmethod(fromrarith_int)
+ def int_w(w_self, space):
+ try:
+ return w_self.num.toint()
+ except OverflowError:
+ raise OperationError(space.w_OverflowError, space.wrap(
+ "long int too large to convert to int"))
+
+ def uint_w(w_self, space):
+ try:
+ return w_self.num.touint()
+ except ValueError:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "cannot convert negative integer to unsigned int"))
+ except OverflowError:
+ raise OperationError(space.w_OverflowError, space.wrap(
+ "long int too large to convert to unsigned int"))
+
+ def bigint_w(w_self, space):
+ return w_self.num
+
def __repr__(self):
return '<W_LongObject(%d)>' % self.num.tolong()
@@ -104,27 +124,6 @@
raise OperationError(space.w_OverflowError,
space.wrap("long int too large to convert to float"))
-def int_w__Long(space, w_value):
- try:
- return w_value.num.toint()
- except OverflowError:
- raise OperationError(space.w_OverflowError, space.wrap(
- "long int too large to convert to int"))
-
-
-def uint_w__Long(space, w_value):
- try:
- return w_value.num.touint()
- except ValueError:
- raise OperationError(space.w_ValueError, space.wrap(
- "cannot convert negative integer to unsigned int"))
- except OverflowError:
- raise OperationError(space.w_OverflowError, space.wrap(
- "long int too large to convert to unsigned int"))
-
-def bigint_w__Long(space, w_value):
- return w_value.num
-
def repr__Long(space, w_long):
return space.wrap(w_long.num.repr())
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
@@ -442,6 +442,7 @@
mm.dispatch_tree = merge(self.dispatch_tree, other.dispatch_tree)
return mm
+NOT_MULTIMETHODS = []
class MM:
"""StdObjSpace multimethods"""
@@ -451,10 +452,7 @@
init = StdObjSpaceMultiMethod('__init__', 1, general__args__=True)
getnewargs = StdObjSpaceMultiMethod('__getnewargs__', 1)
# special visible multimethods
- int_w = StdObjSpaceMultiMethod('int_w', 1, []) # returns an unwrapped int
float_w = StdObjSpaceMultiMethod('float_w', 1, []) # returns an unwrapped float
- uint_w = StdObjSpaceMultiMethod('uint_w', 1, []) # returns an unwrapped unsigned int (r_uint)
- bigint_w = StdObjSpaceMultiMethod('bigint_w', 1, []) # returns an unwrapped rbigint
# NOTE: when adding more sometype_w() methods, you need to write a
# stub in default.py to raise a space.w_TypeError
marshal_w = StdObjSpaceMultiMethod('marshal_w', 1, [], extra_args=['marshaller'])
@@ -462,9 +460,10 @@
# add all regular multimethods here
for _name, _symbol, _arity, _specialnames in ObjSpace.MethodTable:
- if _name not in locals():
+ if _name not in locals() or _name in NOT_MULTIMETHODS:
mm = StdObjSpaceMultiMethod(_symbol, _arity, _specialnames)
locals()[_name] = mm
del mm
pow.extras['defaults'] = (None,)
+
diff --git a/pypy/objspace/std/smalllongobject.py b/pypy/objspace/std/smalllongobject.py
--- a/pypy/objspace/std/smalllongobject.py
+++ b/pypy/objspace/std/smalllongobject.py
@@ -39,6 +39,30 @@
def __repr__(w_self):
return '<W_SmallLongObject(%d)>' % w_self.longlong
+ def int_w(w_self, space):
+ a = w_self.longlong
+ b = intmask(a)
+ if b == a:
+ return b
+ else:
+ raise OperationError(space.w_OverflowError, space.wrap(
+ "long int too large to convert to int"))
+
+ def uint_w(w_self, space):
+ a = w_self.longlong
+ if a < 0:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "cannot convert negative integer to unsigned int"))
+ b = r_uint(a)
+ if r_longlong(b) == a:
+ return b
+ else:
+ raise OperationError(space.w_OverflowError, space.wrap(
+ "long int too large to convert to unsigned int"))
+
+ def bigint_w(w_self, space):
+ return w_self.asbigint()
+
registerimplementation(W_SmallLongObject)
# ____________________________________________________________
@@ -102,30 +126,6 @@
def float__SmallLong(space, w_value):
return space.newfloat(float(w_value.longlong))
-def int_w__SmallLong(space, w_value):
- a = w_value.longlong
- b = intmask(a)
- if b == a:
- return b
- else:
- raise OperationError(space.w_OverflowError, space.wrap(
- "long int too large to convert to int"))
-
-def uint_w__SmallLong(space, w_value):
- a = w_value.longlong
- if a < 0:
- raise OperationError(space.w_ValueError, space.wrap(
- "cannot convert negative integer to unsigned int"))
- b = r_uint(a)
- if r_longlong(b) == a:
- return b
- else:
- raise OperationError(space.w_OverflowError, space.wrap(
- "long int too large to convert to unsigned int"))
-
-def bigint_w__SmallLong(space, w_value):
- return w_value.asbigint()
-
def lt__SmallLong_SmallLong(space, w_small1, w_small2):
return space.newbool(w_small1.longlong < w_small2.longlong)
def le__SmallLong_SmallLong(space, w_small1, w_small2):
More information about the pypy-commit
mailing list