[pypy-svn] r42107 - in pypy/dist/pypy/rlib: . test

afa at codespeak.net afa at codespeak.net
Mon Apr 16 23:15:55 CEST 2007


Author: afa
Date: Mon Apr 16 23:15:54 2007
New Revision: 42107

Modified:
   pypy/dist/pypy/rlib/_rsocket_ctypes.py
   pypy/dist/pypy/rlib/rsocket.py
   pypy/dist/pypy/rlib/test/test_rsocket.py
Log:
rsocket.py is now importable on Windows. 2 tests even pass


Modified: pypy/dist/pypy/rlib/_rsocket_ctypes.py
==============================================================================
--- pypy/dist/pypy/rlib/_rsocket_ctypes.py	(original)
+++ pypy/dist/pypy/rlib/_rsocket_ctypes.py	Mon Apr 16 23:15:54 2007
@@ -10,39 +10,54 @@
 from ctypes import c_short, POINTER, ARRAY, cdll, sizeof, SetPointerType
 from pypy.rlib.rarithmetic import intmask, r_uint
 
-
 # Also not used here, but exported for other code.
 from ctypes import cast, pointer, create_string_buffer
 
-includes = ('sys/types.h',
-            'sys/socket.h',
-            'sys/un.h',
-            'sys/poll.h',
-            'netinet/in.h',
-            'netinet/tcp.h',
-            'unistd.h',
-            'fcntl.h',
-            'stdio.h',
-            'netdb.h',
-            'arpa/inet.h',
-            'stdint.h', 
-            'errno.h',
-            )
-cond_includes = [('AF_NETLINK', 'linux/netlink.h')]
-HEADER = ''.join(['#include <%s>\n' % filename for filename in includes])
-COND_HEADER = ''.join(['#ifdef %s\n#include <%s>\n#endif\n' % cond_include
-                      for cond_include in cond_includes])
+_POSIX = os.name == "posix"
+_MS_WINDOWS = os.name == "nt"
+
+if _POSIX:
+    includes = ('sys/types.h',
+                'sys/socket.h',
+                'sys/un.h',
+                'sys/poll.h',
+                'netinet/in.h',
+                'netinet/tcp.h',
+                'unistd.h',
+                'fcntl.h',
+                'stdio.h',
+                'netdb.h',
+                'arpa/inet.h',
+                'stdint.h', 
+                'errno.h',
+                )
+    cond_includes = [('AF_NETLINK', 'linux/netlink.h')]
+    HEADER = ''.join(['#include <%s>\n' % filename for filename in includes])
+    COND_HEADER = ''.join(['#ifdef %s\n#include <%s>\n#endif\n' % cond_include
+                          for cond_include in cond_includes])
+if _MS_WINDOWS:
+    HEADER = '\n'.join([
+        '#include <WinSock2.h>',
+        '#include <WS2tcpip.h>',
+        # winsock2 defines AF_UNIX, but not sockaddr_un
+        '#undef AF_UNIX',
+        # these types do not exist on windows
+        'typedef int ssize_t;',
+        'typedef unsigned __int16 uint16_t;',
+        'typedef unsigned __int32 uint32_t;',
+        ])
+    COND_HEADER = ''
 constants = {}
 
 class CConfig:
     _header_ = HEADER + COND_HEADER
     # constants
-    O_NONBLOCK = ctypes_platform.ConstantInteger('O_NONBLOCK')
-    F_GETFL = ctypes_platform.ConstantInteger('F_GETFL')
-    F_SETFL = ctypes_platform.ConstantInteger('F_SETFL')
+    O_NONBLOCK = ctypes_platform.DefinedConstantInteger('O_NONBLOCK')
+    F_GETFL = ctypes_platform.DefinedConstantInteger('F_GETFL')
+    F_SETFL = ctypes_platform.DefinedConstantInteger('F_SETFL')
 
     linux      = ctypes_platform.Defined('linux')
-    MS_WINDOWS = ctypes_platform.Defined('MS_WINDOWS')
+    MS_WINDOWS = ctypes_platform.Defined('_WIN32')
     INVALID_SOCKET = ctypes_platform.DefinedConstantInteger('INVALID_SOCKET')
     INET_ADDRSTRLEN = ctypes_platform.DefinedConstantInteger('INET_ADDRSTRLEN')
     INET6_ADDRSTRLEN= ctypes_platform.DefinedConstantInteger('INET6_ADDRSTRLEN')
@@ -213,11 +228,23 @@
                                           [('p_proto', c_int),
                                            ])
 
-CConfig.nfds_t = ctypes_platform.SimpleType('nfds_t')
-CConfig.pollfd = ctypes_platform.Struct('struct pollfd',
-                                        [('fd', c_int),
-                                         ('events', c_short),
-                                         ('revents', c_short)])
+if _POSIX:
+    CConfig.nfds_t = ctypes_platform.SimpleType('nfds_t')
+    CConfig.pollfd = ctypes_platform.Struct('struct pollfd',
+                                            [('fd', c_int),
+                                             ('events', c_short),
+                                             ('revents', c_short)])
+
+if _MS_WINDOWS:
+    CConfig.WSAData = ctypes_platform.Struct('struct WSAData',
+                                     [('wVersion', c_ushort),
+                                      ('wHighVersion', c_ushort),
+                                      ('szDescription', c_char * 1), # (WSADESCRIPTION_LEN+1)
+                                      ('szSystemStatus', c_char * 1), # (WSASYS_STATUS_LEN+1)
+                                      ('iMaxSockets', c_ushort),
+                                      ('iMaxUdpDg', c_ushort),
+                                      ('lpVendorInfo', c_char_p)])
+
 
 class cConfig:
     pass
@@ -260,6 +287,8 @@
 
 linux = cConfig.linux
 MS_WINDOWS = cConfig.MS_WINDOWS
+assert MS_WINDOWS == _MS_WINDOWS
+
 if MS_WINDOWS:
     def invalid_socket(fd):
         return fd == INVALID_SOCKET
@@ -285,8 +314,9 @@
 in_addr_size = sizeof(in_addr)
 in6_addr = cConfig.in6_addr
 addrinfo = cConfig.addrinfo
-nfds_t = cConfig.nfds_t
-pollfd = cConfig.pollfd
+if _POSIX:
+    nfds_t = cConfig.nfds_t
+    pollfd = cConfig.pollfd
 
 c_int_size = sizeof(c_int)
 SetPointerType(addrinfo_ptr, addrinfo)
@@ -294,23 +324,31 @@
 
 
 # functions
-dllname = util.find_library('c')
-assert dllname is not None
-socketdll = cdll.LoadLibrary(dllname)
-
-dup = socketdll.dup
-dup.argtypes = [c_int]
-dup.restype = c_int
+if MS_WINDOWS:
+    from ctypes import windll
+    dllname = util.find_library('wsock32')
+    assert dllname is not None
+    socketdll = windll.LoadLibrary(dllname)
+else:
+    dllname = util.find_library('c')
+    assert dllname is not None
+    socketdll = cdll.LoadLibrary(dllname)
+
+if _POSIX:
+    dup = socketdll.dup
+    dup.argtypes = [c_int]
+    dup.restype = c_int
 
 #errno = c_int.in_dll(socketdll, 'errno')
 
-strerror = socketdll.strerror
-strerror.argtypes = [c_int]
-strerror.restype = c_char_p
-
-gai_strerror = socketdll.gai_strerror
-gai_strerror.argtypes = [c_int]
-gai_strerror.restype = c_char_p
+if _POSIX:
+    strerror = socketdll.strerror
+    strerror.argtypes = [c_int]
+    strerror.restype = c_char_p
+
+    gai_strerror = socketdll.gai_strerror
+    gai_strerror.argtypes = [c_int]
+    gai_strerror.restype = c_char_p
 
 #h_errno = c_int.in_dll(socketdll, 'h_errno')
 #
@@ -333,20 +371,21 @@
 socketconnect.argtypes = [c_int, sockaddr_ptr, socklen_t]
 socketconnect.restype = c_int
 
-getaddrinfo = socketdll.getaddrinfo
-getaddrinfo.argtypes = [c_char_p, c_char_p, addrinfo_ptr,
-                        POINTER(addrinfo_ptr)]
-getaddrinfo.restype = c_int
-
-freeaddrinfo = socketdll.freeaddrinfo
-freeaddrinfo.argtypes = [addrinfo_ptr]
-freeaddrinfo.restype = None
-
-getnameinfo = socketdll.getnameinfo
-getnameinfo.argtypes = [sockaddr_ptr, socklen_t,
-                        c_char_p, size_t,
-                        c_char_p, size_t, c_int]
-getnameinfo.restype = c_int
+if not MS_WINDOWS:
+    getaddrinfo = socketdll.getaddrinfo
+    getaddrinfo.argtypes = [c_char_p, c_char_p, addrinfo_ptr,
+                            POINTER(addrinfo_ptr)]
+    getaddrinfo.restype = c_int
+
+    freeaddrinfo = socketdll.freeaddrinfo
+    freeaddrinfo.argtypes = [addrinfo_ptr]
+    freeaddrinfo.restype = None
+
+    getnameinfo = socketdll.getnameinfo
+    getnameinfo.argtypes = [sockaddr_ptr, socklen_t,
+                            c_char_p, size_t,
+                            c_char_p, size_t, c_int]
+    getnameinfo.restype = c_int
 
 htonl = socketdll.htonl
 htonl.argtypes = [uint32_t]
@@ -364,21 +403,23 @@
 ntohs.argtypes = [uint16_t]
 ntohs.restype = uint16_t
 
-inet_aton = socketdll.inet_aton
-inet_aton.argtypes = [c_char_p, POINTER(in_addr)]
-inet_aton.restype = c_int
+if _POSIX:
+    inet_aton = socketdll.inet_aton
+    inet_aton.argtypes = [c_char_p, POINTER(in_addr)]
+    inet_aton.restype = c_int
 
 inet_ntoa = socketdll.inet_ntoa
 inet_ntoa.argtypes = [in_addr]
 inet_ntoa.restype = c_char_p
 
-inet_pton = socketdll.inet_pton
-inet_pton.argtypes = [c_int, c_char_p, c_void_p]
-inet_pton.restype = c_int
-
-inet_ntop = socketdll.inet_ntop
-inet_ntop.argtypes = [c_int, c_void_p, c_char_p, socklen_t]
-inet_ntop.restype = c_char_p
+if _POSIX:
+    inet_pton = socketdll.inet_pton
+    inet_pton.argtypes = [c_int, c_char_p, c_void_p]
+    inet_pton.restype = c_int
+
+    inet_ntop = socketdll.inet_ntop
+    inet_ntop.argtypes = [c_int, c_void_p, c_char_p, socklen_t]
+    inet_ntop.restype = c_char_p
 
 socketaccept = socketdll.accept
 socketaccept.argtypes = [c_int, sockaddr_ptr, POINTER(socklen_t)]
@@ -436,11 +477,6 @@
 socketshutdown.argtypes = [c_int, c_int]
 socketshutdown.restype = c_int
 
-
-getaddrinfo = socketdll.getaddrinfo
-getaddrinfo.argtypes = [ c_char_p, c_char_p, addrinfo_ptr, POINTER(addrinfo_ptr)]
-getaddrinfo.restype = c_int
-
 gethostname = socketdll.gethostname
 gethostname.argtypes = [c_char_p, c_int]
 gethostname.restype = c_int
@@ -465,28 +501,37 @@
 getprotobyname.argtypes = [c_char_p]
 getprotobyname.restype = POINTER(cConfig.protoent)
 
-fcntl = socketdll.fcntl
-fcntl.argtypes = [c_int] * 3
-fcntl.restype = c_int
-
-memcpy = socketdll.memcpy
-memcpy.argtypes = [c_void_p, c_void_p, size_t]
-memcpy.restype = c_void_p
-
-socketpair_t = ARRAY(c_int, 2)
-socketpair = socketdll.socketpair
-socketpair.argtypes = [c_int, c_int, c_int, POINTER(socketpair_t)]
-socketpair.restype = c_int
+if _POSIX:
+    fcntl = socketdll.fcntl
+    fcntl.argtypes = [c_int] * 3
+    fcntl.restype = c_int
+
+    socketpair_t = ARRAY(c_int, 2)
+    socketpair = socketdll.socketpair
+    socketpair.argtypes = [c_int, c_int, c_int, POINTER(socketpair_t)]
+    socketpair.restype = c_int
 
 shutdown = socketdll.shutdown
 shutdown.argtypes = [c_int, c_int]
 shutdown.restype = c_int
 
-poll = socketdll.poll
-poll.argtypes = [POINTER(pollfd), nfds_t, c_int]
-poll.restype = c_int
+if _POSIX:
+    poll = socketdll.poll
+    poll.argtypes = [POINTER(pollfd), nfds_t, c_int]
+    poll.restype = c_int
 
 if MS_WINDOWS:
+    WSAData = cConfig.WSAData
+    WSAStartup = socketdll.WSAStartup
+    WSAStartup.argtypes = [c_int, POINTER(WSAData)]
+    WSAStartup.restype = c_int
+
+    WSAGetLastError = socketdll.WSAGetLastError
+    WSAGetLastError.argtypes = []
+    WSAGetLastError.restype = c_int
+    geterrno = WSAGetLastError
+    
+    import errno
     WIN32_ERROR_MESSAGES = {
         errno.WSAEINTR:  "Interrupted system call",
         errno.WSAEBADF:  "Bad file descriptor",
@@ -547,7 +592,7 @@
         }
 
     def socket_strerror(errno):
-        return WIN32_ERROR_MESSAGES.get(errno, "winsock error")
+        return WIN32_ERROR_MESSAGES.get(errno, "winsock error %d" % errno)
 else:
     def socket_strerror(errno):
         return strerror(errno)

Modified: pypy/dist/pypy/rlib/rsocket.py
==============================================================================
--- pypy/dist/pypy/rlib/rsocket.py	(original)
+++ pypy/dist/pypy/rlib/rsocket.py	Mon Apr 16 23:15:54 2007
@@ -22,6 +22,16 @@
 constants = _c.constants
 locals().update(constants) # Define constants from _c
 
+if _c.MS_WINDOWS:
+    def rsocket_startup():
+        wsadata = _c.WSAData()
+        res = _c.WSAStartup(1, byref(wsadata))
+        assert res == 0
+else:
+    def rsocket_startup():
+        pass
+ 
+ 
 ntohs = _c.ntohs
 ntohl = _c.ntohl
 htons = _c.htons
@@ -311,60 +321,62 @@
 
 # ____________________________________________________________
 
-class UNIXAddress(Address):
-    family = AF_UNIX
-    struct = _c.sockaddr_un
-    maxlen = sizeof(struct)
-
-    def __init__(self, path):
-        sun = _c.sockaddr_un(sun_family = AF_UNIX)
-        if _c.linux and path.startswith('\x00'):
-            # Linux abstract namespace extension
-            if len(path) > sizeof(sun.sun_path):
-                raise RSocketError("AF_UNIX path too long")
-        else:
-            # regular NULL-terminated string
-            if len(path) >= sizeof(sun.sun_path):
-                raise RSocketError("AF_UNIX path too long")
-            sun.sun_path[len(path)] = 0
-        for i in range(len(path)):
-            sun.sun_path[i] = ord(path[i])
-        self.sun = sun
-        self.addr = cast(pointer(sun), _c.sockaddr_ptr).contents
-        self.addrlen = offsetof(_c.sockaddr_un, 'sun_path') + len(path)
-
-    def as_sockaddr_un(self):
-        if self.addrlen <= offsetof(_c.sockaddr_un, 'sun_path'):
-            raise RSocketError("invalid address")
-        return cast(pointer(self.addr), POINTER(_c.sockaddr_un)).contents
+if 'AF_UNIX' in constants:
+    class UNIXAddress(Address):
+        family = AF_UNIX
+        struct = _c.sockaddr_un
+        maxlen = sizeof(struct)
 
-    def __repr__(self):
-        try:
-            return '<UNIXAddress %r>' % (self.get_path(),)
-        except SocketError:
-            return '<UNIXAddress ?>'
+        def __init__(self, path):
+            sun = _c.sockaddr_un(sun_family = AF_UNIX)
+            if _c.linux and path.startswith('\x00'):
+                # Linux abstract namespace extension
+                if len(path) > sizeof(sun.sun_path):
+                    raise RSocketError("AF_UNIX path too long")
+            else:
+                # regular NULL-terminated string
+                if len(path) >= sizeof(sun.sun_path):
+                    raise RSocketError("AF_UNIX path too long")
+                sun.sun_path[len(path)] = 0
+            for i in range(len(path)):
+                sun.sun_path[i] = ord(path[i])
+            self.sun = sun
+            self.addr = cast(pointer(sun), _c.sockaddr_ptr).contents
+            self.addrlen = offsetof(_c.sockaddr_un, 'sun_path') + len(path)
 
-    def get_path(self):
-        a = self.as_sockaddr_un()
-        if _c.linux and a.sun_path[0] == 0:
-            # Linux abstract namespace
-            buf = copy_buffer(cast(pointer(a.sun_path), POINTER(c_char)),
-                           self.addrlen - offsetof(_c.sockaddr_un, 'sun_path'))
-            return buf.raw
-        else:
-            # regular NULL-terminated string
-            return cast(pointer(a.sun_path), c_char_p).value
+        def as_sockaddr_un(self):
+            if self.addrlen <= offsetof(_c.sockaddr_un, 'sun_path'):
+                raise RSocketError("invalid address")
+            return cast(pointer(self.addr), POINTER(_c.sockaddr_un)).contents
 
-    def eq(self, other):   # __eq__() is not called by RPython :-/
-        return (isinstance(other, UNIXAddress) and
-                self.get_path() == other.get_path())
+        def __repr__(self):
+            try:
+                return '<UNIXAddress %r>' % (self.get_path(),)
+            except SocketError:
+                return '<UNIXAddress ?>'
+
+        def get_path(self):
+            a = self.as_sockaddr_un()
+            if _c.linux and a.sun_path[0] == 0:
+                # Linux abstract namespace
+                buf = copy_buffer(cast(pointer(a.sun_path), POINTER(c_char)),
+                               self.addrlen - offsetof(_c.sockaddr_un,
+                                                       'sun_path'))
+                return buf.raw
+            else:
+                # regular NULL-terminated string
+                return cast(pointer(a.sun_path), c_char_p).value
+
+        def eq(self, other):   # __eq__() is not called by RPython :-/
+            return (isinstance(other, UNIXAddress) and
+                    self.get_path() == other.get_path())
 
-    def as_object(self, space):
-        return space.wrap(self.get_path())
+        def as_object(self, space):
+            return space.wrap(self.get_path())
 
-    def from_object(space, w_address):
-        return UNIXAddress(space.str_w(w_address))
-    from_object = staticmethod(from_object)
+        def from_object(space, w_address):
+            return UNIXAddress(space.str_w(w_address))
+        from_object = staticmethod(from_object)
 
 if 'AF_NETLINK' in constants:
     class NETLINKAddress(Address):
@@ -837,7 +849,7 @@
 
 
 # ____________________________________________________________
-if AF_UNIX is None:
+if 'AF_UNIX' not in constants or AF_UNIX is None:
     socketpair_default_family = AF_INET
 else:
     socketpair_default_family = AF_UNIX

Modified: pypy/dist/pypy/rlib/test/test_rsocket.py
==============================================================================
--- pypy/dist/pypy/rlib/test/test_rsocket.py	(original)
+++ pypy/dist/pypy/rlib/test/test_rsocket.py	Mon Apr 16 23:15:54 2007
@@ -2,6 +2,9 @@
 from pypy.rlib import rsocket
 from pypy.rlib.rsocket import *
 
+def setup_module(mod):
+    rsocket_startup()
+
 def test_ipv4_addr():
     a = INETAddress("localhost", 4000)
     assert a.get_host() == "127.0.0.1"



More information about the Pypy-commit mailing list