[pypy-svn] r16016 - pypy/dist/pypy/objspace/std

tismer at codespeak.net tismer at codespeak.net
Fri Aug 12 19:26:15 CEST 2005


Author: tismer
Date: Fri Aug 12 19:26:13 2005
New Revision: 16016

Modified:
   pypy/dist/pypy/objspace/std/floatobject.py
   pypy/dist/pypy/objspace/std/intobject.py
   pypy/dist/pypy/objspace/std/longobject.py
   pypy/dist/pypy/objspace/std/model.py
Log:
a little clean-up to int/long/floatobject:

Use multimethod exceptions instead of directly
calling surrogate functions.
Reason:
I would like to have the objspace implementations
create exactly the type they are made for, or raise
NonImplemented. That makes it easier to call such
functions from somewhere else. Although I also think
that calling into objspace implementations is just
a temporary thing, and we should have an extra sub-interface
for everything that gets called "under the hood" and
refrain from calling into W_Anything if it is avoidable.

I will employ this stylepretty soon, isolating the long impl
from the objspace and putting it into,say, longutil for the moment.

Btw., the way pow() needs to be redirected is slightly inefficient,
and using the NotImplementedto resolve it is slightly an abuse,
because the reason why we cannot early dispatch on it is that we are
lacking an ability to express an interface with non-negative ints.
Would it be possible to do that? Like pow__Int_Unsigned_None ???


Modified: pypy/dist/pypy/objspace/std/floatobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/floatobject.py	(original)
+++ pypy/dist/pypy/objspace/std/floatobject.py	Fri Aug 12 19:26:13 2005
@@ -1,6 +1,7 @@
 from pypy.objspace.std.objspace import *
 from pypy.interpreter import gateway
 from pypy.objspace.std.noneobject import W_NoneObject
+from pypy.objspace.std.longobject import W_LongObject, _AsDouble, _FromDouble
 from pypy.rpython.rarithmetic import ovfcheck_float_to_int, intmask
 
 ##############################################################
@@ -38,6 +39,14 @@
 def delegate_Int2Float(w_intobj):
     return W_FloatObject(w_intobj.space, float(w_intobj.intval))
 
+# long-to-float delegation
+def delegate_Long2Float(w_longobj):
+    try:
+        return W_FloatObject(w_longobj.space, _AsDouble(w_longobj))
+    except OverflowError:
+        raise OperationError(w_longobj.space.w_OverflowError,
+                             w_longobj.space.wrap("long int too large to convert to float"))
+
 
 # float__Float is supposed to do nothing, unless it has
 # a derived float object, where it should return
@@ -56,6 +65,13 @@
     else:
         return space.newint(value)
 
+def long__Float(space, w_floatobj):
+    try:
+        return _FromDouble(space, w_floatobj.floatval)
+    except OverflowError:
+        raise OperationError(space.w_OverflowError,
+                             space.wrap("cannot convert float infinity to long"))
+
 def float_w__Float(space, w_float):
     return w_float.floatval
 
@@ -308,6 +324,7 @@
 
     return W_FloatObject(space, z)
 
+
 def neg__Float(space, w_float1):
     return W_FloatObject(space, -w_float1.floatval)
 
@@ -335,3 +352,18 @@
     return space.newtuple([W_FloatObject(space, w_float.floatval)])
 
 register_all(vars())
+
+# pow delegation for negative 1st arg
+def pow_neg__Long_Long_None(space, w_int1, w_int2, thirdarg):
+    w_float1 = delegate_Long2Float(w_int1)
+    w_float2 = delegate_Long2Float(w_int2)
+    return pow__Float_Float_ANY(space, w_float1, w_float2, thirdarg)
+
+StdObjSpace.MM.pow.register(pow_neg__Long_Long_None, W_LongObject, W_LongObject, W_NoneObject, order=1)
+
+def pow_neg__Int_Int_None(space, w_int1, w_int2, thirdarg):
+    w_float1 = delegate_Int2Float(w_int1)
+    w_float2 = delegate_Int2Float(w_int2)
+    return pow__Float_Float_ANY(space, w_float1, w_float2, thirdarg)
+
+StdObjSpace.MM.pow.register(pow_neg__Int_Int_None, W_IntObject, W_IntObject, W_NoneObject, order=2)

Modified: pypy/dist/pypy/objspace/std/intobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/intobject.py	(original)
+++ pypy/dist/pypy/objspace/std/intobject.py	Fri Aug 12 19:26:13 2005
@@ -202,9 +202,6 @@
             raise OperationError(space.w_TypeError,
                              space.wrap("pow() 2nd argument "
                  "cannot be negative when 3rd argument specified"))
-        return space.pow(space.float(space.wrap(iv)),
-                         space.float(space.wrap(iw)),
-                         space.w_None)
         ## bounce it, since it always returns float
         raise FailedToImplement(space.w_ValueError,
                                 space.wrap("integer exponentiation"))
@@ -229,15 +226,6 @@
                                 space.wrap("integer exponentiation"))
     return W_IntObject(space, ix)
 
-"""
-def pow__Int_Int_Int(space, w_int1, w_int2, w_int3):
-    x = w_int1.intval
-    y = w_int2.intval
-    z = w_int3.intval
-    ret = _impl_int_int_pow(space, x, y, z)
-    return W_IntObject(space, ret)
-"""
-
 def pow__Int_Int_Int(space, w_int1, w_int2, w_int3):
     x = w_int1.intval
     y = w_int2.intval

Modified: pypy/dist/pypy/objspace/std/longobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/longobject.py	(original)
+++ pypy/dist/pypy/objspace/std/longobject.py	Fri Aug 12 19:26:13 2005
@@ -1,7 +1,6 @@
 import sys, operator
 from pypy.objspace.std.objspace import *
 from pypy.objspace.std.intobject import W_IntObject
-from pypy.objspace.std.floatobject import W_FloatObject
 from pypy.objspace.std.noneobject import W_NoneObject
 from pypy.rpython.rarithmetic import LONG_BIT, LONG_MASK, intmask, r_uint
 
@@ -93,6 +92,7 @@
         #    digits, sign = args_from_long(digits)
         w_self.digits = DigitArray(digits)
         w_self.sign = sign
+        w_self.space = space
         assert len(w_self.digits)
 
     def fromint(space, intval):
@@ -175,14 +175,6 @@
 def delegate_Int2Long(w_intobj):
     return long__Int(w_intobj.space, w_intobj)
 
-# long-to-float delegation
-def delegate_Long2Float(w_longobj):
-    try:
-        return W_FloatObject(w_longobj.space, _AsDouble(w_longobj))
-    except OverflowError:
-        raise OperationError(w_longobj.space.w_OverflowError,
-                             w_longobj.space.wrap("long int too large to convert to float"))
-
 
 # long__Long is supposed to do nothing, unless it has
 # a derived long object, where it should return
@@ -212,13 +204,6 @@
         raise OperationError(space.w_OverflowError,
                              space.wrap("long int too large to convert to float"))
 
-def long__Float(space, w_floatobj):
-    try:
-        return _FromDouble(space, w_floatobj.floatval)
-    except OverflowError:
-        raise OperationError(space.w_OverflowError,
-                             space.wrap("cannot convert float infinity to long"))
-
 def int_w__Long(space, w_value):
     try:
         return _AsLong(w_value)
@@ -374,9 +359,8 @@
             raise OperationError(space.w_TypeError, space.wrap(
                 "pow() 2nd argument "
                 "cannot be negative when 3rd argument specified"))
-        return space.pow(space.newfloat(_AsDouble(a)),
-                         space.newfloat(_AsDouble(b)),
-                         space.w_None)
+        raise FailedToImplement(space.w_ValueError, space.wrap(
+            "long pow() to negative"))
 
     if c is not None:
         # if modulus == 0:
@@ -1642,7 +1626,7 @@
     elif s[p] == '+':
         p += 1
 
-    a = W_LongObject(space, [0], 1)
+    a = W_LongObject.fromint(space, 0)
     cnt = DEC_PER_DIGIT
     tens = 1
     dig = 0
@@ -1670,3 +1654,6 @@
         digit >>= 1
         bits += 1
     return bits
+
+def _get_odd(a):
+    return a.digits[0] & 1

Modified: pypy/dist/pypy/objspace/std/model.py
==============================================================================
--- pypy/dist/pypy/objspace/std/model.py	(original)
+++ pypy/dist/pypy/objspace/std/model.py	Fri Aug 12 19:26:13 2005
@@ -99,7 +99,7 @@
             (floatobject.W_FloatObject, floatobject.delegate_Int2Float),
             ]
         self.typeorder[longobject.W_LongObject] += [
-            (floatobject.W_FloatObject, longobject.delegate_Long2Float),
+            (floatobject.W_FloatObject, floatobject.delegate_Long2Float),
             ]
         self.typeorder[stringobject.W_StringObject] += [
          (unicodeobject.W_UnicodeObject, unicodeobject.delegate_String2Unicode),



More information about the Pypy-commit mailing list