[pypy-commit] pypy numpy-dtype-alt: refactor signatures, causing a translation issue I don't understand.
alex_gaynor
noreply at buildbot.pypy.org
Sat Aug 20 20:31:18 CEST 2011
Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: numpy-dtype-alt
Changeset: r46670:16e3a4db6a1d
Date: 2011-08-20 13:36 -0500
http://bitbucket.org/pypy/pypy/changeset/16e3a4db6a1d/
Log: refactor signatures, causing a translation issue I don't understand.
diff --git a/pypy/module/micronumpy/interp_dtype.py b/pypy/module/micronumpy/interp_dtype.py
--- a/pypy/module/micronumpy/interp_dtype.py
+++ b/pypy/module/micronumpy/interp_dtype.py
@@ -4,6 +4,7 @@
from pypy.interpreter.baseobjspace import Wrappable
from pypy.interpreter.gateway import interp2app
from pypy.interpreter.typedef import TypeDef, interp_attrproperty
+from pypy.module.micronumpy import signature
from pypy.objspace.std.floatobject import float2string
from pypy.rlib import rfloat
from pypy.rlib.rarithmetic import widen
@@ -60,6 +61,8 @@
TP = lltype.Ptr(lltype.Array(T, hints={'nolength': True}))
class W_LowLevelDtype(W_Dtype):
+ signature = signature.BaseSignature()
+
def erase(self, storage):
return rffi.cast(VOID_TP, storage)
diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -4,8 +4,7 @@
from pypy.interpreter.error import OperationError, operationerrfmt
from pypy.interpreter.gateway import interp2app, unwrap_spec, NoneNotWrapped
from pypy.interpreter.typedef import TypeDef, GetSetProperty
-from pypy.module.micronumpy import interp_ufuncs, interp_dtype
-from pypy.module.micronumpy.interp_support import Signature
+from pypy.module.micronumpy import interp_ufuncs, interp_dtype, signature
from pypy.rlib import jit
from pypy.rlib.objectmodel import specialize
from pypy.rlib.rfloat import DTSF_STR_PRECISION
@@ -21,8 +20,6 @@
slice_driver2 = jit.JitDriver(greens=['signature'], reds=['i', 'j', 'step', 'stop', 'source', 'dest'])
class BaseArray(Wrappable):
- signature = None
-
def __init__(self):
self.invalidates = []
@@ -35,6 +32,9 @@
arr.force_if_needed()
del self.invalidates[:]
+ def add_invalidates(self, other):
+ self.invalidates.append(other)
+
def descr__new__(space, w_subtype, w_size_or_iterable, w_dtype=NoneNotWrapped):
if w_dtype is None:
w_dtype = space.w_float
@@ -243,7 +243,10 @@
return self.get_concrete().eval(start).wrap(space)
else:
# Slice
- res = SingleDimSlice(start, stop, step, slice_length, self, self.signature.transition(SingleDimSlice.static_signature))
+ new_sig = signature.Signature.find_sig([
+ SingleDimSlice.signature, self.signature
+ ])
+ res = SingleDimSlice(start, stop, step, slice_length, self, new_sig)
return space.wrap(res)
def descr_setitem(self, space, w_idx, w_value):
@@ -318,7 +321,7 @@
"""
Intermediate class representing a float literal.
"""
- signature = Signature()
+ signature = signature.BaseSignature()
def __init__(self, value):
BaseArray.__init__(self)
@@ -386,9 +389,8 @@
class Call1(VirtualArray):
- def __init__(self, function, values, signature):
+ def __init__(self, signature, values):
VirtualArray.__init__(self, signature)
- self.function = function
self.values = values
def _del_sources(self):
@@ -401,15 +403,16 @@
return self.values.find_dtype()
def _eval(self, i):
- return self.function(self.find_dtype(), self.values.eval(i))
+ call_sig = self.signature.components[0]
+ assert isinstance(call_sig, signature.Call1)
+ return call_sig.func(self.find_dtype(), self.values.eval(i))
class Call2(VirtualArray):
"""
Intermediate class for performing binary operations.
"""
- def __init__(self, space, function, left, right, signature):
+ def __init__(self, space, signature, left, right):
VirtualArray.__init__(self, signature)
- self.function = function
self.left = left
self.right = right
@@ -448,7 +451,9 @@
dtype = self.find_dtype()
lhs, rhs = self.left.eval(i), self.right.eval(i)
lhs, rhs = lhs.convert_to(dtype), rhs.convert_to(dtype)
- return self.function(dtype, lhs, rhs)
+ call_sig = self.signature.components[0]
+ assert isinstance(call_sig, signature.Call2)
+ return call_sig.func(dtype, lhs, rhs)
def _find_dtype(self):
if self.res_dtype is not None:
@@ -487,7 +492,7 @@
raise NotImplementedError
class SingleDimSlice(ViewArray):
- static_signature = Signature()
+ signature = signature.BaseSignature()
def __init__(self, start, stop, step, slice_length, parent, signature):
ViewArray.__init__(self, parent, signature)
@@ -527,13 +532,12 @@
class SingleDimArray(BaseArray):
- signature = Signature()
-
def __init__(self, size, dtype):
BaseArray.__init__(self)
self.size = size
self.dtype = dtype
self.storage = dtype.malloc(size)
+ self.signature = dtype.signature
def get_concrete(self):
return self
diff --git a/pypy/module/micronumpy/interp_support.py b/pypy/module/micronumpy/interp_support.py
--- a/pypy/module/micronumpy/interp_support.py
+++ b/pypy/module/micronumpy/interp_support.py
@@ -31,14 +31,4 @@
start += FLOAT_SIZE
end += FLOAT_SIZE
- return space.wrap(a)
-
-class Signature(object):
- def __init__(self):
- self.transitions = {}
-
- def transition(self, target):
- if target in self.transitions:
- return self.transitions[target]
- self.transitions[target] = new = Signature()
- return new
\ No newline at end of file
+ return space.wrap(a)
\ No newline at end of file
diff --git a/pypy/module/micronumpy/interp_ufuncs.py b/pypy/module/micronumpy/interp_ufuncs.py
--- a/pypy/module/micronumpy/interp_ufuncs.py
+++ b/pypy/module/micronumpy/interp_ufuncs.py
@@ -1,7 +1,6 @@
import math
-from pypy.module.micronumpy import interp_dtype
-from pypy.module.micronumpy.interp_support import Signature
+from pypy.module.micronumpy import interp_dtype, signature
from pypy.rlib import rfloat
from pypy.tool.sourcetools import func_with_new_name
@@ -9,7 +8,7 @@
def ufunc(func=None, promote_to_float=False):
if func is None:
return lambda func: ufunc(func, promote_to_float)
- signature = Signature()
+ call_sig = signature.Call1(func)
def impl(space, w_obj):
from pypy.module.micronumpy.interp_numarray import (Call1,
convert_to_array, Scalar)
@@ -17,18 +16,19 @@
w_obj = convert_to_array(space, w_obj)
if isinstance(w_obj, Scalar):
res_dtype = space.fromcache(interp_dtype.W_Float64Dtype)
- return func(res_dtype, w_obj).wrap(space)
+ return func(res_dtype, w_obj.value).wrap(space)
- w_res = Call1(func, w_obj, w_obj.signature.transition(signature))
- w_obj.invalidates.append(w_res)
+ new_sig = signature.Signature.find_sig([call_sig, w_obj.signature])
+ w_res = Call1(new_sig, w_obj)
+ w_obj.add_invalidates(w_res)
return w_res
return func_with_new_name(impl, "%s_dispatcher" % func.__name__)
def ufunc2(func=None, promote_to_float=False):
if func is None:
- return lambda func: ufunc2(func)
+ return lambda func: ufunc2(func, promote_to_float)
- signature = Signature()
+ call_sig = signature.Call2(func)
def impl(space, w_lhs, w_rhs):
from pypy.module.micronumpy.interp_numarray import (Call2,
convert_to_array, Scalar)
@@ -37,12 +37,12 @@
w_rhs = convert_to_array(space, w_rhs)
if isinstance(w_lhs, Scalar) and isinstance(w_rhs, Scalar):
res_dtype = space.fromcache(interp_dtype.W_Float64Dtype)
- return func(res_dtype, w_lhs, w_rhs).wrap(space)
+ return func(res_dtype, w_lhs.value, w_rhs.value).wrap(space)
- new_sig = w_lhs.signature.transition(signature).transition(w_rhs.signature)
- w_res = Call2(space, func, w_lhs, w_rhs, new_sig)
- w_lhs.invalidates.append(w_res)
- w_rhs.invalidates.append(w_res)
+ new_sig = signature.Signature.find_sig([call_sig, w_lhs.signature, w_rhs.signature])
+ w_res = Call2(space, new_sig, w_lhs, w_rhs)
+ w_lhs.add_invalidates(w_res)
+ w_rhs.add_invalidates(w_res)
return w_res
return func_with_new_name(impl, "%s_dispatcher" % func.__name__)
@@ -82,8 +82,7 @@
@ufunc2(**kwargs)
def impl(res_dtype, lvalue, rvalue):
return getattr(res_dtype, op_name)(lvalue, rvalue)
- impl.__name__ = ufunc_name
- return impl
+ return func_with_new_name(impl, ufunc_name)
for ufunc_def in [
("add", "add", 2),
diff --git a/pypy/module/micronumpy/test/test_zjit.py b/pypy/module/micronumpy/test/test_zjit.py
--- a/pypy/module/micronumpy/test/test_zjit.py
+++ b/pypy/module/micronumpy/test/test_zjit.py
@@ -1,8 +1,8 @@
from pypy.jit.metainterp.test.support import LLJitMixin
from pypy.module.micronumpy.compile import numpy_compile
from pypy.module.micronumpy.interp_dtype import W_Float64Dtype
-from pypy.module.micronumpy.interp_numarray import (SingleDimArray, Signature,
- Scalar, Call2, SingleDimSlice, Call1)
+from pypy.module.micronumpy.interp_numarray import (SingleDimArray, Scalar,
+ Call2, SingleDimSlice, Call1)
from pypy.module.micronumpy.interp_ufuncs import negative, add
from pypy.rlib.nonconst import NonConstant
from pypy.rlib.objectmodel import specialize
More information about the pypy-commit
mailing list