[pypy-commit] pypy numpy-dtype-refactor: start to re-flesh out the dtype interface. now we get to the fun part of exposing the boxes at app level
alex_gaynor
noreply at buildbot.pypy.org
Wed Nov 9 15:48:42 CET 2011
Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: numpy-dtype-refactor
Changeset: r49019:4f8dd56c9505
Date: 2011-11-09 09:48 -0500
http://bitbucket.org/pypy/pypy/changeset/4f8dd56c9505/
Log: start to re-flesh out the dtype interface. now we get to the fun
part of exposing the boxes at app level
diff --git a/pypy/module/micronumpy/compile.py b/pypy/module/micronumpy/compile.py
--- a/pypy/module/micronumpy/compile.py
+++ b/pypy/module/micronumpy/compile.py
@@ -50,7 +50,7 @@
return False
def decode_index4(self, w_idx, size):
- return (self.int_w(w_idx), 0, 0, 1)
+ return (self.int_w(self.int(w_idx)), 0, 0, 1)
@specialize.argtype(1)
def wrap(self, obj):
@@ -87,7 +87,10 @@
raise NotImplementedError
def int(self, w_obj):
- return w_obj
+ if isinstance(w_obj, IntObject):
+ return w_obj
+ assert isinstance(w_obj, interp_boxes.W_GenericBox)
+ return IntObject(int(w_obj.value))
def is_true(self, w_obj):
assert isinstance(w_obj, BoolObject)
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
@@ -1,4 +1,7 @@
from pypy.interpreter.baseobjspace import Wrappable
+from pypy.interpreter.error import OperationError
+from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.typedef import TypeDef, interp_attrproperty
from pypy.module.micronumpy import types, signature
from pypy.rlib.objectmodel import specialize
from pypy.rlib.rarithmetic import LONG_BIT
@@ -13,11 +16,14 @@
FLOATINGLTR = "f"
class W_Dtype(Wrappable):
- def __init__(self, itemtype, num, kind):
+ def __init__(self, itemtype, num, kind, name, char, alternate_constructors=[]):
self.signature = signature.BaseSignature()
self.itemtype = itemtype
self.num = num
self.kind = kind
+ self.name = name
+ self.char = char
+ self.alternate_constructors = alternate_constructors
def malloc(self, length):
# XXX find out why test_zjit explodes with tracking of allocations
@@ -41,6 +47,40 @@
struct_ptr = rffi.ptradd(storage, i * self.itemtype.get_element_size())
self.itemtype.store(struct_ptr, 0, box)
+ def descr__new__(space, w_subtype, w_dtype):
+ cache = get_dtype_cache(space)
+
+ if space.is_w(w_dtype, space.w_None):
+ return cache.w_float64dtype
+ elif space.isinstance_w(w_dtype, w_subtype):
+ return w_dtype
+ elif space.isinstance_w(w_dtype, space.w_str):
+ name = space.str_w(w_dtype)
+ for dtype in cache.builtin_dtypes:
+ if dtype.name == name or dtype.char == name:
+ return dtype
+ else:
+ for dtype in cache.builtin_dtypes:
+ if w_dtype in dtype.alternate_constructors:
+ return dtype
+ raise OperationError(space.w_TypeError, space.wrap("data type not understood"))
+
+ def descr_str(self, space):
+ return space.wrap(self.name)
+
+ def descr_repr(self, space):
+ return space.wrap("dtype('%s')" % self.name)
+
+W_Dtype.typedef = TypeDef("dtype",
+ __module__ = "numpy",
+ __new__ = interp2app(W_Dtype.descr__new__.im_func),
+
+ __str__= interp2app(W_Dtype.descr_str),
+ __repr__ = interp2app(W_Dtype.descr_repr),
+
+ num = interp_attrproperty("num", cls=W_Dtype),
+ kind = interp_attrproperty("kind", cls=W_Dtype),
+)
class DtypeCache(object):
def __init__(self, space):
@@ -48,72 +88,104 @@
types.Bool(),
num=0,
kind=BOOLLTR,
+ name="bool",
+ char="?",
+ alternate_constructors=[space.w_bool],
)
self.w_int8dtype = W_Dtype(
types.Int8(),
num=1,
kind=SIGNEDLTR,
+ name="int8",
+ char="b",
)
self.w_uint8dtype = W_Dtype(
types.UInt8(),
num=2,
kind=UNSIGNEDLTR,
+ name="uint8",
+ char="B",
)
self.w_int16dtype = W_Dtype(
types.Int16(),
num=3,
kind=SIGNEDLTR,
+ name="int16",
+ char="h",
)
self.w_uint16dtype = W_Dtype(
types.UInt16(),
num=4,
kind=UNSIGNEDLTR,
+ name="uint16",
+ char="H",
)
self.w_int32dtype = W_Dtype(
types.Int32(),
num=5,
kind=SIGNEDLTR,
+ name="int32",
+ char="i",
)
self.w_uint32dtype = W_Dtype(
types.UInt32(),
num=6,
kind=UNSIGNEDLTR,
+ name="uint32",
+ char="I",
)
if LONG_BIT == 32:
longtype = types.Int32()
unsigned_longtype = types.UInt32()
+ name = "int32"
elif LONG_BIT == 64:
longtype = types.Int64()
unsigned_longtype = types.UInt64()
+ name = "int64"
self.w_longdtype = W_Dtype(
longtype,
num=7,
kind=SIGNEDLTR,
+ name=name,
+ char="l",
+ alternate_constructors=[space.w_int],
)
self.w_ulongdtype = W_Dtype(
unsigned_longtype,
num=8,
kind=UNSIGNEDLTR,
+ name="u" + name,
+ char="L",
)
self.w_int64dtype = W_Dtype(
types.Int64(),
num=9,
kind=SIGNEDLTR,
+ name="int64",
+ char="q",
+ alternate_constructors=[space.w_long],
)
self.w_uint64dtype = W_Dtype(
types.UInt64(),
num=10,
kind=UNSIGNEDLTR,
+ name="uint64",
+ char="Q",
)
self.w_float32dtype = W_Dtype(
types.Float32(),
num=11,
kind=FLOATINGLTR,
+ name="float32",
+ char="f",
)
self.w_float64dtype = W_Dtype(
types.Float64(),
num=12,
kind=FLOATINGLTR,
+ name="float32",
+ char="d",
+ alternate_constructors=[space.w_float],
)
self.builtin_dtypes = [
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
@@ -239,7 +239,7 @@
start, stop, step, slice_length = space.decode_index4(w_idx, self.find_size())
if step == 0:
# Single index
- return self.get_concrete().eval(start).wrap(space)
+ return self.get_concrete().eval(start)
else:
# Slice
new_sig = signature.Signature.find_sig([
@@ -540,8 +540,7 @@
return space.wrap(self.size)
def setitem_w(self, space, item, w_value):
- self.invalidated()
- self.dtype.setitem_w(space, self.storage, item, w_value)
+ return self.setitem(item, self.dtype.coerce(space, w_value))
def setitem(self, item, value):
self.invalidated()
diff --git a/pypy/module/micronumpy/test/test_compile.py b/pypy/module/micronumpy/test/test_compile.py
--- a/pypy/module/micronumpy/test/test_compile.py
+++ b/pypy/module/micronumpy/test/test_compile.py
@@ -140,7 +140,7 @@
a -> 3
"""
interp = self.run(code)
- assert interp.results[0].value.val == 15
+ assert interp.results[0].value.value == 15
def test_min(self):
interp = self.run("""
@@ -149,7 +149,7 @@
b = a + a
min(b)
""")
- assert interp.results[0].value.val == -24
+ assert interp.results[0].value.value == -24
def test_max(self):
interp = self.run("""
@@ -158,7 +158,7 @@
b = a + a
max(b)
""")
- assert interp.results[0].value.val == 256
+ assert interp.results[0].value.value == 256
def test_slice(self):
py.test.skip("in progress")
More information about the pypy-commit
mailing list