[pypy-svn] pypy improve-unwrap_spec: modern unwrap_spec for _sre and _ssl

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


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

Log:	modern unwrap_spec for _sre and _ssl

diff --git a/pypy/module/_socket/interp_socket.py b/pypy/module/_socket/interp_socket.py
--- a/pypy/module/_socket/interp_socket.py
+++ b/pypy/module/_socket/interp_socket.py
@@ -1,8 +1,7 @@
 from pypy.interpreter.baseobjspace import Wrappable
 from pypy.interpreter.typedef import TypeDef, make_weakref_descr,\
      interp_attrproperty
-from pypy.interpreter.gateway import ObjSpace, W_Root, NoneNotWrapped
-from pypy.interpreter.gateway import interp2app, unwrap_spec
+from pypy.interpreter.gateway import NoneNotWrapped, interp2app, unwrap_spec
 from pypy.rlib.rarithmetic import intmask
 from pypy.rlib import rsocket
 from pypy.rlib.rsocket import RSocket, AF_INET, SOCK_STREAM

diff --git a/pypy/module/_sre/interp_sre.py b/pypy/module/_sre/interp_sre.py
--- a/pypy/module/_sre/interp_sre.py
+++ b/pypy/module/_sre/interp_sre.py
@@ -3,7 +3,7 @@
 from pypy.interpreter.typedef import GetSetProperty, TypeDef
 from pypy.interpreter.typedef import interp_attrproperty, interp_attrproperty_w
 from pypy.interpreter.typedef import make_weakref_descr
-from pypy.interpreter.gateway import interp2app, ObjSpace, W_Root
+from pypy.interpreter.gateway import interp2app, unwrap_spec
 from pypy.interpreter.error import OperationError
 from pypy.rlib.rarithmetic import intmask
 from pypy.tool.pairtype import extendabletype
@@ -16,9 +16,9 @@
 from pypy.rlib.rsre import rsre_core
 from pypy.rlib.rsre.rsre_char import MAGIC, CODESIZE, getlower, set_unicode_db
 
+ at unwrap_spec(char_ord=int, flags=int)
 def w_getlower(space, char_ord, flags):
     return space.wrap(getlower(char_ord, flags))
-w_getlower.unwrap_spec = [ObjSpace, int, int]
 
 def w_getcodesize(space):
     return space.wrap(CODESIZE)
@@ -130,16 +130,17 @@
         else:
             return self.space.w_None
 
+    @unwrap_spec(pos=int, endpos=int)
     def match_w(self, w_string, pos=0, endpos=sys.maxint):
         ctx = self.make_ctx(w_string, pos, endpos)
         return self.getmatch(ctx, matchcontext(self.space, ctx))
-    match_w.unwrap_spec = ['self', W_Root, int, int]
 
+    @unwrap_spec(pos=int, endpos=int)
     def search_w(self, w_string, pos=0, endpos=sys.maxint):
         ctx = self.make_ctx(w_string, pos, endpos)
         return self.getmatch(ctx, searchcontext(self.space, ctx))
-    search_w.unwrap_spec = ['self', W_Root, int, int]
 
+    @unwrap_spec(pos=int, endpos=int)
     def findall_w(self, w_string, pos=0, endpos=sys.maxint):
         space = self.space
         matchlist_w = []
@@ -164,16 +165,16 @@
             no_progress = (ctx.match_start == ctx.match_end)
             ctx.reset(ctx.match_end + no_progress)
         return space.newlist(matchlist_w)
-    findall_w.unwrap_spec = ['self', W_Root, int, int]
 
+    @unwrap_spec(pos=int, endpos=int)
     def finditer_w(self, w_string, pos=0, endpos=sys.maxint):
         # this also works as the implementation of the undocumented
         # scanner() method.
         ctx = self.make_ctx(w_string, pos, endpos)
         scanner = W_SRE_Scanner(self, ctx)
         return self.space.wrap(scanner)
-    finditer_w.unwrap_spec = ['self', W_Root, int, int]
 
+    @unwrap_spec(maxsplit=int)
     def split_w(self, w_string, maxsplit=0):
         space = self.space
         splitlist = []
@@ -201,18 +202,17 @@
             ctx.reset(last)
         splitlist.append(slice_w(space, ctx, last, ctx.end, space.w_None))
         return space.newlist(splitlist)
-    split_w.unwrap_spec = ['self', W_Root, int]
 
+    @unwrap_spec(count=int)
     def sub_w(self, w_repl, w_string, count=0):
         w_item, n = self.subx(w_repl, w_string, count)
         return w_item
-    sub_w.unwrap_spec = ['self', W_Root, W_Root, int]
 
+    @unwrap_spec(count=int)
     def subn_w(self, w_repl, w_string, count=0):
         w_item, n = self.subx(w_repl, w_string, count)
         space = self.space
         return space.newtuple([w_item, space.wrap(n)])
-    subn_w.unwrap_spec = ['self', W_Root, W_Root, int]
 
     def subx(self, w_ptemplate, w_string, count):
         space = self.space
@@ -287,6 +287,7 @@
         return w_item, n
 
 
+ at unwrap_spec(flags=int, groups=int)
 def SRE_Pattern__new__(space, w_subtype, w_pattern, flags, w_code,
               groups=0, w_groupindex=None, w_indexgroup=None):
     n = space.len_w(w_code)
@@ -303,8 +304,6 @@
     srepat.w_groupindex = w_groupindex
     srepat.w_indexgroup = w_indexgroup
     return w_srepat
-SRE_Pattern__new__.unwrap_spec = [ObjSpace, W_Root, W_Root, int, W_Root,
-                                  int, W_Root, W_Root]
 
 
 W_SRE_Pattern.typedef = TypeDef(
@@ -359,7 +358,6 @@
                 start, end = self.do_span(args_w[i])
                 results[i] = slice_w(space, ctx, start, end, space.w_None)
             return space.newtuple(results)
-    group_w.unwrap_spec = ['self', 'args_w']
 
     def groups_w(self, w_default=None):
         fmarks = self.flatten_marks()
@@ -542,9 +540,9 @@
 
 W_SRE_Scanner.typedef = TypeDef(
     'SRE_Scanner',
-    __iter__ = interp2app(W_SRE_Scanner.iter_w,   unwrap_spec=['self']),
-    next     = interp2app(W_SRE_Scanner.next_w,   unwrap_spec=['self']),
-    match    = interp2app(W_SRE_Scanner.match_w,  unwrap_spec=['self']),
-    search   = interp2app(W_SRE_Scanner.search_w, unwrap_spec=['self']),
+    __iter__ = interp2app(W_SRE_Scanner.iter_w),
+    next     = interp2app(W_SRE_Scanner.next_w),
+    match    = interp2app(W_SRE_Scanner.match_w),
+    search   = interp2app(W_SRE_Scanner.search_w),
     pattern  = interp_attrproperty('srepat', W_SRE_Scanner),
 )

diff --git a/pypy/module/_ssl/interp_ssl.py b/pypy/module/_ssl/interp_ssl.py
--- a/pypy/module/_ssl/interp_ssl.py
+++ b/pypy/module/_ssl/interp_ssl.py
@@ -2,7 +2,7 @@
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.baseobjspace import W_Root, ObjSpace, Wrappable
 from pypy.interpreter.typedef import TypeDef
-from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.gateway import interp2app, unwrap_spec
 
 from pypy.rlib import rpoll, rsocket
 from pypy.rlib.ropenssl import *
@@ -78,6 +78,7 @@
 
 if HAVE_OPENSSL_RAND:
     # helper routines for seeding the SSL PRNG
+    @unwrap_spec(string=str, entropy=float)
     def RAND_add(space, string, entropy):
         """RAND_add(string, entropy)
 
@@ -90,7 +91,6 @@
             libssl_RAND_add(buf, len(string), entropy)
         finally:
             rffi.free_charp(buf)
-    RAND_add.unwrap_spec = [ObjSpace, str, float]
 
     def RAND_status(space):
         """RAND_status() -> 0 or 1
@@ -101,8 +101,8 @@
 
         res = libssl_RAND_status()
         return space.wrap(res)
-    RAND_status.unwrap_spec = [ObjSpace]
 
+    @unwrap_spec(path=str)
     def RAND_egd(space, path):
         """RAND_egd(path) -> bytes
 
@@ -120,7 +120,6 @@
             msg += " enough data to seed the PRNG"
             raise ssl_error(space, msg)
         return space.wrap(bytes)
-    RAND_egd.unwrap_spec = [ObjSpace, str]
 
 class SSLObject(Wrappable):
     def __init__(self, space):
@@ -137,11 +136,9 @@
     
     def server(self):
         return self.space.wrap(rffi.charp2str(self._server))
-    server.unwrap_spec = ['self']
     
     def issuer(self):
         return self.space.wrap(rffi.charp2str(self._issuer))
-    issuer.unwrap_spec = ['self']
     
     def __del__(self):
         if self.peer_cert:
@@ -153,6 +150,7 @@
         lltype.free(self._server, flavor='raw')
         lltype.free(self._issuer, flavor='raw')
     
+    @unwrap_spec(data='bufferstr')
     def write(self, data):
         """write(s) -> len
 
@@ -201,8 +199,8 @@
             return self.space.wrap(num_bytes)
         else:
             raise _ssl_seterror(self.space, self, num_bytes)
-    write.unwrap_spec = ['self', 'bufferstr']
-    
+
+    @unwrap_spec(num_bytes=int)
     def read(self, num_bytes=1024):
         """read([len]) -> string
 
@@ -256,7 +254,6 @@
         result = rffi.str_from_buffer(raw_buf, gc_buf, num_bytes, count)
         rffi.keep_buffer_alive_until_here(raw_buf, gc_buf)
         return self.space.wrap(result)
-    read.unwrap_spec = ['self', int]
 
     def _refresh_nonblocking(self, space):
         # just in case the blocking state of the socket has been changed
@@ -375,17 +372,12 @@
 
 
 SSLObject.typedef = TypeDef("SSLObject",
-    server = interp2app(SSLObject.server,
-        unwrap_spec=SSLObject.server.unwrap_spec),
-    issuer = interp2app(SSLObject.issuer,
-        unwrap_spec=SSLObject.issuer.unwrap_spec),
-    write = interp2app(SSLObject.write,
-        unwrap_spec=SSLObject.write.unwrap_spec),
-    read = interp2app(SSLObject.read, unwrap_spec=SSLObject.read.unwrap_spec),
-    do_handshake=interp2app(SSLObject.do_handshake,
-                            unwrap_spec=['self', ObjSpace]),
-    shutdown=interp2app(SSLObject.shutdown,
-                        unwrap_spec=['self', ObjSpace]),
+    server = interp2app(SSLObject.server),
+    issuer = interp2app(SSLObject.issuer),
+    write = interp2app(SSLObject.write),
+    read = interp2app(SSLObject.read),
+    do_handshake=interp2app(SSLObject.do_handshake),
+    shutdown=interp2app(SSLObject.shutdown),
 )
 
 
@@ -452,7 +444,6 @@
 
     ss.w_socket = w_sock
     return ss
-new_sslobject.unwrap_spec = [ObjSpace, W_Root, str, str]
 
 def check_socket_and_wait_for_timeout(space, w_sock, writing):
     """If the socket has a timeout, do a select()/poll() on the socket.
@@ -553,12 +544,11 @@
     return ssl_error(space, errstr, errval)
 
 
+ at unwrap_spec(side=int, cert_mode=int, protocol=int)
 def sslwrap(space, w_socket, side, w_key_file=None, w_cert_file=None,
             cert_mode=PY_SSL_CERT_NONE, protocol=PY_SSL_VERSION_SSL23,
             w_cacerts_file=None, w_cipher=None):
     """sslwrap(socket, side, [keyfile, certfile]) -> sslobject"""
     return space.wrap(new_sslobject(
         space, w_socket, side, w_key_file, w_cert_file))
-sslwrap.unwrap_spec = [ObjSpace, W_Root, int, W_Root, W_Root,
-                       int, int, W_Root, W_Root]
 


More information about the Pypy-commit mailing list