[pypy-commit] pypy missing-ndarray-attributes: still not RPython, but make it slightly better. other fixes

fijal noreply at buildbot.pypy.org
Sun Sep 30 01:18:55 CEST 2012


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: missing-ndarray-attributes
Changeset: r57682:d47f1e045e11
Date: 2012-09-30 01:18 +0200
http://bitbucket.org/pypy/pypy/changeset/d47f1e045e11/

Log:	still not RPython, but make it slightly better. other fixes

diff --git a/pypy/module/micronumpy/arrayimpl/sort.py b/pypy/module/micronumpy/arrayimpl/sort.py
--- a/pypy/module/micronumpy/arrayimpl/sort.py
+++ b/pypy/module/micronumpy/arrayimpl/sort.py
@@ -1,15 +1,23 @@
+
+""" This is the implementation of various sorting routines in numpy. It's here
+because it only makes sense on a concrete array
+"""
 
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.rlib.listsort import make_timsort_class
 from pypy.rlib.objectmodel import specialize
 from pypy.rlib.rawstorage import raw_storage_getitem, raw_storage_setitem
+
+from pypy.interpreter.error import OperationError
 from pypy.module.micronumpy.base import W_NDimArray
-from pypy.module.micronumpy import interp_dtype
+from pypy.module.micronumpy import interp_dtype, types
 
 INT_SIZE = rffi.sizeof(lltype.Signed)
 
 @specialize.memo()
-def make_sort_classes(space, TP):
+def make_sort_classes(space, itemtype):
+    TP = itemtype.T
+    
     class ArgArrayRepresentation(object):
         def __init__(self, itemsize, size, values, indexes):
             self.itemsize = itemsize
@@ -18,9 +26,10 @@
             self.indexes = indexes
 
         def getitem(self, item):
-            return (raw_storage_getitem(TP, self.values, item * self.itemsize),
-                    raw_storage_getitem(lltype.Signed, self.indexes,
-                                        item * INT_SIZE))
+            v = raw_storage_getitem(TP, self.values, item * self.itemsize)
+            v = itemtype.for_computation(v)
+            return (v, raw_storage_getitem(lltype.Signed, self.indexes,
+                                           item * INT_SIZE))
 
         def setitem(self, idx, item):
             raw_storage_setitem(self.values, idx * self.itemsize,
@@ -48,14 +57,19 @@
     return ArgArrayRepresentation, ArgSort
 
 def sort_array(arr, space):
-    itemsize = arr.dtype.itemtype.get_element_size()
+    itemtype = arr.dtype.itemtype
+    if (not isinstance(itemtype, types.Float) and
+        not isinstance(itemtype, types.Integer)):
+        raise OperationError(space.w_NotImplementedError,
+           space.wrap("sorting of non-numeric types is not implemented"))
+    itemsize = itemtype.get_element_size()
     # create array of indexes
     dtype = interp_dtype.get_dtype_cache(space).w_longdtype
     indexes = W_NDimArray.from_shape([arr.get_size()], dtype)
     storage = indexes.implementation.get_storage()
     for i in range(arr.get_size()):
         raw_storage_setitem(storage, i * INT_SIZE, i)
-    Repr, Sort = make_sort_classes(space, arr.dtype.itemtype.T)
+    Repr, Sort = make_sort_classes(space, itemtype)
     r = Repr(itemsize, arr.get_size(), arr.get_storage(),
              indexes.implementation.get_storage())
     Sort(r).sort()
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
@@ -276,7 +276,8 @@
     def str_format(self, box):
         return "True" if self.unbox(box) else "False"
 
-    def for_computation(self, v):
+    @staticmethod
+    def for_computation(v):
         return int(v)
 
     def default_fromstring(self, space):
@@ -311,7 +312,8 @@
     def str_format(self, box):
         return str(self.for_computation(self.unbox(box)))
 
-    def for_computation(self, v):
+    @staticmethod
+    def for_computation(v):
         return widen(v)
 
     def default_fromstring(self, space):
@@ -556,7 +558,8 @@
         return float2string(self.for_computation(self.unbox(box)), "g",
                             rfloat.DTSF_STR_PRECISION)
 
-    def for_computation(self, v):
+    @staticmethod
+    def for_computation(v):
         return float(v)
 
     def default_fromstring(self, space):


More information about the pypy-commit mailing list