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

nik at codespeak.net nik at codespeak.net
Sun Nov 27 17:55:15 CET 2005


Author: nik
Date: Sun Nov 27 17:55:14 2005
New Revision: 20308

Modified:
   pypy/dist/pypy/module/_socket/interp_socket.py
   pypy/dist/pypy/module/_socket/test/test_socket2.py
Log:
implemented inet_ntoa/inet_ntop (ipv4 only for the moment) on interp
level. ll primitives can easily be avoided here, i guess it makes sense
to do it then.


Modified: pypy/dist/pypy/module/_socket/interp_socket.py
==============================================================================
--- pypy/dist/pypy/module/_socket/interp_socket.py	(original)
+++ pypy/dist/pypy/module/_socket/interp_socket.py	Sun Nov 27 17:55:14 2005
@@ -9,6 +9,9 @@
 # Force the declarations of external functions
 import pypy.module._socket.rpython.exttable
 
+IPV4_ADDRESS_SIZE = 4
+IPV6_ADDRESS_SIZE = 16
+
 if sys.platform == 'win32':
     WIN32_ERROR_MESSAGES = {
         errno.WSAEINTR:  "Interrupted system call",
@@ -301,9 +304,14 @@
     Convert an IP address from 32-bit packed binary format to string format
     """
     try:
-        return space.wrap(socket.inet_ntoa(packed))
-    except socket.error, e:
-        raise wrap_socketerror(space, e)
+        return inet_ntop_ipv4(space, packed)
+    except OperationError, e:
+        if not e.match(space, space.w_ValueError):
+            raise
+        w_module = space.getbuiltinmodule('_socket')
+        # NB: This socket.error has no errno as per CPython
+        raise OperationError(space.getattr(w_module, space.wrap('error')),
+                             space.wrap("packed IP wrong length for inet_ntoa"))
 inet_ntoa.unwrap_spec = [ObjSpace, str]
 
 def inet_pton(space, af, ip):
@@ -318,17 +326,27 @@
         raise wrap_socketerror(space, e)
 inet_pton.unwrap_spec = [ObjSpace, int, str]
 
-def inet_ntop(space, af, packed):
-    """inet_ntop(af, packed_ip) -> string formatted IP address
+def inet_ntop(space, family, packed):
+    """inet_ntop(family, packed_ip) -> string formatted IP address
 
     Convert a packed IP address of the given family to string format.
     """
-    try:
-        return space.wrap(socket.inet_ntop(af, packed))
-    except socket.error, e:
-        raise wrap_socketerror(space, e)
+    if family == socket.AF_INET:
+        return inet_ntop_ipv4(space, packed)
+    elif family == socket.AF_INET6:
+        raise NotImplementedError()
+    else:
+        raise OperationError(space.w_ValueError,
+                             space.wrap("unknown address family %s" % family))
 inet_ntop.unwrap_spec = [ObjSpace, int, str]
 
+def inet_ntop_ipv4(space, packed):
+    if len(packed) != IPV4_ADDRESS_SIZE:
+        raise OperationError(space.w_ValueError,
+                             space.wrap("invalid length of packed IP address string"))
+    numbers = ord(packed[0]), ord(packed[1]), ord(packed[2]), ord(packed[3])
+    return space.wrap("%d.%d.%d.%d" % numbers)
+
 def enumerateaddrinfo(space, addr):
     result = []
     while True:

Modified: pypy/dist/pypy/module/_socket/test/test_socket2.py
==============================================================================
--- pypy/dist/pypy/module/_socket/test/test_socket2.py	(original)
+++ pypy/dist/pypy/module/_socket/test/test_socket2.py	Sun Nov 27 17:55:14 2005
@@ -1,4 +1,5 @@
 from pypy.objspace.std import StdObjSpace
+from pypy.interpreter.error import OperationError
 from pypy.tool.udir import udir
 import py
 import socket, sys
@@ -8,6 +9,7 @@
     mod.w_socket = space.appexec([], "(): import _socket as m; return m")
     mod.path = udir.join('fd')
     mod.path.write('fo')
+    mod.raises = py.test.raises # make raises available from app-level tests
 
 def test_gethostname():
     host = space.appexec([w_socket], "(_socket): return _socket.gethostname()")
@@ -115,7 +117,7 @@
                         "(_socket, x): return _socket.htonl(x)")
     assert space.unwrap(w_n) == socket.htonl(125)
 
-def test_packed_ip():
+def test_aton_ntoa():
     ip = '123.45.67.89'
     packed = socket.inet_aton(ip)
     w_p = space.appexec([w_socket, space.wrap(ip)],
@@ -125,17 +127,29 @@
                          "(_socket, p): return _socket.inet_ntoa(p)")
     assert space.unwrap(w_ip) == ip
 
-def test_pton():
-    ip = '123.45.67.89'
-    packed = socket.inet_aton(ip)
+def test_pton_ntop_ipv4():
     if not hasattr(socket, 'inet_pton'):
         py.test.skip('No socket.(inet_pton|inet_ntop) on this platform')
-    w_p = space.appexec([w_socket, space.wrap(ip)],
-                        "(_socket, ip): return _socket.inet_pton(_socket.AF_INET, ip)")
-    assert space.unwrap(w_p) == packed
-    w_ip = space.appexec([w_socket, space.wrap(packed)],
-                         "(_socket, p): return _socket.inet_ntop(_socket.AF_INET, p)")
-    assert space.unwrap(w_ip) == ip
+    for ip in '123.45.67.89', '0.0.0.0', '255.255.255.254':
+        packed = socket.inet_aton(ip)
+        w_p = space.appexec([w_socket, space.wrap(ip)],
+                            "(_socket, ip): return _socket.inet_pton(_socket.AF_INET, ip)")
+        assert space.unwrap(w_p) == packed
+        w_ip = space.appexec([w_socket, space.wrap(packed)],
+                             "(_socket, p): return _socket.inet_ntop(_socket.AF_INET, p)")
+        assert space.unwrap(w_ip) == ip
+
+def app_test_ntoa_exception():
+    import _socket
+    raises(_socket.error, _socket.inet_ntoa, "ab")
+
+def app_test_ntop_exceptions():
+    import _socket
+    for family, packed, exception in \
+                [(_socket.AF_INET + _socket.AF_INET6, "", ValueError),
+                 (_socket.AF_INET, "a", ValueError),
+                 (_socket.AF_INET, u"aa\u2222a", UnicodeEncodeError)]:
+        raises(exception, _socket.inet_ntop, family, packed)
 
 def test_has_ipv6():
     res = space.appexec([w_socket], "(_socket): return _socket.has_ipv6")



More information about the Pypy-commit mailing list