Author: Brian Kearns <[email protected]>
Branch: numpy-newbyteorder
Changeset: r68017:8c6fe040d6c7
Date: 2013-11-14 01:28 -0500
http://bitbucket.org/pypy/pypy/changeset/8c6fe040d6c7/
Log: move size from types to dtype
diff --git a/pypy/module/micronumpy/arrayimpl/concrete.py
b/pypy/module/micronumpy/arrayimpl/concrete.py
--- a/pypy/module/micronumpy/arrayimpl/concrete.py
+++ b/pypy/module/micronumpy/arrayimpl/concrete.py
@@ -52,7 +52,7 @@
loop.setslice(space, shape, self, impl)
def get_size(self):
- return self.size // self.dtype.itemtype.get_element_size()
+ return self.size // self.dtype.get_size()
def get_storage_size(self):
return self.size
@@ -399,7 +399,7 @@
self.storage = parent.storage
self.order = parent.order
self.dtype = dtype
- self.size = support.product(shape) *
self.dtype.itemtype.get_element_size()
+ self.size = support.product(shape) * self.dtype.get_size()
self.start = start
self.orig_arr = orig_arr
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
@@ -38,7 +38,7 @@
_immutable_fields_ = ["itemtype?", "num", "kind", "name?", "char",
"w_box_type", "byteorder", "float_type"]
def __init__(self, itemtype, num, kind, name, char, w_box_type,
byteorder=NPY_NATIVE,
- alternate_constructors=[], aliases=[], float_type=None,
+ size=1, alternate_constructors=[], aliases=[],
float_type=None,
fields=None, fieldnames=None, shape=[], subdtype=None):
self.itemtype = itemtype
self.num = num
@@ -47,6 +47,7 @@
self.char = char
self.w_box_type = w_box_type
self.byteorder = byteorder
+ self.size = size
self.alternate_constructors = alternate_constructors
self.aliases = aliases
self.float_type = float_type
@@ -123,7 +124,7 @@
return self.byteorder in (NPY_NATIVE, NPY_NATBYTE)
def get_size(self):
- return self.itemtype.get_element_size()
+ return self.size * self.itemtype.get_element_size()
def get_name(self):
if self.char == 'S':
@@ -137,7 +138,7 @@
return space.wrap("dtype('%s')" % self.get_name())
def descr_get_itemsize(self, space):
- return space.wrap(self.itemtype.get_element_size())
+ return space.wrap(self.get_size())
def descr_get_alignment(self, space):
return space.wrap(self.itemtype.alignment)
@@ -209,10 +210,11 @@
self.fields[space.str_w(key)] = offset, dtype
ofs_and_items.append((offset, dtype.itemtype))
- size += dtype.itemtype.get_element_size()
+ size += dtype.get_size()
- self.itemtype = types.RecordType(ofs_and_items, size)
- self.name = "void" + str(8 * self.itemtype.get_element_size())
+ self.itemtype = types.RecordType(ofs_and_items)
+ self.size = size
+ self.name = "void" + str(8 * self.get_size())
def descr_get_names(self, space):
if self.fieldnames is None:
@@ -264,7 +266,7 @@
w_class = space.type(self)
kind = self.kind
- elemsize = self.itemtype.get_element_size()
+ elemsize = self.get_size()
builder_args = space.newtuple([space.wrap("%s%d" % (kind, elemsize)),
space.wrap(0), space.wrap(1)])
version = space.wrap(3)
@@ -319,7 +321,8 @@
elif newendian != NPY_IGNORE:
endian = newendian
itemtype = self.itemtype.__class__(endian in (NPY_NATIVE, NPY_NATBYTE))
- return W_Dtype(itemtype, self.num, self.kind, self.name, self.char,
self.w_box_type, endian)
+ return W_Dtype(itemtype, self.num, self.kind, self.name, self.char,
+ self.w_box_type, endian, size=self.size)
def dtype_from_list(space, w_lst):
lst_w = space.listview(w_lst)
@@ -343,12 +346,13 @@
assert isinstance(subdtype, W_Dtype)
fields[fldname] = (offset, subdtype)
ofs_and_items.append((offset, subdtype.itemtype))
- offset += subdtype.itemtype.get_element_size() * size
+ offset += subdtype.get_size() * size
fieldnames.append(fldname)
- itemtype = types.RecordType(ofs_and_items, offset)
- return W_Dtype(itemtype, NPY_VOID, NPY_VOIDLTR, "void" + str(8 *
itemtype.get_element_size()),
- NPY_VOIDLTR, space.gettypefor(interp_boxes.W_VoidBox),
fields=fields,
- fieldnames=fieldnames)
+ itemtype = types.RecordType(ofs_and_items)
+ return W_Dtype(itemtype, NPY_VOID, NPY_VOIDLTR,
+ "void" + str(8 * offset * itemtype.get_element_size()),
+ NPY_VOIDLTR, space.gettypefor(interp_boxes.W_VoidBox),
+ fields=fields, fieldnames=fieldnames, size=offset)
def dtype_from_dict(space, w_dict):
raise OperationError(space.w_NotImplementedError, space.wrap(
@@ -362,7 +366,8 @@
# w_align and w_copy are necessary for pickling
cache = get_dtype_cache(space)
- if w_shape is not None and (space.isinstance_w(w_shape, space.w_int) or
space.len_w(w_shape) > 0):
+ if w_shape is not None and (space.isinstance_w(w_shape, space.w_int) or
+ space.len_w(w_shape) > 0):
subdtype = descr__new__(space, w_subtype, w_dtype, w_align, w_copy)
assert isinstance(subdtype, W_Dtype)
size = 1
@@ -373,8 +378,11 @@
dim = space.int_w(w_dim)
shape.append(dim)
size *= dim
- return W_Dtype(types.VoidType(subdtype.itemtype.get_element_size() *
size), NPY_VOID, NPY_VOIDLTR, "void" + str(8 *
subdtype.itemtype.get_element_size() * size),
- NPY_VOIDLTR, space.gettypefor(interp_boxes.W_VoidBox),
shape=shape, subdtype=subdtype)
+ return W_Dtype(types.VoidType(), NPY_VOID, NPY_VOIDLTR,
+ "void" + str(8 * subdtype.get_size() * size),
+ NPY_VOIDLTR, space.gettypefor(interp_boxes.W_VoidBox),
+ shape=shape, subdtype=subdtype,
+ size=subdtype.get_size() * size)
if space.is_none(w_dtype):
return cache.w_float64dtype
@@ -464,17 +472,17 @@
size = 1
if char == NPY_STRINGLTR:
- itemtype = types.StringType(size)
+ itemtype = types.StringType()
basename = 'string'
num = NPY_STRING
w_box_type = space.gettypefor(interp_boxes.W_StringBox)
elif char == NPY_VOIDLTR:
- itemtype = types.VoidType(size)
+ itemtype = types.VoidType()
basename = 'void'
num = NPY_VOID
w_box_type = space.gettypefor(interp_boxes.W_VoidBox)
elif char == NPY_UNICODELTR:
- itemtype = types.UnicodeType(size)
+ itemtype = types.UnicodeType()
basename = 'unicode'
num = NPY_UNICODE
w_box_type = space.gettypefor(interp_boxes.W_UnicodeBox)
@@ -482,27 +490,29 @@
assert False
return W_Dtype(itemtype, num, char,
- basename + str(8 * itemtype.get_element_size()),
- char, w_box_type)
+ basename + str(8 * size * itemtype.get_element_size()),
+ char, w_box_type, size=size)
def new_string_dtype(space, size):
- itemtype = types.StringType(size)
+ itemtype = types.StringType()
return W_Dtype(
itemtype,
+ size=size,
num=NPY_STRING,
kind=NPY_STRINGLTR,
- name='string' + str(8 * itemtype.get_element_size()),
+ name='string' + str(8 * size * itemtype.get_element_size()),
char=NPY_STRINGLTR,
w_box_type = space.gettypefor(interp_boxes.W_StringBox),
)
def new_unicode_dtype(space, size):
- itemtype = types.UnicodeType(size)
+ itemtype = types.UnicodeType()
return W_Dtype(
itemtype,
+ size=size,
num=NPY_UNICODE,
kind=NPY_UNICODELTR,
- name='unicode' + str(8 * itemtype.get_element_size()),
+ name='unicode' + str(8 * size * itemtype.get_element_size()),
char=NPY_UNICODELTR,
w_box_type = space.gettypefor(interp_boxes.W_UnicodeBox),
)
@@ -677,7 +687,8 @@
float_type = self.w_floatlongdtype,
)
self.w_stringdtype = W_Dtype(
- types.StringType(0),
+ types.StringType(),
+ size=0,
num=NPY_STRING,
kind=NPY_STRINGLTR,
name='string',
@@ -687,7 +698,8 @@
aliases=["str"],
)
self.w_unicodedtype = W_Dtype(
- types.UnicodeType(0),
+ types.UnicodeType(),
+ size=0,
num=NPY_UNICODE,
kind=NPY_UNICODELTR,
name='unicode',
@@ -696,7 +708,8 @@
alternate_constructors=[space.w_unicode],
)
self.w_voiddtype = W_Dtype(
- types.VoidType(0),
+ types.VoidType(),
+ size=0,
num=NPY_VOID,
kind=NPY_VOIDLTR,
name='void',
@@ -764,7 +777,7 @@
self.w_intpdtype, self.w_uintpdtype,
]
self.float_dtypes_by_num_bytes = sorted(
- (dtype.itemtype.get_element_size(), dtype)
+ (dtype.get_size(), dtype)
for dtype in float_dtypes
)
self.dtypes_by_num = {}
@@ -774,7 +787,7 @@
for dtype in reversed(self.builtin_dtypes):
self.dtypes_by_num[dtype.num] = dtype
self.dtypes_by_name[dtype.name] = dtype
- can_name = dtype.kind + str(dtype.itemtype.get_element_size())
+ can_name = dtype.kind + str(dtype.get_size())
self.dtypes_by_name[can_name] = dtype
self.dtypes_by_name[NPY_NATBYTE + can_name] = dtype
self.dtypes_by_name[NPY_NATIVE + can_name] = dtype
@@ -844,7 +857,7 @@
for k, v in typeinfo_partial.iteritems():
space.setitem(w_typeinfo, space.wrap(k), space.gettypefor(v))
for k, dtype in typeinfo_full.iteritems():
- itemsize = dtype.itemtype.get_element_size()
+ itemsize = dtype.get_size()
items_w = [space.wrap(dtype.char),
space.wrap(dtype.num),
space.wrap(itemsize * 8), # in case of changing
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
@@ -85,10 +85,10 @@
return space.wrap(len(self.get_shape()))
def descr_get_itemsize(self, space):
- return space.wrap(self.get_dtype().itemtype.get_element_size())
+ return space.wrap(self.get_dtype().get_size())
def descr_get_nbytes(self, space):
- return space.wrap(self.get_size() *
self.get_dtype().itemtype.get_element_size())
+ return space.wrap(self.get_size() * self.get_dtype().get_size())
def descr_fill(self, space, w_value):
self.fill(self.get_dtype().coerce(space, w_value))
@@ -1340,7 +1340,7 @@
# not an array or incorrect dtype
shape, elems_w = find_shape_and_elems(space, w_object, dtype)
if dtype is None or (
- dtype.is_str_or_unicode() and dtype.itemtype.get_size() < 1):
+ dtype.is_str_or_unicode() and dtype.get_size() < 1):
for w_elem in elems_w:
dtype = interp_ufuncs.find_dtype_for_scalar(space, w_elem,
dtype)
@@ -1349,7 +1349,7 @@
if dtype is None:
dtype = interp_dtype.get_dtype_cache(space).w_float64dtype
- if dtype.is_str_or_unicode() and dtype.itemtype.get_size() < 1:
+ if dtype.is_str_or_unicode() and dtype.get_size() < 1:
# promote S0 -> S1, U0 -> U1
dtype = interp_dtype.variable_dtype(space, dtype.char + '1')
if ndmin > len(shape):
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
@@ -475,8 +475,7 @@
if dt2.is_record_type():
return dt2
if dt1.is_str_or_unicode():
- if dt2.itemtype.get_element_size() >= \
- dt1.itemtype.get_element_size():
+ if dt2.get_size() >= dt1.get_size():
return dt2
return dt1
return dt2
@@ -556,7 +555,7 @@
return interp_dtype.variable_dtype(space,
'S%d' % space.len_w(w_obj))
elif current_guess.num == NPY_STRING:
- if current_guess.itemtype.get_size() < space.len_w(w_obj):
+ if current_guess.get_size() < space.len_w(w_obj):
return interp_dtype.variable_dtype(space,
'S%d' % space.len_w(w_obj))
return current_guess
diff --git a/pypy/module/micronumpy/iter.py b/pypy/module/micronumpy/iter.py
--- a/pypy/module/micronumpy/iter.py
+++ b/pypy/module/micronumpy/iter.py
@@ -164,7 +164,7 @@
self.array = array
self.offset = 0
self.dtype = array.dtype
- self.skip = self.dtype.itemtype.get_element_size()
+ self.skip = self.dtype.get_size()
self.size = array.size
def setitem(self, elem):
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -1591,17 +1591,8 @@
ComponentBoxType = interp_boxes.W_FloatLongBox
class BaseStringType(BaseType):
- _immutable_fields = ['size']
-
- def __init__(self, size=0):
- BaseType.__init__(self)
- self.size = size
-
def get_element_size(self):
- return self.size * rffi.sizeof(self.T)
-
- def get_size(self):
- return self.size
+ return rffi.sizeof(self.T)
def str_unary_op(func):
specialize.argtype(1)(func)
@@ -1636,13 +1627,13 @@
def store(self, arr, i, offset, box):
assert isinstance(box, interp_boxes.W_StringBox)
- return self._store(arr.storage, i, offset, box)
+ size = min(arr.dtype.size, box.arr.size - box.ofs)
+ return self._store(arr.storage, i, offset, box, size)
@jit.unroll_safe
- def _store(self, storage, i, offset, box):
+ def _store(self, storage, i, offset, box, size):
assert isinstance(box, interp_boxes.W_StringBox)
- # XXX simplify to range(box.dtype.get_size()) ?
- for k in range(min(self.size, box.arr.size-box.ofs)):
+ for k in range(size):
storage[k + offset + i] = box.arr.storage[k + box.ofs]
def read(self, arr, i, offset, dtype=None):
@@ -1725,17 +1716,17 @@
else:
w_arg = box.descr_str(space)
arg = space.str_w(space.str(w_arg))
- arr = VoidBoxStorage(self.size, mydtype)
+ arr = VoidBoxStorage(mydtype.size, mydtype)
i = 0
- for i in range(min(len(arg), self.size)):
+ for i in range(min(len(arg), mydtype.size)):
arr.storage[i] = arg[i]
- for j in range(i + 1, self.size):
+ for j in range(i + 1, mydtype.size):
arr.storage[j] = '\x00'
- return interp_boxes.W_StringBox(arr, 0, arr.dtype)
+ return interp_boxes.W_StringBox(arr, 0, arr.dtype)
def fill(self, storage, width, box, start, stop, offset):
for i in xrange(start, stop, width):
- self._store(storage, i, offset, box)
+ self._store(storage, i, offset, box, width)
class UnicodeType(BaseStringType):
T = lltype.UniChar
@@ -1772,14 +1763,14 @@
ofs += size
def coerce(self, space, dtype, w_items):
- arr = VoidBoxStorage(self.size, dtype)
+ arr = VoidBoxStorage(dtype.get_size(), dtype)
self._coerce(space, arr, 0, dtype, w_items, dtype.shape)
return interp_boxes.W_VoidBox(arr, 0, dtype)
@jit.unroll_safe
def store(self, arr, i, ofs, box):
assert isinstance(box, interp_boxes.W_VoidBox)
- for k in range(self.get_element_size()):
+ for k in range(box.arr.dtype.get_size()):
arr.storage[k + ofs] = box.arr.storage[k + box.ofs]
def readarray(self, arr, i, offset, dtype=None):
@@ -1793,15 +1784,14 @@
class RecordType(BaseType):
T = lltype.Char
- _immutable_fields_ = ['offsets_and_fields', 'size']
+ _immutable_fields_ = ['offsets_and_fields']
- def __init__(self, offsets_and_fields, size):
+ def __init__(self, offsets_and_fields):
BaseType.__init__(self)
self.offsets_and_fields = offsets_and_fields
- self.size = size
def get_element_size(self):
- return self.size
+ return rffi.sizeof(self.T)
def read(self, arr, i, offset, dtype=None):
if dtype is None:
@@ -1821,7 +1811,7 @@
raise OperationError(space.w_ValueError, space.wrap(
"wrong length"))
items_w = space.fixedview(w_item)
- arr = VoidBoxStorage(self.size, dtype)
+ arr = VoidBoxStorage(dtype.get_size(), dtype)
for i in range(len(items_w)):
subdtype = dtype.fields[dtype.fieldnames[i]][1]
ofs, itemtype = self.offsets_and_fields[i]
@@ -1833,7 +1823,7 @@
@jit.unroll_safe
def store(self, arr, i, ofs, box):
assert isinstance(box, interp_boxes.W_VoidBox)
- for k in range(self.get_element_size()):
+ for k in range(box.arr.dtype.get_size()):
arr.storage[k + i] = box.arr.storage[k + box.ofs]
@jit.unroll_safe
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit