[pypy-svn] r18619 - pypy/dist/pypy/module/Numeric

aft at codespeak.net aft at codespeak.net
Sat Oct 15 13:40:31 CEST 2005


Author: aft
Date: Sat Oct 15 13:40:28 2005
New Revision: 18619

Modified:
   pypy/dist/pypy/module/Numeric/__init__.py
   pypy/dist/pypy/module/Numeric/app_numeric.py
   pypy/dist/pypy/module/Numeric/interp_numeric.py
Log:
Merging...

Modified: pypy/dist/pypy/module/Numeric/__init__.py
==============================================================================
--- pypy/dist/pypy/module/Numeric/__init__.py	(original)
+++ pypy/dist/pypy/module/Numeric/__init__.py	Sat Oct 15 13:40:28 2005
@@ -3,7 +3,7 @@
 class Module(MixedModule):
     """An RPython reimplementation of the Numeric module
 """
-    
+
     appleveldefs = {
     }
 
@@ -12,10 +12,11 @@
         'Int' : "space.wrap('l')",
 #        'array' : 'interp_numeric.w_array',
         'zeros' : 'interp_numeric.w_zeros',
-        'nzeros' : 'interp_numeric.w_nzeros',
-        'array'  : 'interp_numeric.w_array',   
+        'array'  : 'interp_numeric.array',
+        'TOWER_TYPES' : 'space.wrap(interp_numeric.TOWER_TYPES)',
+        'TOWER_TYPES_VALUES' :'space.wrap(interp_numeric.TOWER_TYPES_VALUES)'
         }
-        
+
 ##         'CODESIZE':       'space.wrap(interp_sre.CODESIZE)',
 ##         'MAGIC':          'space.wrap(interp_sre.MAGIC)',
 ##         'copyright':      'space.wrap(interp_sre.copyright)',

Modified: pypy/dist/pypy/module/Numeric/app_numeric.py
==============================================================================
--- pypy/dist/pypy/module/Numeric/app_numeric.py	(original)
+++ pypy/dist/pypy/module/Numeric/app_numeric.py	Sat Oct 15 13:40:28 2005
@@ -1,34 +1,11 @@
 
 
-from Numeric import zeros,nzeros,array
-from Numeric import  Float
+from Numeric import zeros,array
+from Numeric import  Float,TOWER_TYPES,TOWER_TYPES_VALUES
 
 
 
-class TestArray:
-    def test(self):
-        a = zeros( (3,2), Float )
-        assert (3,2) == a.shape
 
-        b = zeros( (8,), Float )
-        assert 0.==b[1]
-        b[1]= 1.
-        assert 1.==b[1]
-
-    def testZeros(self):
-        pass
-
-TestArray().test()
-TestArray().testZeros()
-#### Original test above.
-
-a=nzeros((2,7),Float)
-
-assert (2,7)== a.shape
-b=nzeros((10,),Float)
-assert 0.==b[2]
-b[3]=555
-assert b[3]==555
 
 def assertRaises(block,exception=Exception,shout='This should raise an exception'):
     try:
@@ -38,8 +15,32 @@
     else:
         assert False,shout
 
+"""
+PyPy issues : >>>> 1=1 produces syntax error, but in script, takes AGES to eventually do this.
+this is due to the size of the traceback which is generated in the script.
+
+"""
+#first we check the really simple, empty or minimal arrays
+assert (0,)==array(()).shape
+assert ()==array((1)).shape
+assert array(()).isArray()  and array((1)).isArray()
+
+#next we check the typecodes on these small examples
+assert 'l'==array(()).typecode()
+assert 'l'==array((1)).typecode()
+assert 'd'==array((1.0)).typecode()
+
+#we are not supporting complex numbers or any other objects yet
+assertRaises(lambda :array((1j)),ValueError)
+assertRaises(lambda :array((1j,)),ValueError)
+assertRaises(lambda :array(('a')),ValueError)
+
+#now check accessing of values on empty array, and a scalar
+#assertRaises(lambda :array(())[0],IndexError
+
+
+
+
+
 
-assertRaises(lambda :array(()),exception=ValueError)   #this should fail
 
-a=array([1])
-#assert a[0]==1   last test broken...

Modified: pypy/dist/pypy/module/Numeric/interp_numeric.py
==============================================================================
--- pypy/dist/pypy/module/Numeric/interp_numeric.py	(original)
+++ pypy/dist/pypy/module/Numeric/interp_numeric.py	Sat Oct 15 13:40:28 2005
@@ -153,7 +153,7 @@
         if len(idx_tuple)!=len(self.dims):
             # TODO raise OperationError or remove this and make it a pre-condition
             raise RuntimeError
-        
+
         idx = self.get_array_offset( idx_tuple )
         return space.wrap( self.storage[idx] )
 
@@ -180,7 +180,7 @@
                 offset+= idx.index*self.strides[i]
         array = W_Array_Float( space, dims, strides, self.storage )
         return space.wrap(array)
-        
+
 
 descr___getitem__ = interp2app( W_Array.descr___getitem__, unwrap_spec=['self', ObjSpace, W_Root ] )
 descr___setitem__ = interp2app( W_Array.descr___setitem__, unwrap_spec=['self', ObjSpace, W_Root, W_Root ] )
@@ -211,18 +211,10 @@
 """
 class W_NumericArray(Wrappable):
 
-    def __init__(self, space, dims ):
+    def __init__(self, space, dims ,value=None ):
         self.space = space
-        assert isinstance(dims, list)
         self.dims = dims
-        self.strides = [1]
-        self.base_object = None
-        self.base_offset = 0 # needed later for offseting into a shared storage
-        stride = 1
-        for n in self.dims[:-1]:
-            stride *= n
-            self.strides.append( stride )
-        self.strides.reverse()
+        self.value= value
 
     def check_space_true(self, space, w_index):
         if not space.is_true(space.isinstance( w_index, space.w_int )):
@@ -231,6 +223,15 @@
         assert isinstance( idx, int )
         return idx
 
+    def isArray(self,space):
+        return self.space.wrap(True)
+
+    def typecode(self,space):
+        code='l'
+        if isinstance(self.value,float):
+            code='d'
+        return self.space.wrap(code)
+
     def descr___getitem__( self, space, w_index ):
         return self.get_single_item( space, [ self.check_space_true( space, w_index)])
 
@@ -252,22 +253,6 @@
             idx += self.strides[i]*idx_tuple[i]
         return idx
 
-
-class W_NumericArray_Float(W_NumericArray):
-
-    def __init__(self, space, dims, storage=None ):
-        W_NumericArray.__init__(self, space, dims )
-        storage_size = get_storage_size(dims)
-        self.storage = []
-        if storage is not None:
-            assert isinstance(storage, list)
-            # TODO return proper exception here
-            assert len(storage)==storage_size
-            assert isinstance(storage[0], float)
-            self.storage = storage
-        else:
-            self.storage = [0.0]*storage_size
-
     def get_single_item( self, space, idx_tuple ):
         if len(idx_tuple)!=len(self.dims):
             # TODO raise OperationError or remove this and make it a pre-condition
@@ -280,32 +265,44 @@
         value = space.float_w( w_value )
         self.storage[idx] = value
 
+
+
+
 descr___getitem__ = interp2app( W_NumericArray.descr___getitem__, unwrap_spec=['self', ObjSpace, W_Root ] )
 descr___setitem__ = interp2app( W_NumericArray.descr___setitem__, unwrap_spec=['self', ObjSpace, W_Root, W_Root ] )
 
+isArray = interp2app( W_NumericArray.isArray, unwrap_spec=['self', ObjSpace ] )
+typecode = interp2app( W_NumericArray.typecode, unwrap_spec=['self', ObjSpace ] )
+
+
 
 W_NumericArray.typedef = TypeDef("W_NumericArray",
                           shape = GetSetProperty( W_NumericArray.fget_shape, cls=W_NumericArray),
+                          isArray = isArray,
+                          typecode= typecode,
                           __getitem__ = descr___getitem__,
                           __setitem__ = descr___setitem__,
                           )
 
-W_NumericArray_Float.typedef = TypeDef("W_NumericArray_Float", W_NumericArray.typedef,
-                                )
-
-def w_nzeros( space, w_dim_tuple, type_str ):
-    dims = []
-    for w_int in space.unpackiterable(w_dim_tuple):
-        dims.append( space.unwrap( w_int ) )
-    if type_str == 'd':
-        return space.wrap(W_NumericArray_Float( space, dims ))
-    raise OperationError( space.w_ValueError, space.wrap('Unknown type code') )
+TOWER_TYPES_VALUES=[(int,1),(float,1.0),(complex,2.0+3j)] #we will work with these types to start with
+TOWER_TYPES_VALUES=TOWER_TYPES_VALUES[0:2]   #cannot unpack complex values yet at interp level.
 
-w_nzeros.unwrap_spec = [ ObjSpace, W_Root, str ]
+TOWER_TYPES=[typ for typ,val in TOWER_TYPES_VALUES]
 
+def OpError(space,w_arg):
+    raise OperationError( space.w_ValueError,space.wrap('Cannot unwrap this <%s>'%str(w_arg)))
 
-def w_array( space, w_dim_tuple):
-    raise OperationError( space.w_ValueError, space.wrap('Cannot create void array'))
+def array( space, w_arg):
+    try:
+        arg=space.unwrap( w_arg)
+    except:
+        OpError(space,w_arg)
+    if arg in ((),[]):
+        return W_NumericArray(space,(0,))
+    if type(arg) not in (int,float):
+        OpError(space,w_arg)
+    else:
+        return W_NumericArray(space,(),value=arg)
 
-w_array.unwrap_spec = [ ObjSpace, W_Root ]
+array.unwrap_spec = [ ObjSpace, W_Root ]
 



More information about the Pypy-commit mailing list