[pypy-svn] r29910 - in pypy/dist/pypy: annotation rpython/numpy rpython/numpy/test

simonb at codespeak.net simonb at codespeak.net
Sun Jul 9 19:02:50 CEST 2006


Author: simonb
Date: Sun Jul  9 19:02:47 2006
New Revision: 29910

Modified:
   pypy/dist/pypy/annotation/binaryop.py
   pypy/dist/pypy/annotation/model.py
   pypy/dist/pypy/rpython/numpy/aarray.py
   pypy/dist/pypy/rpython/numpy/test/test_array.py
Log:
(arre, simon) took Numpy stuff out of annotation package, renamed SomeNumpyObject -> SomeArray, implementation of 'add' operation annotation and rtyping for arrays

Modified: pypy/dist/pypy/annotation/binaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/binaryop.py	(original)
+++ pypy/dist/pypy/annotation/binaryop.py	Sun Jul  9 19:02:47 2006
@@ -15,7 +15,6 @@
 from pypy.annotation.model import SomeAddress, SomeTypedAddressAccess
 from pypy.annotation.model import SomeWeakGcAddress
 from pypy.annotation.model import SomeCTypesObject
-from pypy.annotation.model import SomeNumpyObject
 from pypy.annotation.model import unionof, UnionError, set, missing_operation, TLS
 from pypy.annotation.model import add_knowntypedata, merge_knowntypedata
 from pypy.annotation.model import lltype_to_annotation
@@ -797,14 +796,6 @@
         s_result = SomeCTypesObject(result_ctype, ownsmemory=False)
         return s_result.return_annotation()
 
-class __extend__(pairtype(SomeNumpyObject, SomeInteger)):
-    def setitem((s_cto, s_index), s_value):
-        pass
-
-    def getitem((s_cto, s_index)):
-        # TODO: higher ranked arrays have getitem returns SomeNumpyObject
-        return s_cto.get_item_type()
-
 class __extend__(pairtype(SomeCTypesObject, SomeSlice)):
     # XXX ctypes array slicing not really supported for now
     def setitem((s_cto, s_slice), s_iterable):

Modified: pypy/dist/pypy/annotation/model.py
==============================================================================
--- pypy/dist/pypy/annotation/model.py	(original)
+++ pypy/dist/pypy/annotation/model.py	Sun Jul  9 19:02:47 2006
@@ -455,51 +455,6 @@
         # special case for returning primitives or c_char_p
         return getattr(entry, 's_return_trick', self)
 
-class SomeNumpyObject(SomeExternalObject):
-    """Stands for an object from the numpy module."""
-    from pypy.rpython.rctypes import rcarithmetic
-    typecode_to_item = {
-        'b' : SomeInteger(knowntype=rcarithmetic.rcbyte),
-        'h' : SomeInteger(knowntype=rcarithmetic.rcshort),
-        'i' : SomeInteger(knowntype=rcarithmetic.rcint),
-        'l' : SomeInteger(knowntype=rcarithmetic.rclong),
-        'q' : SomeInteger(knowntype=rcarithmetic.rclonglong),
-        'B' : SomeInteger(knowntype=rcarithmetic.rcubyte),
-        'H' : SomeInteger(knowntype=rcarithmetic.rcushort),
-        'I' : SomeInteger(knowntype=rcarithmetic.rcuint),
-        'L' : SomeInteger(knowntype=rcarithmetic.rculong),
-        'Q' : SomeInteger(knowntype=rcarithmetic.rculonglong),
-        'f' : SomeFloat(), # XX single precision float XX
-        'd' : SomeFloat(),
-    }
-    def __init__(self, knowntype, typecode, ownsmemory):
-        self.knowntype = knowntype
-        self.ownsmemory = ownsmemory
-	self.typecode = typecode
-	self.rank = 1
-        # 'ownsmemory' specifies if the object is *statically known* to own
-        # its C memory.  If it is False, it will be rtyped as an alias object.
-        # Alias objects are allowed, at run-time, to have keepalives, so
-        # that they can indirectly own their memory too (it's just less
-        # efficient).
-
-    def can_be_none(self):
-        return True
-
-    def return_annotation(self):
-        """Returns either 'self' or the annotation of the unwrapped version
-        of this ctype, following the logic used when ctypes operations
-        return a value.
-        """
-        from pypy.rpython import extregistry
-        assert extregistry.is_registered_type(self.knowntype)
-        entry = extregistry.lookup_type(self.knowntype)
-        # special case for returning primitives or c_char_p
-        return getattr(entry, 's_return_trick', self)
-
-    def get_item_type(self):
-        return self.typecode_to_item[self.typecode]
-
 class SomeImpossibleValue(SomeObject):
     """The empty set.  Instances are placeholders for objects that
     will never show up at run-time, e.g. elements of an empty list."""

Modified: pypy/dist/pypy/rpython/numpy/aarray.py
==============================================================================
--- pypy/dist/pypy/rpython/numpy/aarray.py	(original)
+++ pypy/dist/pypy/rpython/numpy/aarray.py	Sun Jul  9 19:02:47 2006
@@ -1,5 +1,6 @@
 from pypy.rpython.extregistry import ExtRegistryEntry
-from pypy.annotation.model import SomeNumpyObject, SomeList, SomeImpossibleValue
+from pypy.annotation.pairtype import pairtype
+from pypy.annotation.model import SomeExternalObject, SomeList, SomeImpossibleValue
 from pypy.annotation.model import SomeInteger, SomeFloat, SomeString, SomeChar
 from pypy.annotation.listdef import ListDef
 from pypy.rpython.rctypes import rcarithmetic
@@ -7,6 +8,58 @@
 
 import numpy
 
+class SomeArray(SomeExternalObject):
+    """Stands for an object from the numpy module."""
+    from pypy.rpython.rctypes import rcarithmetic
+    typecode_to_item = {
+        'b' : SomeInteger(knowntype=rcarithmetic.rcbyte),
+        'h' : SomeInteger(knowntype=rcarithmetic.rcshort),
+        'i' : SomeInteger(knowntype=rcarithmetic.rcint),
+        'l' : SomeInteger(knowntype=rcarithmetic.rclong),
+        'q' : SomeInteger(knowntype=rcarithmetic.rclonglong),
+        'B' : SomeInteger(knowntype=rcarithmetic.rcubyte),
+        'H' : SomeInteger(knowntype=rcarithmetic.rcushort),
+        'I' : SomeInteger(knowntype=rcarithmetic.rcuint),
+        'L' : SomeInteger(knowntype=rcarithmetic.rculong),
+        'Q' : SomeInteger(knowntype=rcarithmetic.rculonglong),
+        'f' : SomeFloat(), # XX single precision float XX
+        'd' : SomeFloat(),
+    }
+    def __init__(self, knowntype, typecode):
+        self.knowntype = knowntype
+	self.typecode = typecode
+	self.rank = 1
+
+    def can_be_none(self):
+        return True
+
+    def return_annotation(self):
+        """Returns either 'self' or the annotation of the unwrapped version
+        of this ctype, following the logic used when ctypes operations
+        return a value.
+        """
+        from pypy.rpython import extregistry
+        assert extregistry.is_registered_type(self.knowntype)
+        entry = extregistry.lookup_type(self.knowntype)
+        # special case for returning primitives or c_char_p
+        return getattr(entry, 's_return_trick', self)
+
+    def get_item_type(self):
+        return self.typecode_to_item[self.typecode]
+
+class __extend__(pairtype(SomeArray, SomeArray)):
+    def add((s_arr1,s_arr2)):
+        # TODO: coerce the array types
+        return SomeArray(s_arr1.knowntype, s_arr1.typecode)
+
+class __extend__(pairtype(SomeArray, SomeInteger)):
+    def setitem((s_cto, s_index), s_value):
+        pass
+
+    def getitem((s_cto, s_index)):
+        # TODO: higher ranked arrays have getitem returns SomeArray
+        return s_cto.get_item_type()
+
 numpy_typedict = {
     (SomeInteger, rcarithmetic.rcbyte) : 'b', 
     (SomeInteger, rcarithmetic.rcshort) : 'h', 
@@ -52,7 +105,7 @@
         if typecode is None or typecode not in valid_typecodes:
             raise AnnotatorError("List item type not supported")
         knowntype = numpy.ndarray
-        return SomeNumpyObject(knowntype, typecode, ownsmemory=True)
+        return SomeArray(knowntype, typecode)
 
     def specialize_call(self, hop):
         r_array = hop.r_result

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	Sun Jul  9 19:02:47 2006
@@ -11,6 +11,8 @@
 import sys
 from pypy.rpython.test.test_llinterp import interpret
 from pypy.rpython.rctypes import rcarithmetic
+from pypy.rpython.rint import IntegerRepr
+from pypy.rpython.numpy.rarray import ArrayRepr
 
 import numpy
 
@@ -66,14 +68,15 @@
         assert s.knowntype == rcarithmetic.rcint
 
 class Test_specialization:
-    def test_specialize_array(self):
+    def test_specialize_array_create(self):
         def create_array():
             return numpy.array([1,2])
 
         res = interpret(create_array, [])
-        print res
+        assert res.data[0] == 1
+        assert res.data[1] == 2
 
-    def test_specialize_array2(self):
+    def test_specialize_array_access(self):
         def access_with_variable():
             my_array = numpy.array(range(10))
             my_array[2] = 2
@@ -84,7 +87,17 @@
             return sum
 
         res = interpret(access_with_variable, [])
-        print res
+        assert res == 45
+
+    def test_specialize_array_add(self):
+        def create_array():
+            a1 = numpy.array([1,2])
+            a2 = numpy.array([6,9])
+            return a1 + a2
+
+        res = interpret(create_array, [])
+        assert res.data[0] == 7
+        assert res.data[1] == 11
 
 class Test_compile:
     def setup_class(self):



More information about the Pypy-commit mailing list