[pypy-svn] pypy improve-unwrap_spec: simplify _rawffi

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


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: improve-unwrap_spec
Changeset: r42063:023eba0c4a01
Date: 2011-02-16 13:23 +0100
http://bitbucket.org/pypy/pypy/changeset/023eba0c4a01/

Log:	simplify _rawffi

diff --git a/pypy/module/_rawffi/interp_rawffi.py b/pypy/module/_rawffi/interp_rawffi.py
--- a/pypy/module/_rawffi/interp_rawffi.py
+++ b/pypy/module/_rawffi/interp_rawffi.py
@@ -1,8 +1,7 @@
 import sys
-from pypy.interpreter.baseobjspace import W_Root, ObjSpace, Wrappable, \
-     Arguments
+from pypy.interpreter.baseobjspace import Wrappable
 from pypy.interpreter.error import OperationError, wrap_oserror, operationerrfmt
-from pypy.interpreter.gateway import interp2app, NoneNotWrapped
+from pypy.interpreter.gateway import interp2app, NoneNotWrapped, unwrap_spec
 from pypy.interpreter.typedef import TypeDef, GetSetProperty
 
 from pypy.rlib.clibffi import *
@@ -142,6 +141,7 @@
         self.w_cache = space.newdict()
         self.space = space
 
+    @unwrap_spec(flags=int)
     def ptr(self, space, w_name, w_argtypes, w_restype, flags=FUNCFLAG_CDECL):
         """ Get a pointer for function name with provided argtypes
         and restype
@@ -193,8 +193,8 @@
         w_funcptr = W_FuncPtr(space, ptr, argshapes, resshape)
         space.setitem(self.w_cache, w_key, w_funcptr)
         return w_funcptr
-    ptr.unwrap_spec = ['self', ObjSpace, W_Root, W_Root, W_Root, int]
 
+    @unwrap_spec(name=str)
     def getaddressindll(self, space, name):
         try:
             address_as_uint = rffi.cast(lltype.Unsigned,
@@ -203,8 +203,8 @@
             raise operationerrfmt(space.w_ValueError,
                                   "Cannot find symbol %s", name)
         return space.wrap(address_as_uint)
-    getaddressindll.unwrap_spec = ['self', ObjSpace, str]
 
+ at unwrap_spec(name='str_or_None')
 def descr_new_cdll(space, w_type, name):
     try:
         cdll = CDLL(name)
@@ -214,7 +214,6 @@
     except OSError, e:
         raise wrap_oserror(space, e)
     return space.wrap(W_CDLL(space, name, cdll))
-descr_new_cdll.unwrap_spec = [ObjSpace, W_Root, 'str_or_None']
 
 W_CDLL.typedef = TypeDef(
     'CDLL',
@@ -251,10 +250,10 @@
     def get_basic_ffi_type(self):
         raise NotImplementedError
 
+    @unwrap_spec(n=int)
     def descr_size_alignment(self, space, n=1):
         return space.newtuple([space.wrap(self.size * n),
                                space.wrap(self.alignment)])
-    descr_size_alignment.unwrap_spec = ['self', ObjSpace, int]
     
 
 class W_DataInstance(Wrappable):
@@ -276,13 +275,11 @@
         array = ARRAY_OF_PTRS.allocate(space, 1)
         array.setitem(space, 0, space.wrap(self))
         return space.wrap(array)
-    byptr.unwrap_spec = ['self', ObjSpace]
 
     def free(self, space):
         if not self.ll_buffer:
             raise segfault_exception(space, "freeing NULL pointer")
         self._free()
-    free.unwrap_spec = ['self', ObjSpace]
 
     def _free(self):
         if tracker.DO_TRACING:
@@ -294,7 +291,6 @@
     def descr_buffer(self, space):
         from pypy.module._rawffi.buffer import RawFFIBuffer
         return space.wrap(RawFFIBuffer(self))
-    descr_buffer.unwrap_spec = ['self', ObjSpace]
 
     def getrawsize(self):
         raise NotImplementedError("abstract base class")
@@ -391,7 +387,6 @@
             #     hence our testing is not performing that well
             del tracker.alloced[rffi.cast(lltype.Signed, array.ll_buffer)]
         return space.wrap(array)
-    byptr.unwrap_spec = ['self', ObjSpace]
 
     def call(self, space, args_w):
         from pypy.module._rawffi.array import W_ArrayInstance
@@ -445,8 +440,8 @@
                 return space.w_None
         except StackCheckError, e:
             raise OperationError(space.w_ValueError, space.wrap(e.message))
-    call.unwrap_spec = ['self', ObjSpace, 'args_w']
 
+ at unwrap_spec(addr=r_uint, flags=int)
 def descr_new_funcptr(space, w_tp, addr, w_args, w_res, flags=FUNCFLAG_CDECL):
     argshapes = unpack_argshapes(space, w_args)
     resshape = unpack_resshape(space, w_res)
@@ -455,7 +450,6 @@
     ptr = RawFuncPtr('???', ffi_args, ffi_res, rffi.cast(rffi.VOIDP, addr),
                      flags)
     return space.wrap(W_FuncPtr(space, ptr, argshapes, resshape))
-descr_new_funcptr.unwrap_spec = [ObjSpace, W_Root, r_uint, W_Root, W_Root, int]
 
 W_FuncPtr.typedef = TypeDef(
     'FuncPtr',
@@ -467,6 +461,7 @@
 W_FuncPtr.typedef.acceptable_as_base_class = False
 
 def _create_new_accessor(func_name, name):
+    @unwrap_spec(tp_letter=str)
     def accessor(space, tp_letter):
         if len(tp_letter) != 1:
             raise OperationError(space.w_ValueError, space.wrap(
@@ -477,50 +472,49 @@
         except KeyError:
             raise operationerrfmt(space.w_ValueError,
                         "Unknown type specification %s", tp_letter)
-    accessor.unwrap_spec = [ObjSpace, str]
     return func_with_new_name(accessor, func_name)
 
 sizeof = _create_new_accessor('sizeof', 'c_size')
 alignment = _create_new_accessor('alignment', 'c_alignment')
 
+ at unwrap_spec(address=r_uint, maxlength=int)
 def charp2string(space, address, maxlength=sys.maxint):
     if address == 0:
         return space.w_None
     s = rffi.charp2strn(rffi.cast(rffi.CCHARP, address), maxlength)
     return space.wrap(s)
-charp2string.unwrap_spec = [ObjSpace, r_uint, int]
 
+ at unwrap_spec(address=r_uint, maxlength=int)
 def wcharp2unicode(space, address, maxlength=sys.maxint):
     if address == 0:
         return space.w_None
     s = rffi.wcharp2unicoden(rffi.cast(rffi.CWCHARP, address), maxlength)
     return space.wrap(s)
-wcharp2unicode.unwrap_spec = [ObjSpace, r_uint, int]
 
+ at unwrap_spec(address=r_uint, maxlength=int)
 def charp2rawstring(space, address, maxlength=-1):
     if maxlength == -1:
         return charp2string(space, address)
     s = rffi.charpsize2str(rffi.cast(rffi.CCHARP, address), maxlength)
     return space.wrap(s)
-charp2rawstring.unwrap_spec = [ObjSpace, r_uint, int]
 
+ at unwrap_spec(address=r_uint, maxlength=int)
 def wcharp2rawunicode(space, address, maxlength=-1):
     if maxlength == -1:
         return wcharp2unicode(space, address)
     s = rffi.wcharpsize2unicode(rffi.cast(rffi.CWCHARP, address), maxlength)
     return space.wrap(s)
-wcharp2rawunicode.unwrap_spec = [ObjSpace, r_uint, int]
 
 if _MS_WINDOWS:
+    @unwrap_spec(code=int)
     def FormatError(space, code):
         return space.wrap(rwin32.FormatError(code))
-    FormatError.unwrap_spec = [ObjSpace, int]
 
+    @unwrap_spec(hresult=int)
     def check_HRESULT(space, hresult):
         if rwin32.FAILED(hresult):
             raise OperationError(space.w_WindowsError, space.wrap(hresult))
         return space.wrap(hresult)
-    check_HRESULT.unwrap_spec = [ObjSpace, int]
 
 def get_libc(space):
     name = get_libc_name()

diff --git a/pypy/module/_rawffi/callback.py b/pypy/module/_rawffi/callback.py
--- a/pypy/module/_rawffi/callback.py
+++ b/pypy/module/_rawffi/callback.py
@@ -1,7 +1,5 @@
 
-from pypy.interpreter.baseobjspace import W_Root, ObjSpace, Wrappable,\
-     Arguments
-from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.gateway import interp2app, unwrap_spec
 from pypy.interpreter.typedef import TypeDef, GetSetProperty
 from pypy.rpython.lltypesystem import lltype, rffi
 from pypy.module._rawffi.array import get_elem, push_elem
@@ -93,13 +91,11 @@
             addr = rffi.cast(lltype.Signed, self.ll_callback.ll_closure)
             tracker.trace_free(addr)
         del self.global_counter.CallbackPtr_by_number[self.number]
-    free.unwrap_spec = ['self']
 
+ at unwrap_spec(flags=int)
 def descr_new_callbackptr(space, w_type, w_callable, w_args, w_result,
                           flags=FUNCFLAG_CDECL):
     return W_CallbackPtr(space, w_callable, w_args, w_result, flags)
-descr_new_callbackptr.unwrap_spec = [ObjSpace, W_Root, W_Root, W_Root, W_Root,
-                                     int]
 
 W_CallbackPtr.typedef = TypeDef(
     'CallbackPtr',

diff --git a/pypy/module/_rawffi/array.py b/pypy/module/_rawffi/array.py
--- a/pypy/module/_rawffi/array.py
+++ b/pypy/module/_rawffi/array.py
@@ -3,9 +3,7 @@
 to app-level with apropriate interface
 """
 
-from pypy.interpreter.baseobjspace import W_Root, ObjSpace, Wrappable,\
-     Arguments
-from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.gateway import interp2app, unwrap_spec
 from pypy.interpreter.typedef import TypeDef, GetSetProperty, interp_attrproperty
 from pypy.rpython.lltypesystem import lltype, rffi
 from pypy.interpreter.error import OperationError
@@ -47,6 +45,7 @@
     def get_basic_ffi_type(self):
         return self.basicffitype
 
+    @unwrap_spec(length=int, autofree=bool)
     def descr_call(self, space, length, w_items=None, autofree=False):
         result = self.allocate(space, length, autofree)
         if not space.is_w(w_items, space.w_None):
@@ -66,11 +65,10 @@
         return space.wrap("<_rawffi.Array '%s' (%d, %d)>" % (self.itemcode,
                                                              self.size,
                                                              self.alignment))
-    descr_repr.unwrap_spec = ['self', ObjSpace]
 
+    @unwrap_spec(address=r_uint, length=int)
     def fromaddress(self, space, address, length):
         return space.wrap(W_ArrayInstance(space, self, length, address))
-    fromaddress.unwrap_spec = ['self', ObjSpace, r_uint, int]
 
 PRIMITIVE_ARRAY_TYPES = {}
 for _code in TYPEMAP:
@@ -84,10 +82,8 @@
 
 W_Array.typedef = TypeDef(
     'Array',
-    __new__  = interp2app(descr_new_array,
-                          unwrap_spec=[ObjSpace, W_Root, W_Root]),
-    __call__ = interp2app(W_Array.descr_call,
-                          unwrap_spec=['self', ObjSpace, int, W_Root, int]),
+    __new__  = interp2app(descr_new_array),
+    __call__ = interp2app(W_Array.descr_call),
     __repr__ = interp2app(W_Array.descr_repr),
     fromaddress = interp2app(W_Array.fromaddress),
     size_alignment = interp2app(W_Array.descr_size_alignment)
@@ -113,7 +109,6 @@
         addr = rffi.cast(lltype.Unsigned, self.ll_buffer)
         return space.wrap("<_rawffi array %x of length %d>" % (addr,
                                                                self.length))
-    descr_repr.unwrap_spec = ['self', ObjSpace]
 
     # This only allows non-negative indexes.  Arrays of shape 'c' also
     # support simple slices.
@@ -135,7 +130,6 @@
             self.setslice(space, w_index, w_value)
         else:
             self.setitem(space, num, w_value)
-    descr_setitem.unwrap_spec = ['self', ObjSpace, W_Root, W_Root]
 
     def getitem(self, space, num):
         if not self.ll_buffer:
@@ -154,17 +148,15 @@
             return self.getslice(space, w_index)
         else:
             return self.getitem(space, num)
-    descr_getitem.unwrap_spec = ['self', ObjSpace, W_Root]
 
     def getlength(self, space):
         return space.wrap(self.length)
-    getlength.unwrap_spec = ['self', ObjSpace]
 
+    @unwrap_spec(num=int)
     def descr_itemaddress(self, space, num):
         itemsize = self.shape.size
         ptr = rffi.ptradd(self.ll_buffer, itemsize * num)
         return space.wrap(rffi.cast(lltype.Unsigned, ptr))
-    descr_itemaddress.unwrap_spec = ['self', ObjSpace, int]
 
     def getrawsize(self):
         itemsize = self.shape.size

diff --git a/pypy/module/_rawffi/tracker.py b/pypy/module/_rawffi/tracker.py
--- a/pypy/module/_rawffi/tracker.py
+++ b/pypy/module/_rawffi/tracker.py
@@ -2,8 +2,6 @@
 """ The file that keeps track about freed/kept-alive objects allocated
 by _rawffi. Used for debugging ctypes
 """
-from pypy.interpreter.baseobjspace import W_Root, ObjSpace, Wrappable, \
-     Arguments
 
 class Tracker(object):
     DO_TRACING = True
@@ -23,7 +21,6 @@
 
 def num_of_allocated_objects(space):
     return space.wrap(len(tracker.alloced))
-num_of_allocated_objects.unwrap_spec = [ObjSpace]
 
 def print_alloced_objects(space):
     xxx

diff --git a/pypy/module/_rawffi/structure.py b/pypy/module/_rawffi/structure.py
--- a/pypy/module/_rawffi/structure.py
+++ b/pypy/module/_rawffi/structure.py
@@ -3,10 +3,8 @@
 to app-level with apropriate interface
 """
 
-from pypy.interpreter.baseobjspace import W_Root, Wrappable
-from pypy.interpreter.gateway import interp2app, ObjSpace
+from pypy.interpreter.gateway import interp2app, unwrap_spec
 from pypy.interpreter.typedef import interp_attrproperty
-from pypy.interpreter.argument import Arguments
 from pypy.interpreter.typedef import TypeDef, GetSetProperty
 from pypy.rpython.lltypesystem import lltype, rffi
 from pypy.interpreter.error import OperationError, operationerrfmt
@@ -169,33 +167,32 @@
             raise operationerrfmt(space.w_AttributeError,
                 "C Structure has no attribute %s", attr)
 
+    @unwrap_spec(autofree=bool)
     def descr_call(self, space, autofree=False):
         return space.wrap(self.allocate(space, 1, autofree))
-    descr_call.unwrap_spec = ['self', ObjSpace, int]
 
     def descr_repr(self, space):
         fieldnames = ' '.join(["'%s'" % name for name, _, _ in self.fields])
         return space.wrap("<_rawffi.Structure %s (%d, %d)>" % (fieldnames,
                                                                self.size,
                                                                self.alignment))
-    descr_repr.unwrap_spec = ['self', ObjSpace]
 
+    @unwrap_spec(address=r_uint)
     def fromaddress(self, space, address):
         return space.wrap(W_StructureInstance(space, self, address))
-    fromaddress.unwrap_spec = ['self', ObjSpace, r_uint]
 
+    @unwrap_spec(attr=str)
     def descr_fieldoffset(self, space, attr):
         index = self.getindex(space, attr)
         return space.wrap(self.ll_positions[index])
-    descr_fieldoffset.unwrap_spec = ['self', ObjSpace, str]
 
+    @unwrap_spec(attr=str)
     def descr_fieldsize(self, space, attr):
         index = self.getindex(space, attr)
         if self.ll_bitsizes and index < len(self.ll_bitsizes):
             return space.wrap(self.ll_bitsizes[index])
         else:
             return space.wrap(self.fields[index][1].size)
-    descr_fieldsize.unwrap_spec = ['self', ObjSpace, str]
 
     # get the corresponding ffi_type
     ffi_struct = lltype.nullptr(clibffi.FFI_STRUCT_P.TO)
@@ -227,21 +224,20 @@
     
 
 
-def descr_new_structure(space, w_type, w_shapeinfo, union=0, pack=0):
+ at unwrap_spec(union=bool, pack=int)
+def descr_new_structure(space, w_type, w_shapeinfo, union=False, pack=0):
     if pack < 0:
         raise OperationError(space.w_ValueError, space.wrap(
             "_pack_ must be a non-negative integer"))
 
-    is_union = bool(union)
     if space.is_true(space.isinstance(w_shapeinfo, space.w_tuple)):
         w_size, w_alignment = space.fixedview(w_shapeinfo, expected_length=2)
         S = W_Structure(space, None, space.int_w(w_size),
-                                     space.int_w(w_alignment), is_union)
+                                     space.int_w(w_alignment), union)
     else:
         fields = unpack_fields(space, w_shapeinfo)
-        S = W_Structure(space, fields, 0, 0, is_union, pack)
+        S = W_Structure(space, fields, 0, 0, union, pack)
     return space.wrap(S)
-descr_new_structure.unwrap_spec = [ObjSpace, W_Root, W_Root, int, int]
 
 W_Structure.typedef = TypeDef(
     'Structure',
@@ -321,29 +317,28 @@
     def descr_repr(self, space):
         addr = rffi.cast(lltype.Unsigned, self.ll_buffer)
         return space.wrap("<_rawffi struct %x>" % (addr,))
-    descr_repr.unwrap_spec = ['self', ObjSpace]
 
+    @unwrap_spec(attr=str)
     def getattr(self, space, attr):
         if not self.ll_buffer:
             raise segfault_exception(space, "accessing NULL pointer")
         i = self.shape.getindex(space, attr)
         _, tp, _ = self.shape.fields[i]
         return wrap_value(space, cast_pos, self, i, tp.itemcode)
-    getattr.unwrap_spec = ['self', ObjSpace, str]
 
+    @unwrap_spec(attr=str)
     def setattr(self, space, attr, w_value):
         if not self.ll_buffer:
             raise segfault_exception(space, "accessing NULL pointer")
         i = self.shape.getindex(space, attr)
         _, tp, _ = self.shape.fields[i]
         unwrap_value(space, push_field, self, i, tp.itemcode, w_value)
-    setattr.unwrap_spec = ['self', ObjSpace, str, W_Root]
 
+    @unwrap_spec(attr=str)
     def descr_fieldaddress(self, space, attr):
         i = self.shape.getindex(space, attr)
         ptr = rffi.ptradd(self.ll_buffer, self.shape.ll_positions[i])
         return space.wrap(rffi.cast(lltype.Unsigned, ptr))
-    descr_fieldaddress.unwrap_spec = ['self', ObjSpace, str]
 
     def getrawsize(self):
         return self.shape.size


More information about the Pypy-commit mailing list