[pypy-svn] pypy improve-unwrap_spec: modern unwrap_spec in _socket module

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


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

Log:	modern unwrap_spec in _socket module

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
@@ -2,7 +2,7 @@
 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
+from pypy.interpreter.gateway import interp2app, unwrap_spec
 from pypy.rlib.rarithmetic import intmask
 from pypy.rlib import rsocket
 from pypy.rlib.rsocket import RSocket, AF_INET, SOCK_STREAM
@@ -35,7 +35,6 @@
                                    addr.as_object(sock.fd, space)])
         except SocketError, e:
             raise converted_error(space, e)
-    accept_w.unwrap_spec = ['self', ObjSpace]
 
     def bind_w(self, space, w_addr):
         """bind(address)
@@ -48,7 +47,6 @@
             self.bind(self.addr_from_object(space, w_addr))
         except SocketError, e:
             raise converted_error(space, e)
-    bind_w.unwrap_spec = ['self', ObjSpace, W_Root]
 
     def close_w(self, space):
         """close()
@@ -59,7 +57,6 @@
             self.close()
         except SocketError, e:
             raise converted_error(space, e)
-    close_w.unwrap_spec = ['self', ObjSpace]
 
     def connect_w(self, space, w_addr):
         """connect(address)
@@ -74,7 +71,6 @@
         except TypeError, e:
             raise OperationError(space.w_TypeError,
                                  space.wrap(str(e)))
-    connect_w.unwrap_spec = ['self', ObjSpace, W_Root]
 
     def connect_ex_w(self, space, w_addr):
         """connect_ex(address) -> errno
@@ -88,14 +84,12 @@
             raise converted_error(space, e)
         error = self.connect_ex(addr)
         return space.wrap(error)
-    connect_ex_w.unwrap_spec = ['self', ObjSpace, W_Root]
 
     def dup_w(self, space):
         try:
             return self.dup(W_RSocket)
         except SocketError, e:
             raise converted_error(space, e)
-    dup_w.unwrap_spec = ['self', ObjSpace]
     
     def fileno_w(self, space):
         """fileno() -> integer
@@ -103,7 +97,6 @@
         Return the integer file descriptor of the socket.
         """
         return space.wrap(intmask(self.fd))
-    fileno_w.unwrap_spec = ['self', ObjSpace]
 
     def getpeername_w(self, space):
         """getpeername() -> address info
@@ -116,7 +109,6 @@
             return addr.as_object(self.fd, space)
         except SocketError, e:
             raise converted_error(space, e)
-    getpeername_w.unwrap_spec = ['self', ObjSpace]
 
     def getsockname_w(self, space):
         """getsockname() -> address info
@@ -129,8 +121,8 @@
             return addr.as_object(self.fd, space)
         except SocketError, e:
             raise converted_error(space, e)
-    getsockname_w.unwrap_spec = ['self', ObjSpace]
 
+    @unwrap_spec(level=int, optname=int)
     def getsockopt_w(self, space, level, optname, w_buflen=NoneNotWrapped):
         """getsockopt(level, option[, buffersize]) -> value
 
@@ -145,7 +137,6 @@
                 raise converted_error(space, e)
         buflen = space.int_w(w_buflen)
         return space.wrap(self.getsockopt(level, optname, buflen))
-    getsockopt_w.unwrap_spec = ['self', ObjSpace, int, int, W_Root]
 
     def gettimeout_w(self, space):
         """gettimeout() -> timeout
@@ -157,8 +148,8 @@
         if timeout < 0.0:
             return space.w_None
         return space.wrap(timeout)
-    gettimeout_w.unwrap_spec = ['self', ObjSpace]
-    
+
+    @unwrap_spec(backlog=int)
     def listen_w(self, space, backlog):
         """listen(backlog)
 
@@ -170,7 +161,6 @@
             self.listen(backlog)
         except SocketError, e:
             raise converted_error(space, e)
-    listen_w.unwrap_spec = ['self', ObjSpace, int]
 
     def makefile_w(self, space, w_mode="r", w_buffsize=-1):
         """makefile([mode[, buffersize]]) -> file object
@@ -179,10 +169,8 @@
         The mode and buffersize arguments are as for the built-in open() function.
         """
         return app_makefile(space, self, w_mode, w_buffsize)
-    makefile_w.unwrap_spec = ['self', ObjSpace, W_Root, W_Root]
-        
-        
-                   
+
+    @unwrap_spec(buffersize='nonnegint', flags=int)
     def recv_w(self, space, buffersize, flags=0):
         """recv(buffersize[, flags]) -> data
 
@@ -196,8 +184,8 @@
         except SocketError, e:
             raise converted_error(space, e)
         return space.wrap(data)
-    recv_w.unwrap_spec = ['self', ObjSpace, 'nonnegint', int]
 
+    @unwrap_spec(buffersize='nonnegint', flags=int)
     def recvfrom_w(self, space, buffersize, flags=0):
         """recvfrom(buffersize[, flags]) -> (data, address info)
 
@@ -212,8 +200,8 @@
             return space.newtuple([space.wrap(data), w_addr])
         except SocketError, e:
             raise converted_error(space, e)
-    recvfrom_w.unwrap_spec = ['self', ObjSpace, 'nonnegint', int]
 
+    @unwrap_spec(data='bufferstr', flags=int)
     def send_w(self, space, data, flags=0):
         """send(data[, flags]) -> count
 
@@ -226,8 +214,8 @@
         except SocketError, e:
             raise converted_error(space, e)
         return space.wrap(count)
-    send_w.unwrap_spec = ['self', ObjSpace, 'bufferstr', int]
 
+    @unwrap_spec(data='bufferstr', flags=int)
     def sendall_w(self, space, data, flags=0):
         """sendall(data[, flags])
 
@@ -240,8 +228,8 @@
             count = self.sendall(data, flags, SignalChecker(space))
         except SocketError, e:
             raise converted_error(space, e)
-    sendall_w.unwrap_spec = ['self', ObjSpace, 'bufferstr', int]
 
+    @unwrap_spec(data='bufferstr')
     def sendto_w(self, space, data, w_param2, w_param3=NoneNotWrapped):
         """sendto(data[, flags], address) -> count
 
@@ -262,18 +250,18 @@
         except SocketError, e:
             raise converted_error(space, e)
         return space.wrap(count)
-    sendto_w.unwrap_spec = ['self', ObjSpace, 'bufferstr', W_Root, W_Root]
 
-    def setblocking_w(self, space, flag):
+    @unwrap_spec(flag=bool)
+    def setblocking_w(self, flag):
         """setblocking(flag)
 
         Set the socket to blocking (flag is true) or non-blocking (false).
         setblocking(True) is equivalent to settimeout(None);
         setblocking(False) is equivalent to settimeout(0.0).
         """
-        self.setblocking(bool(flag))
-    setblocking_w.unwrap_spec = ['self', ObjSpace, int]
+        self.setblocking(flag)
 
+    @unwrap_spec(level=int, optname=int)
     def setsockopt_w(self, space, level, optname, w_optval):
         """setsockopt(level, option, value)
 
@@ -293,9 +281,7 @@
             self.setsockopt_int(level, optname, optval)
         except SocketError, e:
             raise converted_error(space, e)
-            
-    setsockopt_w.unwrap_spec = ['self', ObjSpace, int, int, W_Root]
-    
+
     def settimeout_w(self, space, w_timeout):
         """settimeout(timeout)
 
@@ -312,8 +298,8 @@
                 raise OperationError(space.w_ValueError,
                                      space.wrap('Timeout value out of range'))
         self.settimeout(timeout)
-    settimeout_w.unwrap_spec = ['self', ObjSpace, W_Root]
 
+    @unwrap_spec(nbytes=int, flags=int)
     def recv_into_w(self, space, w_buffer, nbytes=0, flags=0):
         rwbuffer = space.rwbuffer_w(w_buffer)
         lgt = rwbuffer.getlength()
@@ -324,8 +310,7 @@
         except SocketError, e:
             raise converted_error(space, e)
 
-    recv_into_w.unwrap_spec = ['self', ObjSpace, W_Root, int, int]
-
+    @unwrap_spec(nbytes=int, flags=int)
     def recvfrom_into_w(self, space, w_buffer, nbytes=0, flags=0):
         rwbuffer = space.rwbuffer_w(w_buffer)
         lgt = rwbuffer.getlength()
@@ -340,8 +325,8 @@
             return space.newtuple([space.wrap(readlgt), w_addr])
         except SocketError, e:
             raise converted_error(space, e)        
-    recvfrom_into_w.unwrap_spec = ['self', ObjSpace, W_Root, int, int]
 
+    @unwrap_spec(cmd=int)
     def ioctl_w(self, space, cmd, w_option):
         from pypy.rpython.lltypesystem import rffi, lltype
         from pypy.rlib import rwin32
@@ -381,8 +366,8 @@
             return space.wrap(recv_ptr[0])
         finally:
             lltype.free(recv_ptr, flavor='raw')
-    ioctl_w.unwrap_spec = ['self', ObjSpace, int, W_Root]
 
+    @unwrap_spec(how=int)
     def shutdown_w(self, space, how):
         """shutdown(flag)
 
@@ -394,7 +379,6 @@
             self.shutdown(how)
         except SocketError, e:
             raise converted_error(space, e)
-    shutdown_w.unwrap_spec = ['self', ObjSpace, int]
 
     #------------------------------------------------------------
     # Support functions for socket._socketobject
@@ -406,7 +390,6 @@
         Intended only to be used by socket._socketobject
         """
         self.usecount += 1
-    _reuse_w.unwrap_spec = ['self']
 
     def _drop_w(self, space):
         """_drop()
@@ -419,7 +402,6 @@
         if self.usecount > 0:
             return
         self.close_w(space)
-    _drop_w.unwrap_spec = ['self', ObjSpace]
 
 app_makefile = gateway.applevel(r'''
 def makefile(self, mode="r", buffersize=-1):
@@ -433,6 +415,7 @@
     return os.fdopen(newfd, mode, buffersize)
 ''', filename =__file__).interphook('makefile')
 
+ at unwrap_spec(family=int, type=int, proto=int)
 def newsocket(space, w_subtype, family=AF_INET,
               type=SOCK_STREAM, proto=0):
     # XXX If we want to support subclassing the socket type we will need
@@ -445,8 +428,7 @@
     except SocketError, e:
         raise converted_error(space, e)
     return space.wrap(sock)
-descr_socket_new = interp2app(newsocket,
-                               unwrap_spec=[ObjSpace, W_Root, int, int, int])
+descr_socket_new = interp2app(newsocket)
 
 # ____________________________________________________________
 # Error handling
@@ -480,9 +462,7 @@
 socketmethods = {}
 for methodname in socketmethodnames:
     method = getattr(W_RSocket, methodname + '_w')
-    assert hasattr(method,'unwrap_spec'), methodname
-    assert method.im_func.func_code.co_argcount == len(method.unwrap_spec), methodname
-    socketmethods[methodname] = interp2app(method, unwrap_spec=method.unwrap_spec)
+    socketmethods[methodname] = interp2app(method)
 
 W_RSocket.typedef = TypeDef("_socket.socket",
     __doc__ = """\

diff --git a/pypy/module/_socket/interp_func.py b/pypy/module/_socket/interp_func.py
--- a/pypy/module/_socket/interp_func.py
+++ b/pypy/module/_socket/interp_func.py
@@ -1,4 +1,4 @@
-from pypy.interpreter.gateway import ObjSpace, W_Root, NoneNotWrapped
+from pypy.interpreter.gateway import NoneNotWrapped, unwrap_spec
 from pypy.module._socket.interp_socket import converted_error, W_RSocket
 from pypy.rlib import rsocket
 from pypy.rlib.rsocket import SocketError
@@ -15,20 +15,19 @@
     except SocketError, e:
         raise converted_error(space, e)
     return space.wrap(res)
-gethostname.unwrap_spec = [ObjSpace]
 
-def gethostbyname(space, hostname):
+ at unwrap_spec(host=str)
+def gethostbyname(space, host):
     """gethostbyname(host) -> address
 
     Return the IP address (a string of the form '255.255.255.255') for a host.
     """
     try:
-        addr = rsocket.gethostbyname(hostname)
+        addr = rsocket.gethostbyname(host)
         ip = addr.get_host()
     except SocketError, e:
         raise converted_error(space, e)
     return space.wrap(ip)
-gethostbyname.unwrap_spec = [ObjSpace, str]
 
 def common_wrapgethost(space, (name, aliases, address_list)):
     aliases = [space.wrap(alias) for alias in aliases]
@@ -37,6 +36,7 @@
                            space.newlist(aliases),
                            space.newlist(address_list)])
 
+ at unwrap_spec(host=str)
 def gethostbyname_ex(space, host):
     """gethostbyname_ex(host) -> (name, aliaslist, addresslist)
 
@@ -48,8 +48,8 @@
     except SocketError, e:
         raise converted_error(space, e)
     return common_wrapgethost(space, res)
-gethostbyname_ex.unwrap_spec = [ObjSpace, str]
 
+ at unwrap_spec(host=str)
 def gethostbyaddr(space, host):
     """gethostbyaddr(host) -> (name, aliaslist, addresslist)
 
@@ -61,8 +61,8 @@
     except SocketError, e:
         raise converted_error(space, e)
     return common_wrapgethost(space, res)
-gethostbyaddr.unwrap_spec = [ObjSpace, str]
 
+ at unwrap_spec(name=str)
 def getservbyname(space, name, w_proto=None):
     """getservbyname(servicename[, protocolname]) -> integer
 
@@ -79,8 +79,8 @@
     except SocketError, e:
         raise converted_error(space, e)
     return space.wrap(port)
-getservbyname.unwrap_spec = [ObjSpace, str, W_Root]
 
+ at unwrap_spec(port=int)
 def getservbyport(space, port, w_proto=None):
     """getservbyport(port[, protocolname]) -> string
 
@@ -102,8 +102,8 @@
     except SocketError, e:
         raise converted_error(space, e)
     return space.wrap(service)
-getservbyport.unwrap_spec = [ObjSpace, int, W_Root]
 
+ at unwrap_spec(name=str)
 def getprotobyname(space, name):
     """getprotobyname(name) -> integer
 
@@ -114,8 +114,8 @@
     except SocketError, e:
         raise converted_error(space, e)
     return space.wrap(proto)
-getprotobyname.unwrap_spec = [ObjSpace, str]
 
+ at unwrap_spec(flags=int)
 def getnameinfo(space, w_sockaddr, flags):
     """getnameinfo(sockaddr, flags) --> (host, port)
 
@@ -126,8 +126,8 @@
     except SocketError, e:
         raise converted_error(space, e)
     return space.newtuple([space.wrap(host), space.wrap(servport)])
-getnameinfo.unwrap_spec = [ObjSpace, W_Root, int]
 
+ at unwrap_spec(fd=int, family=int, type=int, proto=int)
 def fromfd(space, fd, family, type, proto=0):
     """fromfd(fd, family, type[, proto]) -> socket object
 
@@ -139,11 +139,11 @@
     except SocketError, e:
         raise converted_error(space, e)
     return space.wrap(sock)
-fromfd.unwrap_spec = [ObjSpace, int, int, int, int]
 
-def socketpair(space, family = rsocket.socketpair_default_family,
-                      type   = rsocket.SOCK_STREAM,
-                      proto  = 0):
+ at unwrap_spec(family=int, type=int, proto=int)
+def socketpair(space, family=rsocket.socketpair_default_family,
+                      type  =rsocket.SOCK_STREAM,
+                      proto =0):
     """socketpair([family[, type[, proto]]]) -> (socket object, socket object)
 
     Create a pair of socket objects from the sockets returned by the platform
@@ -156,43 +156,43 @@
     except SocketError, e:
         raise converted_error(space, e)
     return space.newtuple([space.wrap(sock1), space.wrap(sock2)])
-socketpair.unwrap_spec = [ObjSpace, int, int, int]
 
 # The following 4 functions refuse all negative numbers, like CPython 2.6.
 # They could also check that the argument is not too large, but CPython 2.6
 # is not doing that consistently.
+ at unwrap_spec(x="c_uint")
 def ntohs(space, x):
     """ntohs(integer) -> integer
 
     Convert a 16-bit integer from network to host byte order.
     """
     return space.wrap(rsocket.ntohs(x))
-ntohs.unwrap_spec = [ObjSpace, "c_uint"]
 
+ at unwrap_spec(x="c_uint")
 def ntohl(space, x):
     """ntohl(integer) -> integer
 
     Convert a 32-bit integer from network to host byte order.
     """
     return space.wrap(rsocket.ntohl(x))
-ntohl.unwrap_spec = [ObjSpace, "c_uint"]
 
+ at unwrap_spec(x="c_uint")
 def htons(space, x):
     """htons(integer) -> integer
 
     Convert a 16-bit integer from host to network byte order.
     """
     return space.wrap(rsocket.htons(x))
-htons.unwrap_spec = [ObjSpace, "c_uint"]
 
+ at unwrap_spec(x="c_uint")
 def htonl(space, x):
     """htonl(integer) -> integer
 
     Convert a 32-bit integer from host to network byte order.
     """
     return space.wrap(rsocket.htonl(x))
-htonl.unwrap_spec = [ObjSpace, "c_uint"]
 
+ at unwrap_spec(ip=str)
 def inet_aton(space, ip):
     """inet_aton(string) -> packed 32-bit IP representation
 
@@ -204,8 +204,8 @@
     except SocketError, e:
         raise converted_error(space, e)
     return space.wrap(buf)
-inet_aton.unwrap_spec = [ObjSpace, str]
 
+ at unwrap_spec(packed=str)
 def inet_ntoa(space, packed):
     """inet_ntoa(packed_ip) -> ip_address_string
 
@@ -216,8 +216,8 @@
     except SocketError, e:
         raise converted_error(space, e)
     return space.wrap(ip)
-inet_ntoa.unwrap_spec = [ObjSpace, str]
 
+ at unwrap_spec(family=int, ip=str)
 def inet_pton(space, family, ip):
     """inet_pton(family, ip) -> packed IP address string
 
@@ -229,8 +229,8 @@
     except SocketError, e:
         raise converted_error(space, e)
     return space.wrap(buf)
-inet_pton.unwrap_spec = [ObjSpace, int, str]
 
+ at unwrap_spec(family=int, packed=str)
 def inet_ntop(space, family, packed):
     """inet_ntop(family, packed_ip) -> string formatted IP address
 
@@ -244,8 +244,8 @@
         raise OperationError(space.w_ValueError,
                   space.wrap(str(e)))
     return space.wrap(ip)
-inet_ntop.unwrap_spec = [ObjSpace, int, str]
 
+ at unwrap_spec(family=int, socktype=int, proto=int, flags=int)
 def getaddrinfo(space, w_host, w_port,
                 family=rsocket.AF_UNSPEC, socktype=0, proto=0, flags=0):
     """getaddrinfo(host, port [, family, socktype, proto, flags])
@@ -288,7 +288,6 @@
                             addr.as_object(-1, space)]) # -1 as per cpython
             for (family, socktype, protocol, canonname, addr) in lst]
     return space.newlist(lst1)
-getaddrinfo.unwrap_spec = [ObjSpace, W_Root, W_Root, int, int, int, int]
 
 def getdefaulttimeout(space):
     """getdefaulttimeout() -> timeout
@@ -301,7 +300,6 @@
     if timeout < 0.0:
         return space.w_None
     return space.wrap(timeout)
-getdefaulttimeout.unwrap_spec = [ObjSpace]
 
 def setdefaulttimeout(space, w_timeout):
     if space.is_w(w_timeout, space.w_None):


More information about the Pypy-commit mailing list