[pypy-svn] r46593 - in pypy/dist/pypy/rpython/numpy: . test

simonb at codespeak.net simonb at codespeak.net
Fri Sep 14 23:44:56 CEST 2007


Author: simonb
Date: Fri Sep 14 23:44:45 2007
New Revision: 46593

Modified:
   pypy/dist/pypy/rpython/numpy/aarray.py
   pypy/dist/pypy/rpython/numpy/rarray.py
   pypy/dist/pypy/rpython/numpy/test/test_array.py
Log:
add dtype attribute, annotation of copy method

Modified: pypy/dist/pypy/rpython/numpy/aarray.py
==============================================================================
--- pypy/dist/pypy/rpython/numpy/aarray.py	(original)
+++ pypy/dist/pypy/rpython/numpy/aarray.py	Fri Sep 14 23:44:45 2007
@@ -46,7 +46,7 @@
     def __init__(self, typecode, ndim=1):
         if not typecode in self.typecode_to_item:
             raise AnnotatorError("bad typecode: %r"%typecode)
-        self.typecode = typecode
+        self.dtype = self.typecode = typecode
         self.ndim = ndim
 
     def get_one_dim(self):
@@ -66,6 +66,8 @@
                 s = SomeTuple([SomeInteger()]*s_array.ndim)
             elif attr == 'ndim':
                 s = SomeInteger()
+            elif attr == 'dtype':
+                s = SomeChar()
         if s is None:
             return SomeObject.getattr(s_array, s_attr)
         return s
@@ -81,6 +83,7 @@
 
     def method_transpose(self):
         return SomeArray(self.typecode, self.ndim)
+    method_copy = method_transpose
 
     def method_astype(self, s_dtype):
         if isinstance(s_dtype, SomeChar) and s_dtype.is_constant():

Modified: pypy/dist/pypy/rpython/numpy/rarray.py
==============================================================================
--- pypy/dist/pypy/rpython/numpy/rarray.py	(original)
+++ pypy/dist/pypy/rpython/numpy/rarray.py	Fri Sep 14 23:44:45 2007
@@ -17,7 +17,7 @@
 from pypy.rpython.lltypesystem import lltype, llmemory, rtuple
 from pypy.rpython.lltypesystem.rtupletype import TUPLE_TYPE
 from pypy.rpython.lltypesystem.lltype import \
-    GcArray, GcStruct, Number, Primitive, Signed, Ptr, Unsigned, Void, FixedSizeArray, Bool,\
+    GcArray, GcStruct, Number, Primitive, Signed, Ptr, Unsigned, Char, Void, FixedSizeArray, Bool,\
     GcForwardReference, malloc, direct_arrayitems, direct_ptradd, nullptr, typeMethod,\
     cast_primitive
 from pypy.rpython.lltypesystem.rffi import cast
@@ -315,6 +315,10 @@
         cname = inputconst(Void, 'ndim')
         return hop.llops.genop('getfield', [v_array, cname], resulttype=Signed)
 
+    def get_dtype(self, hop, v_array):
+        cdtype = inputconst(Char, self.s_array.dtype)
+        return cdtype
+
     def get_shape(self, hop, v_array):
         TUPLE = TUPLE_TYPE([Signed]*self.ndim)
         cARRAY = inputconst(lltype.Void, self.lowleveltype.TO) 

Modified: pypy/dist/pypy/rpython/numpy/test/test_array.py
==============================================================================
--- pypy/dist/pypy/rpython/numpy/test/test_array.py	(original)
+++ pypy/dist/pypy/rpython/numpy/test/test_array.py	Fri Sep 14 23:44:45 2007
@@ -5,7 +5,7 @@
 import py
 import pypy.rpython.numpy.implementation
 from pypy.annotation import model as annmodel
-from pypy.annotation.model import SomeObject, SomeTuple
+from pypy.annotation.model import SomeObject, SomeInteger, SomeChar, SomeTuple
 from pypy.annotation.annrpython import RPythonAnnotator
 from pypy.tool.error import AnnotatorError
 from pypy.translator.translator import TranslationContext, graphof
@@ -78,6 +78,34 @@
         s = a.build_types(access_with_variable, [])
         assert s.knowntype == rffi.r_int
 
+    def test_annotate_attr(self):
+        def f():
+            a = numpy.empty((3,4,5))
+            return a.ndim
+
+        t = TranslationContext()
+        a = t.buildannotator()
+        s = a.build_types(f, [])
+        assert isinstance(s, SomeInteger)
+
+        def f():
+            a = numpy.empty((3,4,5))
+            return a.shape
+
+        t = TranslationContext()
+        a = t.buildannotator()
+        s = a.build_types(f, [])
+        assert isinstance(s, SomeTuple)
+
+        def f():
+            a = numpy.empty((3,4,5))
+            return a.dtype
+
+        t = TranslationContext()
+        a = t.buildannotator()
+        s = a.build_types(f, [])
+        assert isinstance(s, SomeChar)
+
     def test_annotate_empty(self):
         def f():
             a = numpy.empty((3,4,5))
@@ -240,6 +268,15 @@
         assert s.ndim == 1
         assert s.typecode == 'd'
 
+        def f_copy():
+            a = numpy.array(range(12))
+            return a.copy()
+
+        s = a.build_types(f_copy, [])
+        assert type(s) == SomeArray
+        assert s.ndim == 1
+        assert s.typecode == 'l'
+
     def test_annotate_indexing(self):
         def f():
             a = numpy.empty((4,3), dtype='i')
@@ -316,6 +353,28 @@
             assert res.dataptr[i] == 0
         assert res.ndim == 2
 
+    def test_specialize_array_attr(self):
+        def f():
+            a = numpy.empty((3,4,5))
+            return a.ndim
+        res = interpret(f, [])
+        assert res == 3
+
+        def f():
+            a = numpy.empty((3,4,5))
+            return a.shape
+        res = interpret(f, [])
+        assert res.item0 == 3
+        assert res.item1 == 4
+        assert res.item2 == 5
+
+        def f():
+            a = numpy.empty((3,4,5))
+            return a.dtype
+        res = interpret(f, [])
+        assert res == 'd'
+
+
     def test_specialize_array_access(self):
         def access_with_variable():
             my_array = numpy.array(range(10), dtype='i')
@@ -329,14 +388,6 @@
         res = interpret(access_with_variable, [])
         assert res == 45
 
-    def test_specialize_array_attr(self):
-        def f():
-            a = numpy.array([1,2])
-            return a.ndim
-
-        res = interpret(f, [])
-        assert res == 1
-
     def test_specialize_array_attr_shape(self):
         def f():
             a = numpy.empty((2,3))
@@ -392,6 +443,16 @@
         assert res.dataptr[0] == 0.
         assert res.dataptr[1] == 0.5
 
+        def f_copy():
+            a = numpy.array(range(4))
+            b = a.copy()
+            a[:] = 0
+            return b
+
+        res = interpret(f_copy, [])
+        for i in range(4):
+            assert res.dataptr[i] == i
+
     def test_specialize_view_0(self):
         def f():
             a = numpy.empty((4,3), dtype='i')



More information about the Pypy-commit mailing list