[pypy-svn] r18537 - in pypy/dist/pypy/module/_socket: . test

afa at codespeak.net afa at codespeak.net
Fri Oct 14 15:13:32 CEST 2005


Author: afa
Date: Fri Oct 14 15:13:30 2005
New Revision: 18537

Added:
   pypy/dist/pypy/module/_socket/   (props changed)
   pypy/dist/pypy/module/_socket/__init__.py
   pypy/dist/pypy/module/_socket/app_socket.py
   pypy/dist/pypy/module/_socket/interp_socket.py
   pypy/dist/pypy/module/_socket/test/   (props changed)
   pypy/dist/pypy/module/_socket/test/test_socket2.py
Log:
(valentino, afa): start of the socket module
lots of functions are still missing



Added: pypy/dist/pypy/module/_socket/__init__.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/_socket/__init__.py	Fri Oct 14 15:13:30 2005
@@ -0,0 +1,34 @@
+# Package initialisation
+from pypy.interpreter.mixedmodule import MixedModule
+import _socket
+import sys
+
+class Module(MixedModule):
+    appleveldefs = {
+        'error'      : 'app_socket.error',
+        'herror'     : 'app_socket.herror',
+        'gaierror'   : 'app_socket.gaierror',
+        'timeout'    : 'app_socket.timeout',
+    }
+
+    interpleveldefs = {
+    }
+
+for name in """
+    gethostbyname gethostbyname_ex gethostbyaddr gethostname
+    getservbyname getservbyport getprotobyname
+    fromfd socketpair
+    ntohs ntohl htons htonl inet_aton inet_ntoa inet_pton inet_ntop
+    getaddrinfo getnameinfo
+    getdefaulttimeout setdefaulttimeout
+    """.split():
+    
+    if hasattr(_socket, name):
+        Module.interpleveldefs[name] = 'interp_socket.%s' % (name, )
+
+for constant in dir(_socket):
+    value = getattr(_socket, constant)
+    if constant.isupper() and type(value) in (int, str):
+        Module.interpleveldefs[constant] = "space.wrap(%s)" % value
+
+Module.interpleveldefs['has_ipv6'] = "space.wrap(%s)" % _socket.has_ipv6

Added: pypy/dist/pypy/module/_socket/app_socket.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/_socket/app_socket.py	Fri Oct 14 15:13:30 2005
@@ -0,0 +1,20 @@
+"""Implementation module for socket operations.
+
+See the socket module for documentation."""
+
+class error(Exception):
+    pass
+
+class herror(error):
+    pass
+
+class gaierror(error):
+    pass
+
+class timeout(error):
+    pass
+
+class SocketType:
+    pass
+
+socket = SocketType

Added: pypy/dist/pypy/module/_socket/interp_socket.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/_socket/interp_socket.py	Fri Oct 14 15:13:30 2005
@@ -0,0 +1,61 @@
+
+import socket
+from pypy.interpreter.error import OperationError
+from pypy.interpreter.gateway import ObjSpace, W_Root, NoneNotWrapped
+
+def gethostname(space):
+    """gethostname() -> string
+
+    Return the current host name.
+    """
+    return space.wrap(socket.gethostname())
+gethostname.unwrap_spec = [ObjSpace]
+
+def gethostbyname(space, name):
+    """gethostbyname(host) -> address
+
+    Return the IP address (a string of the form '255.255.255.255') for a host.
+    """
+    return space.wrap(socket.gethostbyname(name))
+gethostbyname.unwrap_spec = [ObjSpace, str]
+
+def gethostbyname_ex(space, name):
+    """gethostbyname_ex(host) -> (name, aliaslist, addresslist)
+
+    Return the true host name, a list of aliases, and a list of IP addresses,
+    for a host.  The host argument is a string giving a host name or IP number.
+    """
+    return space.wrap(socket.gethostbyname_ex(name))
+gethostbyname_ex.unwrap_spec = [ObjSpace, str]
+
+def gethostbyaddr(space, ip_num):
+    """gethostbyaddr(host) -> (name, aliaslist, addresslist)
+
+    Return the true host name, a list of aliases, and a list of IP addresses,
+    for a host.  The host argument is a string giving a host name or IP number.
+    """
+    return space.wrap(socket.gethostbyaddr(ip_num))
+gethostbyaddr.unwrap_spec = [ObjSpace, str]
+
+def getservbyname(space, name, w_proto = NoneNotWrapped):
+    """getservbyname(servicename[, protocolname]) -> integer
+
+    Return a port number from a service name and protocol name.
+    The optional protocol name, if given, should be 'tcp' or 'udp',
+    otherwise any protocol will match.
+    """
+    if w_proto is None:
+        return space.wrap(socket.getservbyname(name))
+    else:
+        return space.wrap(socket.getservbyname(name, space.str_w(w_proto)))
+getservbyname.unwrap_spec = [ObjSpace, str, W_Root]
+
+def getservbyport(space, port):
+    """getservbyport(port[, protocolname]) -> string
+
+    Return the service name from a port number and protocol name.
+    The optional protocol name, if given, should be 'tcp' or 'udp',
+    otherwise any protocol will match.
+    """
+
+# getservbyport getprotobyname

Added: pypy/dist/pypy/module/_socket/test/test_socket2.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/_socket/test/test_socket2.py	Fri Oct 14 15:13:30 2005
@@ -0,0 +1,51 @@
+from pypy.objspace.std import StdObjSpace 
+from pypy.tool.udir import udir
+import py
+import socket, sys
+
+def setup_module(mod): 
+    mod.space = StdObjSpace(usemodules=['_socket'])
+    mod.w_socket = space.appexec([], "(): import _socket as m; return m")
+    
+def test_gethostname():
+    host = space.appexec([w_socket], "(_socket): return _socket.gethostname()")
+    assert space.unwrap(host) == socket.gethostname()
+
+def test_gethostbyname():
+    host = "localhost"
+    ip = space.appexec([w_socket, space.wrap(host)],
+                       "(_socket, host): return _socket.gethostbyname(host)")
+    assert space.unwrap(ip) == socket.gethostbyname(host)
+
+def test_gethostbyname_ex():
+    host = "localhost"
+    ip = space.appexec([w_socket, space.wrap(host)],
+                       "(_socket, host): return _socket.gethostbyname_ex(host)")
+    assert isinstance(space.unwrap(ip), tuple)
+    assert space.unwrap(ip) == socket.gethostbyname_ex(host)
+
+def test_gethostbyaddr():
+    host = "localhost"
+    ip = space.appexec([w_socket, space.wrap(host)],
+                       "(_socket, host): return _socket.gethostbyaddr(host)")
+    assert space.unwrap(ip) == socket.gethostbyaddr(host)
+    host = "127.0.0.1"
+    ip = space.appexec([w_socket, space.wrap(host)],
+                       "(_socket, host): return _socket.gethostbyaddr(host)")
+    assert space.unwrap(ip) == socket.gethostbyaddr(host)
+
+def test_getservbyname():
+    name = "smtp"
+    # 2 args version
+    port = space.appexec([w_socket, space.wrap(name)],
+                        "(_socket, name): return _socket.getservbyname(name, 'tcp')")
+    # 1 arg version
+    if sys.version_info < (2, 4):
+        py.test.skip("getservbyname second argument is not optional before python 2.4")
+    port = space.appexec([w_socket, space.wrap(name)],
+                        "(_socket, name): return _socket.getservbyname(name)")
+
+def test_has_ipv6():
+    res = space.appexec([w_socket], "(_socket): return _socket.has_ipv6")
+    assert space.unwrap(res) == socket.has_ipv6
+



More information about the Pypy-commit mailing list