Author: mattip <[email protected]>
Branch: dtypes-compatability
Changeset: r78518:46fc142d78df
Date: 2015-07-09 00:32 +0300
http://bitbucket.org/pypy/pypy/changeset/46fc142d78df/
Log: str(dtype), repr(dtype), dtype.descr all use different formatting,
add failing test
diff --git a/pypy/module/micronumpy/descriptor.py
b/pypy/module/micronumpy/descriptor.py
--- a/pypy/module/micronumpy/descriptor.py
+++ b/pypy/module/micronumpy/descriptor.py
@@ -198,17 +198,61 @@
size >>= 2
return space.wrap("%s%s%s" % (endian, basic, size))
- def descr_get_descr(self, space):
+ def descr_get_descr(self, space, style='descr'):
if not self.is_record():
return space.newlist([space.newtuple([space.wrap(""),
self.descr_get_str(space)])])
+ elif self.alignment >= 0 and style != 'descr':
+ # we need to force a sorting order for the keys,
+ # so return a string instead of a dict
+ names = "'names':["
+ formats = "'formats':["
+ offsets = "'offsets':["
+ titles = "'titles':["
+ use_titles = False
+ for name, title in self.names:
+ offset, subdtype = self.fields[name]
+ if subdtype.is_record():
+ substr = space.str_w(subdtype.descr_get_descr(space,
style))
+ elif subdtype.subdtype is not None:
+ substr = space.str_w(
+ subdtype.subdtype.descr_get_str(space)).replace('|',
'')
+ else:
+ substr = space.str_w(
+ subdtype.descr_get_str(space)).replace('|', '')
+ offsets += str(offset)
+ names += "'" + name + "'"
+ titles += "'" + str(title) + "'"
+ if title is not None:
+ use_titles = True
+ formats += "'" + substr + "',"
+ offsets += ','
+ names += ','
+ titles += ','
+ formats = formats[:-1] + ']'
+ offsets = offsets[:-1] + ']'
+ names = names[:-1] + ']'
+ titles = titles[:-1] + ']'
+ if style == 'str':
+ suffix = ", 'aligned':True}"
+ else:
+ suffix = "}, align=True"
+ if use_titles:
+ return space.wrap('{' + names + ', ' + formats + ', ' +
+ offsets + ', ' + "'itemsize':" + str(self.elsize) +
+ titles + ', ' + suffix)
+ else:
+ return space.wrap('{' + names + ', ' + formats + ', ' +
+ offsets + ', ' + "'itemsize':" + str(self.elsize) +
+ suffix)
+
else:
descr = []
for name, title in self.names:
subdtype = self.fields[name][1]
subdescr = [space.wrap(name)]
if subdtype.is_record():
- subdescr.append(subdtype.descr_get_descr(space))
+ subdescr.append(subdtype.descr_get_descr(space, style))
elif subdtype.subdtype is not None:
subdescr.append(subdtype.subdtype.descr_get_str(space))
else:
@@ -382,7 +426,7 @@
def descr_str(self, space):
if self.fields:
- return space.str(self.descr_get_descr(space))
+ return space.str(self.descr_get_descr(space, style='str'))
elif self.subdtype is not None:
return space.str(space.newtuple([
self.subdtype.descr_get_str(space),
@@ -395,7 +439,7 @@
def descr_repr(self, space):
if self.fields:
- r = self.descr_get_descr(space)
+ r = self.descr_get_descr(space, style='repr')
elif self.subdtype is not None:
r = space.newtuple([self.subdtype.descr_get_str(space),
self.descr_get_shape(space)])
diff --git a/pypy/module/micronumpy/test/test_dtypes.py
b/pypy/module/micronumpy/test/test_dtypes.py
--- a/pypy/module/micronumpy/test/test_dtypes.py
+++ b/pypy/module/micronumpy/test/test_dtypes.py
@@ -1324,11 +1324,14 @@
dt = np.dtype({'f0': ('i4', 0), 'f1':('u1', 4)}, align=True)
assert dt.itemsize == 8
assert dt.alignment == 4
+ assert str(dt) == "{'names':['f0','f1'], 'formats':['<i4','u1'],
'offsets':[0,4], 'itemsize':8, 'aligned':True}"
+ dt = np.dtype([('f1', 'u1'), ('f0', 'i4')], align=True)
+ assert str(dt) == "{'names':['f1','f0'], 'formats':['u1','<i4'],
'offsets':[0,4], 'itemsize':8, 'aligned':True}"
# Nesting should preserve that alignment
dt1 = np.dtype([('f0', 'i4'),
('f1', [('f1', 'i1'), ('f2', 'i4'), ('f3', 'i1')]),
('f2', 'i1')], align=True)
- assert dt.alignment == 4
+ assert dt1.alignment == 4
assert dt1['f1'].itemsize == 12
assert dt1.itemsize == 20
dt2 = np.dtype({'names':['f0', 'f1', 'f2'],
@@ -1342,6 +1345,12 @@
'f2': ('i1', 16)}, align=True)
assert dt3.itemsize == 20
assert dt1 == dt2
+ assert str(dt3) == "{'names':['f0','f1','f2'], " + \
+ "'formats':['<i4',{'names':['f1','f2','f3'], " + \
+ "'formats':['i1','<i4','i1'], "
+ \
+ "'offsets':[0,4,8],
'itemsize':12}," + \
+ "'i1'], " + \
+ "'offsets':[0,4,16], 'itemsize':20,
'aligned':True}"
print '+++++++++++++++++'
assert dt2 == dt3
# Nesting should preserve packing
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit