[pypy-svn] r15169 - in pypy/dist/pypy: interpreter module/math
hpk at codespeak.net
hpk at codespeak.net
Wed Jul 27 12:46:30 CEST 2005
Author: hpk
Date: Wed Jul 27 12:46:29 2005
New Revision: 15169
Added:
pypy/dist/pypy/module/math/_genmath.py (contents, props changed)
pypy/dist/pypy/module/math/interp_math.py (contents, props changed)
pypy/dist/pypy/module/math/readme.test
Modified:
pypy/dist/pypy/interpreter/baseobjspace.py
pypy/dist/pypy/module/math/ (props changed)
pypy/dist/pypy/module/math/__init__.py (contents, props changed)
Log:
(hpk,pedronis)
- generated/made an almost complete math module (mixedmodule)
- compliance 2.4.1 test_math.py passes
- _genmath.py is only there if we need to regenerate it
at some point (e.g. regarding exceptions)
- use 'math' now by default (even without --nofakedmodules)
- not unlikely we need to revisit the math module when it
comes to exceptions
Modified: pypy/dist/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/baseobjspace.py (original)
+++ pypy/dist/pypy/interpreter/baseobjspace.py Wed Jul 27 12:46:29 2005
@@ -151,7 +151,7 @@
builtinmodule_list.append(('parser', None))
if self.options.nofakedmodules:
builtinmodule_list.append(('posix', None))
- builtinmodule_list.append(('math', None))
+ builtinmodule_list.append(('math', None))
self._builtinmodule_list = builtinmodule_list
return self._builtinmodule_list
Modified: pypy/dist/pypy/module/math/__init__.py
==============================================================================
--- pypy/dist/pypy/module/math/__init__.py (original)
+++ pypy/dist/pypy/module/math/__init__.py Wed Jul 27 12:46:29 2005
@@ -1,9 +1,37 @@
+
# Package initialisation
from pypy.interpreter.mixedmodule import MixedModule
-
+
class Module(MixedModule):
appleveldefs = {
}
-
interpleveldefs = {
- }
+ 'e' : 'interp_math.get(space).w_e',
+ 'pi' : 'interp_math.get(space).w_pi',
+ 'pow' : 'interp_math.pow',
+ 'cosh' : 'interp_math.cosh',
+ 'ldexp' : 'interp_math.ldexp',
+ 'hypot' : 'interp_math.hypot',
+ 'tan' : 'interp_math.tan',
+ 'asin' : 'interp_math.asin',
+ 'log' : 'interp_math.log',
+ 'fabs' : 'interp_math.fabs',
+ 'floor' : 'interp_math.floor',
+ 'sqrt' : 'interp_math.sqrt',
+ 'frexp' : 'interp_math.frexp',
+ 'degrees' : 'interp_math.degrees',
+ 'log10' : 'interp_math.log10',
+ 'fmod' : 'interp_math.fmod',
+ 'atan' : 'interp_math.atan',
+ 'ceil' : 'interp_math.ceil',
+ 'sinh' : 'interp_math.sinh',
+ 'cos' : 'interp_math.cos',
+ 'tanh' : 'interp_math.tanh',
+ 'radians' : 'interp_math.radians',
+ 'sin' : 'interp_math.sin',
+ 'atan2' : 'interp_math.atan2',
+ 'modf' : 'interp_math.modf',
+ 'exp' : 'interp_math.exp',
+ 'acos' : 'interp_math.acos',
+
+}
Added: pypy/dist/pypy/module/math/_genmath.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/math/_genmath.py Wed Jul 27 12:46:29 2005
@@ -0,0 +1,62 @@
+# ONESHOT SCRIPT (probably can go away soon)
+# to generate the mixed module 'math' (see same directory)
+import py
+import math
+import re
+import sys
+rex_arg = re.compile(".*\((.*)\).*")
+
+if __name__ == '__main__':
+ print py.code.Source("""
+ import math
+ from pypy.interpreter.gateway import ObjSpace
+
+ """)
+ names = []
+ for name, func in math.__dict__.items():
+ if not callable(func):
+ continue
+ sig = func.__doc__.split('\n')[0].strip()
+ sig = sig.split('->')[0].strip()
+ m = rex_arg.match(sig)
+ assert m
+ args = m.group(1)
+ args = ", ".join(args.split(','))
+ sig = sig.replace('(', '(space,')
+ sig = ", ".join(sig.split(','))
+ argc = len(args.split(','))
+ unwrap_spec = ['ObjSpace']
+ unwrap_spec += ['float'] * argc
+ unwrap_spec = ", ".join(unwrap_spec)
+ doc = func.__doc__.replace('\n', '\n ')
+
+ print py.code.Source('''
+ def %(sig)s:
+ """%(doc)s
+ """
+ return space.wrap(math.%(name)s(%(args)s))
+ %(name)s.unwrap_spec = [%(unwrap_spec)s]
+ ''' % locals())
+ names.append(name)
+
+ print >>sys.stderr, py.code.Source("""
+ # Package initialisation
+ from pypy.interpreter.mixedmodule import MixedModule
+
+ class Module(MixedModule):
+ appleveldefs = {
+ }
+ interpleveldefs = {
+ """)
+
+ for name in names:
+ space = " " * (15-len(name))
+ print >>sys.stderr, (
+ " %(name)r%(space)s: 'interp_math.%(name)s'," % locals())
+ print >>sys.stderr, py.code.Source("""
+ }
+ """)
+
+
+
+
Added: pypy/dist/pypy/module/math/interp_math.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/math/interp_math.py Wed Jul 27 12:46:29 2005
@@ -0,0 +1,209 @@
+
+import math
+from pypy.interpreter.gateway import ObjSpace
+
+class State:
+ def __init__(self, space):
+ self.w_e = space.wrap(math.e)
+ self.w_pi = space.wrap(math.pi)
+def get(space):
+ return space.fromcache(State)
+
+def pow(space, x, y):
+ """pow(x,y)
+
+ Return x**y (x to the power of y).
+ """
+ return space.wrap(math.pow(x, y))
+pow.unwrap_spec = [ObjSpace, float, float]
+
+def cosh(space, x):
+ """cosh(x)
+
+ Return the hyperbolic cosine of x.
+ """
+ return space.wrap(math.cosh(x))
+cosh.unwrap_spec = [ObjSpace, float]
+
+def ldexp(space, x, i):
+ """ldexp(x, i) -> x * (2**i)
+ """
+ return space.wrap(math.ldexp(x, i))
+ldexp.unwrap_spec = [ObjSpace, float, int]
+
+def hypot(space, x, y):
+ """hypot(x,y)
+
+ Return the Euclidean distance, sqrt(x*x + y*y).
+ """
+ return space.wrap(math.hypot(x, y))
+hypot.unwrap_spec = [ObjSpace, float, float]
+
+def tan(space, x):
+ """tan(x)
+
+ Return the tangent of x (measured in radians).
+ """
+ return space.wrap(math.tan(x))
+tan.unwrap_spec = [ObjSpace, float]
+
+def asin(space, x):
+ """asin(x)
+
+ Return the arc sine (measured in radians) of x.
+ """
+ return space.wrap(math.asin(x))
+asin.unwrap_spec = [ObjSpace, float]
+
+def log(space, x, base=math.e):
+ """log(x[, base]) -> the logarithm of x to the given base.
+ If the base not specified, returns the natural logarithm (base e) of x.
+ """
+ return space.wrap(math.log(x, base))
+log.unwrap_spec = [ObjSpace, float, float]
+
+def fabs(space, x):
+ """fabs(x)
+
+ Return the absolute value of the float x.
+ """
+ return space.wrap(math.fabs(x))
+fabs.unwrap_spec = [ObjSpace, float]
+
+def floor(space, x):
+ """floor(x)
+
+ Return the floor of x as a float.
+ This is the largest integral value <= x.
+ """
+ return space.wrap(math.floor(x))
+floor.unwrap_spec = [ObjSpace, float]
+
+def sqrt(space, x):
+ """sqrt(x)
+
+ Return the square root of x.
+ """
+ return space.wrap(math.sqrt(x))
+sqrt.unwrap_spec = [ObjSpace, float]
+
+def frexp(space, x):
+ """frexp(x)
+
+ Return the mantissa and exponent of x, as pair (m, e).
+ m is a float and e is an int, such that x = m * 2.**e.
+ If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.
+ """
+ mant, expo = math.frexp(x)
+ return space.newtuple([space.wrap(mant), space.wrap(expo)])
+frexp.unwrap_spec = [ObjSpace, float]
+
+def degrees(space, x):
+ """degrees(x) -> converts angle x from radians to degrees
+ """
+ return space.wrap(math.degrees(x))
+degrees.unwrap_spec = [ObjSpace, float]
+
+def log10(space, x):
+ """log10(x) -> the base 10 logarithm of x.
+ """
+ return space.wrap(math.log10(x))
+log10.unwrap_spec = [ObjSpace, float]
+
+def fmod(space, x, y):
+ """fmod(x,y)
+
+ Return fmod(x, y), according to platform C. x % y may differ.
+ """
+ return space.wrap(math.fmod(x, y))
+fmod.unwrap_spec = [ObjSpace, float, float]
+
+def atan(space, x):
+ """atan(x)
+
+ Return the arc tangent (measured in radians) of x.
+ """
+ return space.wrap(math.atan(x))
+atan.unwrap_spec = [ObjSpace, float]
+
+def ceil(space, x):
+ """ceil(x)
+
+ Return the ceiling of x as a float.
+ This is the smallest integral value >= x.
+ """
+ return space.wrap(math.ceil(x))
+ceil.unwrap_spec = [ObjSpace, float]
+
+def sinh(space, x):
+ """sinh(x)
+
+ Return the hyperbolic sine of x.
+ """
+ return space.wrap(math.sinh(x))
+sinh.unwrap_spec = [ObjSpace, float]
+
+def cos(space, x):
+ """cos(x)
+
+ Return the cosine of x (measured in radians).
+ """
+ return space.wrap(math.cos(x))
+cos.unwrap_spec = [ObjSpace, float]
+
+def tanh(space, x):
+ """tanh(x)
+
+ Return the hyperbolic tangent of x.
+ """
+ return space.wrap(math.tanh(x))
+tanh.unwrap_spec = [ObjSpace, float]
+
+def radians(space, x):
+ """radians(x) -> converts angle x from degrees to radians
+ """
+ return space.wrap(math.radians(x))
+radians.unwrap_spec = [ObjSpace, float]
+
+def sin(space, x):
+ """sin(x)
+
+ Return the sine of x (measured in radians).
+ """
+ return space.wrap(math.sin(x))
+sin.unwrap_spec = [ObjSpace, float]
+
+def atan2(space, y, x):
+ """atan2(y, x)
+
+ Return the arc tangent (measured in radians) of y/x.
+ Unlike atan(y/x), the signs of both x and y are considered.
+ """
+ return space.wrap(math.atan2(y, x))
+atan2.unwrap_spec = [ObjSpace, float, float]
+
+def modf(space, x):
+ """modf(x)
+
+ Return the fractional and integer parts of x. Both results carry the sign
+ of x. The integer part is returned as a real.
+ """
+ frac, intpart = math.modf(x)
+ return space.newtuple([space.wrap(frac), space.wrap(intpart)])
+modf.unwrap_spec = [ObjSpace, float]
+
+def exp(space, x):
+ """exp(x)
+
+ Return e raised to the power of x.
+ """
+ return space.wrap(math.exp(x))
+exp.unwrap_spec = [ObjSpace, float]
+
+def acos(space, x):
+ """acos(x)
+
+ Return the arc cosine (measured in radians) of x.
+ """
+ return space.wrap(math.acos(x))
+acos.unwrap_spec = [ObjSpace, float]
Added: pypy/dist/pypy/module/math/readme.test
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/math/readme.test Wed Jul 27 12:46:29 2005
@@ -0,0 +1,6 @@
+# please run the compliance test like e.g.
+
+# py.test lib-python/2.4.1/test/test_math.py
+#
+# if you change the math module
+#
More information about the Pypy-commit
mailing list