[pypy-commit] pypy numpy-record-dtypes: implement .byteorder
fijal
noreply at buildbot.pypy.org
Sun Mar 18 09:52:52 CET 2012
Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: numpy-record-dtypes
Changeset: r53779:e7b2477f1ffc
Date: 2012-03-18 09:30 +0200
http://bitbucket.org/pypy/pypy/changeset/e7b2477f1ffc/
Log: implement .byteorder
diff --git a/REVIEW.rst b/REVIEW.rst
--- a/REVIEW.rst
+++ b/REVIEW.rst
@@ -1,7 +1,6 @@
REVIEW
======
-* expose endianess on dtypes
* RecordType.str_format should use Builder
* IntP and UIntP aren't the right size, they should be the same size of rffi.VOIDP, not as Signed/Unsigned
* Instead of setup() can we please have get_alignment on the Type class.
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
@@ -21,8 +21,9 @@
class W_Dtype(Wrappable):
_immutable_fields_ = ["itemtype", "num", "kind"]
- def __init__(self, itemtype, num, kind, name, char, w_box_type, alternate_constructors=[], aliases=[],
- fields=None, fieldnames=None):
+ def __init__(self, itemtype, num, kind, name, char, w_box_type,
+ alternate_constructors=[], aliases=[],
+ fields=None, fieldnames=None, native=True):
self.itemtype = itemtype
self.num = num
self.kind = kind
@@ -33,6 +34,7 @@
self.aliases = aliases
self.fields = fields
self.fieldnames = fieldnames
+ self.native = native
@specialize.argtype(1)
def box(self, value):
@@ -62,6 +64,11 @@
def descr_get_itemsize(self, space):
return space.wrap(self.itemtype.get_element_size())
+ def descr_get_byteorder(self, space):
+ if self.native:
+ return space.wrap('=')
+ return space.wrap(nonnative_byteorder_prefix)
+
def descr_get_alignment(self, space):
return space.wrap(self.itemtype.alignment)
@@ -149,11 +156,7 @@
"dtype from dict"))
def variable_dtype(space, name):
- if name[0] in '<>':
- # ignore byte order, not sure if it's worth it for unicode only
- if name[0] != byteorder_prefix and name[1] == 'U':
- raise OperationError(space.w_NotImplementedError, space.wrap(
- "unimplemented non-native unicode"))
+ if name[0] in '<>=':
name = name[1:]
char = name[0]
if len(name) == 1:
@@ -203,7 +206,7 @@
return cache.dtypes_by_name[name]
except KeyError:
pass
- if name[0] in 'VSU' or name[0] in '<>' and name[1] in 'VSU':
+ if name[0] in 'VSU' or name[0] in '<>=' and name[1] in 'VSU':
return variable_dtype(space, name)
elif space.isinstance_w(w_dtype, space.w_list):
return dtype_from_list(space, w_dtype)
@@ -231,6 +234,7 @@
kind = interp_attrproperty("kind", cls=W_Dtype),
char = interp_attrproperty("char", cls=W_Dtype),
type = interp_attrproperty_w("w_box_type", cls=W_Dtype),
+ byteorder = GetSetProperty(W_Dtype.descr_get_byteorder),
itemsize = GetSetProperty(W_Dtype.descr_get_itemsize),
alignment = GetSetProperty(W_Dtype.descr_get_alignment),
shape = GetSetProperty(W_Dtype.descr_get_shape),
@@ -446,12 +450,14 @@
can_name = dtype.kind + str(dtype.itemtype.get_element_size())
self.dtypes_by_name[can_name] = dtype
self.dtypes_by_name[byteorder_prefix + can_name] = dtype
+ self.dtypes_by_name['=' + can_name] = dtype
new_name = nonnative_byteorder_prefix + can_name
itemtypename = dtype.itemtype.__class__.__name__
itemtype = getattr(types, 'NonNative' + itemtypename)()
self.dtypes_by_name[new_name] = W_Dtype(
itemtype,
- dtype.num, dtype.kind, new_name, dtype.char, dtype.w_box_type)
+ dtype.num, dtype.kind, new_name, dtype.char, dtype.w_box_type,
+ native=False)
for alias in dtype.aliases:
self.dtypes_by_name[alias] = dtype
self.dtypes_by_name[dtype.char] = dtype
diff --git a/pypy/module/micronumpy/test/test_base.py b/pypy/module/micronumpy/test/test_base.py
--- a/pypy/module/micronumpy/test/test_base.py
+++ b/pypy/module/micronumpy/test/test_base.py
@@ -4,7 +4,8 @@
from pypy.module.micronumpy.interp_ufuncs import (find_binop_result_dtype,
find_unaryop_result_dtype)
from pypy.module.micronumpy.interp_boxes import W_Float64Box
-from pypy.module.micronumpy.interp_dtype import nonnative_byteorder_prefix
+from pypy.module.micronumpy.interp_dtype import nonnative_byteorder_prefix,\
+ byteorder_prefix
from pypy.conftest import option
import sys
@@ -17,6 +18,7 @@
sys.modules['_numpypy'] = numpy
cls.space = gettestobjspace(usemodules=['micronumpy'])
cls.w_non_native_prefix = cls.space.wrap(nonnative_byteorder_prefix)
+ cls.w_native_prefix = cls.space.wrap(byteorder_prefix)
class TestSignature(object):
def test_binop_signature(self, 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
@@ -458,8 +458,13 @@
def test_alternate_constructs(self):
from _numpypy import dtype
- assert dtype('i8') == dtype('<i8')# XXX should be equal == dtype(long)
- assert dtype(self.non_native_prefix + 'i8') != dtype('i8')
+ nnp = self.non_native_prefix
+ byteorder = self.native_prefix
+ assert dtype('i8') == dtype(byteorder + 'i8') == dtype('=i8') # XXX should be equal == dtype(long)
+ assert dtype(nnp + 'i8') != dtype('i8')
+ assert dtype(nnp + 'i8').byteorder == nnp
+ assert dtype('=i8').byteorder == '='
+ assert dtype(byteorder + 'i8').byteorder == '='
def test_alignment(self):
from _numpypy import dtype
More information about the pypy-commit
mailing list