[pypy-svn] pypy improve-unwrap_spec: simpler unwrap_spec in micronumpy, mmap modules

amauryfa commits-noreply at bitbucket.org
Wed Feb 16 19:20:00 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: improve-unwrap_spec
Changeset: r42079:b2516cce1853
Date: 2011-02-16 17:27 +0100
http://bitbucket.org/pypy/pypy/changeset/b2516cce1853/

Log:	simpler unwrap_spec in micronumpy, mmap modules

diff --git a/pypy/module/micronumpy/numarray.py b/pypy/module/micronumpy/numarray.py
--- a/pypy/module/micronumpy/numarray.py
+++ b/pypy/module/micronumpy/numarray.py
@@ -1,8 +1,8 @@
 
-from pypy.interpreter.baseobjspace import ObjSpace, W_Root, Wrappable
+from pypy.interpreter.baseobjspace import Wrappable
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.typedef import TypeDef
-from pypy.interpreter.gateway import interp2app, NoneNotWrapped
+from pypy.interpreter.gateway import interp2app, unwrap_spec, NoneNotWrapped
 from pypy.rlib.debug import make_sure_not_resized
 
 class BaseNumArray(Wrappable):
@@ -16,6 +16,7 @@
         self.storage = [0] * dim
         make_sure_not_resized(self.storage)
 
+    @unwrap_spec(index=int)
     def descr_getitem(self, index):
         space = self.space
         try:
@@ -23,8 +24,8 @@
         except IndexError:
             raise OperationError(space.w_IndexError,
                                  space.wrap("list index out of range"))
-    descr_getitem.unwrap_spec = ['self', int]
 
+    @unwrap_spec(index=int, value=int)
     def descr_setitem(self, index, value):
         space = self.space
         try:
@@ -33,11 +34,9 @@
             raise OperationError(space.w_IndexError,
                                  space.wrap("list index out of range"))
         return space.w_None
-    descr_setitem.unwrap_spec = ['self', int, int]
 
     def descr_len(self):
         return self.space.wrap(len(self.storage))
-    descr_len.unwrap_spec = ['self']
 
 NumArray.typedef = TypeDef(
     'NumArray',
@@ -84,19 +83,17 @@
         indexes = self._unpack_indexes(space, w_index)
         pos = compute_pos(space, indexes, self.dim)
         return space.wrap(self.storage[pos])
-    descr_getitem.unwrap_spec = ['self', W_Root]
 
+    @unwrap_spec(value=int)
     def descr_setitem(self, w_index, value):
         space = self.space
         indexes = self._unpack_indexes(space, w_index)
         pos = compute_pos(space, indexes, self.dim)
         self.storage[pos] = value
         return space.w_None
-    descr_setitem.unwrap_spec = ['self', W_Root, int]
 
     def descr_len(self):
         return self.space.wrap(self.dim[0])
-    descr_len.unwrap_spec = ['self']
 
 MultiDimArray.typedef = TypeDef(
     'NumArray',
@@ -124,4 +121,3 @@
         return space.wrap(NumArray(space, dim[0], dtype))
     else:
         return space.wrap(MultiDimArray(space, dim, dtype))
-zeros.unwrap_spec = [ObjSpace, W_Root, W_Root]

diff --git a/pypy/module/micronumpy/ufunc.py b/pypy/module/micronumpy/ufunc.py
--- a/pypy/module/micronumpy/ufunc.py
+++ b/pypy/module/micronumpy/ufunc.py
@@ -19,4 +19,3 @@
         else:
             res.storage[i] = two
     return space.wrap(res)
-minimum.unwrap_spec = [ObjSpace, W_Root, W_Root]

diff --git a/pypy/module/mmap/interp_mmap.py b/pypy/module/mmap/interp_mmap.py
--- a/pypy/module/mmap/interp_mmap.py
+++ b/pypy/module/mmap/interp_mmap.py
@@ -1,9 +1,9 @@
 from pypy.rpython.tool import rffi_platform
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.interpreter.error import OperationError, wrap_oserror
-from pypy.interpreter.baseobjspace import W_Root, ObjSpace, Wrappable
+from pypy.interpreter.baseobjspace import Wrappable
 from pypy.interpreter.typedef import TypeDef
-from pypy.interpreter.gateway import interp2app, NoneNotWrapped
+from pypy.interpreter.gateway import interp2app, unwrap_spec, NoneNotWrapped
 from pypy.rlib import rmmap
 from pypy.rlib.rmmap import RValueError, RTypeError, ROverflowError
 import sys
@@ -18,7 +18,6 @@
         
     def close(self):
         self.mmap.close()
-    close.unwrap_spec = ['self']
 
     def read_byte(self):
         try:
@@ -26,17 +25,16 @@
         except RValueError, v:
             raise OperationError(self.space.w_ValueError,
                                  self.space.wrap(v.message))
-    read_byte.unwrap_spec = ['self']
-    
+
     def readline(self):
         return self.space.wrap(self.mmap.readline())
-    readline.unwrap_spec = ['self']
-    
+
+    @unwrap_spec(num=int)
     def read(self, num=-1):
         self.check_valid()
         return self.space.wrap(self.mmap.read(num))
-    read.unwrap_spec = ['self', int]
 
+    @unwrap_spec(tofind='bufferstr')
     def find(self, tofind, w_start=NoneNotWrapped, w_end=NoneNotWrapped):
         space = self.space
         if w_start is None:
@@ -48,8 +46,8 @@
         else:
             end = space.getindex_w(w_end, None)
         return space.wrap(self.mmap.find(tofind, start, end))
-    find.unwrap_spec = ['self', 'bufferstr', W_Root, W_Root]
 
+    @unwrap_spec(tofind='bufferstr')
     def rfind(self, tofind, w_start=NoneNotWrapped, w_end=NoneNotWrapped):
         space = self.space
         if w_start is None:
@@ -61,27 +59,25 @@
         else:
             end = space.getindex_w(w_end, None)
         return space.wrap(self.mmap.find(tofind, start, end, True))
-    rfind.unwrap_spec = ['self', 'bufferstr', W_Root, W_Root]
 
+    @unwrap_spec(pos='index', whence=int)
     def seek(self, pos, whence=0):
         try:
             self.mmap.seek(pos, whence)
         except RValueError, v:
             raise OperationError(self.space.w_ValueError,
-                                 self.space.wrap(v.message))            
-    seek.unwrap_spec = ['self', 'index', int]
-    
+                                 self.space.wrap(v.message))
+
     def tell(self):
         return self.space.wrap(self.mmap.tell())
-    tell.unwrap_spec = ['self']
-    
+
     def descr_size(self):
         try:
             return self.space.wrap(self.mmap.file_size())
         except OSError, e:
             raise mmap_error(self.space, e)
-    descr_size.unwrap_spec = ['self']
-    
+
+    @unwrap_spec(data='bufferstr')
     def write(self, data):
         self.check_writeable()
         try:
@@ -89,8 +85,8 @@
         except RValueError, v:
             raise OperationError(self.space.w_ValueError,
                                  self.space.wrap(v.message))
-    write.unwrap_spec = ['self', 'bufferstr']
-    
+
+    @unwrap_spec(byte=str)
     def write_byte(self, byte):
         try:
             self.mmap.write_byte(byte)
@@ -100,8 +96,8 @@
         except RTypeError, v:
             raise OperationError(self.space.w_TypeError,
                                  self.space.wrap(v.message))
-    write_byte.unwrap_spec = ['self', str]
 
+    @unwrap_spec(offset=int, size=int)
     def flush(self, offset=0, size=0):
         try:
             return self.space.wrap(self.mmap.flush(offset, size))
@@ -110,16 +106,16 @@
                                  self.space.wrap(v.message))
         except OSError, e:
             raise mmap_error(self.space, e)
-    flush.unwrap_spec = ['self', int, int]
-    
+
+    @unwrap_spec(dest=int, src=int, count=int)
     def move(self, dest, src, count):
         try:
             self.mmap.move(dest, src, count)
         except RValueError, v:
             raise OperationError(self.space.w_ValueError,
                                  self.space.wrap(v.message))
-    move.unwrap_spec = ['self', int, int, int]
-    
+
+    @unwrap_spec(newsize=int)
     def resize(self, newsize):
         self.check_valid()
         self.check_resizeable()
@@ -127,11 +123,9 @@
             self.mmap.resize(newsize)
         except OSError, e:
             raise mmap_error(self.space, e)
-    resize.unwrap_spec = ['self', int]
-    
+
     def __len__(self):
         return self.space.wrap(self.mmap.size)
-    __len__.unwrap_spec = ['self']
 
     def check_valid(self):
         try:
@@ -171,7 +165,6 @@
             res = "".join([self.mmap.getitem(i)
                            for i in range(start, stop, step)])
             return space.wrap(res)
-    descr_getitem.unwrap_spec = ['self', W_Root]
 
     def descr_setitem(self, w_index, w_value):
         space = self.space
@@ -194,17 +187,17 @@
             for i in range(length):
                 self.mmap.setitem(start, value[i])
                 start += step
-    descr_setitem.unwrap_spec = ['self', W_Root, W_Root]
 
     def descr_buffer(self):
         # XXX improve to work directly on the low-level address
         from pypy.interpreter.buffer import StringLikeBuffer
         space = self.space
         return space.wrap(StringLikeBuffer(space, space.wrap(self)))
-    descr_buffer.unwrap_spec = ['self']
 
 if rmmap._POSIX:
 
+    @unwrap_spec(fileno=int, length='index', flags=int,
+                 prot=int, access=int, offset='index')
     def mmap(space, w_subtype, fileno, length, flags=rmmap.MAP_SHARED,
              prot=rmmap.PROT_WRITE | rmmap.PROT_READ,
              access=rmmap._ACCESS_DEFAULT, offset=0):
@@ -222,10 +215,11 @@
         except ROverflowError, e:
             raise OperationError(space.w_OverflowError, space.wrap(e.message))
         return space.wrap(self)
-    mmap.unwrap_spec = [ObjSpace, W_Root, int, 'index', int, int, int, 'index']
 
 elif rmmap._MS_WINDOWS:
 
+    @unwrap_spec(fileno=int, length='index', tagname=str,
+                 access=int, offset='index')
     def mmap(space, w_subtype, fileno, length, tagname="",
              access=rmmap._ACCESS_DEFAULT, offset=0):
         self = space.allocate_instance(W_MMap, w_subtype)
@@ -242,7 +236,6 @@
         except ROverflowError, e:
             raise OperationError(space.w_OverflowError, space.wrap(e.message))
         return space.wrap(self)
-    mmap.unwrap_spec = [ObjSpace, W_Root, int, 'index', str, int, 'index']
 
 W_MMap.typedef = TypeDef("mmap",
     __new__ = interp2app(mmap),


More information about the Pypy-commit mailing list