Maciej Fijalkowski committed d47f1e0

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

Comments (0)

Files changed (2)


+""" 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)
-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
             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,
     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(),


     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):
     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):
         return float2string(self.for_computation(self.unbox(box)), "g",
-    def for_computation(self, v):
+    @staticmethod
+    def for_computation(v):
         return float(v)
     def default_fromstring(self, space):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.