[pypy-svn] r16433 - in pypy/dist/pypy: module/math objspace/std

tismer at codespeak.net tismer at codespeak.net
Wed Aug 24 21:12:00 CEST 2005


Author: tismer
Date: Wed Aug 24 21:11:59 2005
New Revision: 16433

Modified:
   pypy/dist/pypy/module/math/interp_math.py
   pypy/dist/pypy/objspace/std/floatobject.py
   pypy/dist/pypy/objspace/std/longobject.py
   pypy/dist/pypy/objspace/std/objspace.py
Log:
modified math.log to use a multimethod,
because dispatching on an object's type
is very messy if we don't use the space
as *the* general interface.

Modified: pypy/dist/pypy/module/math/interp_math.py
==============================================================================
--- pypy/dist/pypy/module/math/interp_math.py	(original)
+++ pypy/dist/pypy/module/math/interp_math.py	Wed Aug 24 21:11:59 2005
@@ -2,7 +2,6 @@
 import math
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.gateway import ObjSpace, W_Root, NoneNotWrapped
-from pypy.objspace.std import longobject
 
 class State: 
     def __init__(self, space): 
@@ -137,41 +136,15 @@
     return space.wrap(x / degToRad)
 degrees.unwrap_spec = [ObjSpace, float]
 
-def _log_float(space, x, base):
-    if base == 10.0:
-        return math1(space, math.log10, x)
-    num = math1_w(space, math.log, x) 
-    if base == 0.0:
-        return space.wrap(num)
-    else:
-        den = math1_w(space, math.log, base)
-        return space.wrap(num / den)
-
 def _log_any(space, w_x, base):
     try:
-        x = space.float_w(w_x)
-    except OperationError:
-        pass
-    else:
-        return _log_float(space, x, base)
-    try:
-        w_x = space.long(w_x)
-    except OperationError:
-        raise OperationError(space.w_TypeError, space.wrap(
-            'a float is required')) # yes, this message is as bad as CPython's
-    try:
-        if base == 10.0:
-            return space.wrap(longobject._loghelper(math.log10, w_x))
-        ret = longobject._loghelper(math.log, w_x)
-        if base != 0.0:
-            ret /= math.log(base)
-        return space.wrap(ret)
+        return space.log(w_x, base)
     except OverflowError:
         raise OperationError(space.w_OverflowError,
-                             space.wrap("math range error"))
+                             space.wrap('math range error'))
     except ValueError:
         raise OperationError(space.w_ValueError,
-                             space.wrap("math domain error"))
+                             space.wrap('math domain error'))
 
 def log(space, w_x, w_base=NoneNotWrapped):
     """log(x[, base]) -> the logarithm of x to the given base.
@@ -180,11 +153,7 @@
     if w_base is None:
         base = 0.0
     else:
-        try:
-            base = space.float_w(w_base)
-        except OperationError:
-            raise OperationError(space.w_TypeError, space.wrap(
-                'a float is required'))
+        base = space.float_w(w_base)
         if base <= 0.0:
             # just for raising the proper errors
             return math1(space, math.log, base)

Modified: pypy/dist/pypy/objspace/std/floatobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/floatobject.py	(original)
+++ pypy/dist/pypy/objspace/std/floatobject.py	Wed Aug 24 21:11:59 2005
@@ -413,6 +413,19 @@
 def getnewargs__Float(space, w_float):
     return space.newtuple([W_FloatObject(space, w_float.floatval)])
 
+def log__Float(space, w_float, base):
+    # base is supposed to be positive or 0.0, which means we use e
+    x = space.float_w(w_float)
+    if base == 10.0:
+        return space.wrap(math.log10(x))
+    num = math.log(x) 
+    if base == 0.0:
+        return space.wrap(num)
+    else:
+        den = math.log(base)
+        return space.wrap(num / den)
+
+
 register_all(vars())
 
 # pow delegation for negative 2nd arg

Modified: pypy/dist/pypy/objspace/std/longobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/longobject.py	(original)
+++ pypy/dist/pypy/objspace/std/longobject.py	Wed Aug 24 21:11:59 2005
@@ -565,6 +565,15 @@
 def getnewargs__Long(space, w_long1):
     return space.newtuple([W_LongObject(space, w_long1.digits, w_long1.sign)])
 
+def log__Long(space, w_long, base):
+    # base is supposed to be positive or 0.0, which means we use e
+    if base == 10.0:
+        return space.wrap(_loghelper(math.log10, w_long))
+    ret = _loghelper(math.log, w_long)
+    if base != 0.0:
+        ret /= math.log(base)
+    return space.wrap(ret)
+
 
 register_all(vars())
 
@@ -605,6 +614,7 @@
 StdObjSpace.MM.pow.register(pow_ovr__Int_Int_None, W_IntObject, W_IntObject, W_NoneObject, order=1)
 StdObjSpace.MM.pow.register(pow_ovr__Int_Int_Long, W_IntObject, W_IntObject, W_LongObject, order=1)
 
+#_________________________________________________________________
 
 # Helper Functions
 def args_from_long(l): #YYYYYY

Modified: pypy/dist/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/std/objspace.py	(original)
+++ pypy/dist/pypy/objspace/std/objspace.py	Wed Aug 24 21:11:59 2005
@@ -437,6 +437,7 @@
         float_w = MultiMethod('float_w', 1, [])   # returns an unwrapped float
         uint_w  = MultiMethod('uint_w', 1, [])    # returns an unwrapped unsigned int (r_uint)
         marshal_w = MultiMethod('marshal_w', 1, [], extra_args=['marshaller'])
+        log     = MultiMethod('log', 1, [], extra_args=['base'])
 
         # add all regular multimethods here
         for _name, _symbol, _arity, _specialnames in ObjSpace.MethodTable:



More information about the Pypy-commit mailing list