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

arigo at codespeak.net arigo at codespeak.net
Thu Sep 20 15:57:45 CEST 2007


Author: arigo
Date: Thu Sep 20 15:57:41 2007
New Revision: 46767

Modified:
   pypy/dist/pypy/rlib/rsocket_rffi.py
   pypy/dist/pypy/rlib/test/test_rsocket_rffi.py
Log:
inet_pton(), inet_ntop(), with minimal tests for them.


Modified: pypy/dist/pypy/rlib/rsocket_rffi.py
==============================================================================
--- pypy/dist/pypy/rlib/rsocket_rffi.py	(original)
+++ pypy/dist/pypy/rlib/rsocket_rffi.py	Thu Sep 20 15:57:41 2007
@@ -1137,26 +1137,28 @@
 if hasattr(_c, 'inet_pton'):
     def inet_pton(family, ip):
         "human-readable string -> packed string"
-        XXX
         if family == AF_INET:
             size = sizeof(_c.in_addr)
         elif AF_INET6 is not None and family == AF_INET6:
             size = sizeof(_c.in6_addr)
         else:
             raise RSocketError("unknown address family")
-        buf = create_string_buffer(size)
-        res = _c.inet_pton(family, ip, cast(buf, c_void_p))
-        if res < 0:
-            raise last_error()
-        elif res == 0:
-            raise RSocketError("illegal IP address string passed to inet_pton")
-        else:
-            return buf.raw
+        buf = mallocbuf(size)
+        try:
+            res = _c.inet_pton(family, ip, buf)
+            if res < 0:
+                raise last_error()
+            elif res == 0:
+                raise RSocketError("illegal IP address string passed "
+                                   "to inet_pton")
+            else:
+                return ''.join([buf[i] for i in range(size)])
+        finally:
+            lltype.free(buf, flavor='raw')
 
 if hasattr(_c, 'inet_ntop'):
     def inet_ntop(family, packed):
         "packed string -> human-readable string"
-        XXX
         if family == AF_INET:
             srcsize = sizeof(_c.in_addr)
             dstsize = _c.INET_ADDRSTRLEN
@@ -1167,13 +1169,18 @@
             raise RSocketError("unknown address family")
         if len(packed) != srcsize:
             raise ValueError("packed IP wrong length for inet_ntop")
-        srcbuf = create_string_buffer(srcsize)
-        srcbuf.raw = packed
-        dstbuf = create_string_buffer(dstsize)
-        res = _c.inet_ntop(family, cast(srcbuf, c_void_p), dstbuf, dstsize)
-        if res is None:
-            raise last_error()
-        return res
+        srcbuf = rffi.str2charp(packed)
+        try:
+            dstbuf = mallocbuf(dstsize)
+            try:
+                res = _c.inet_ntop(family, srcbuf, dstbuf, dstsize)
+                if not res:
+                    raise last_error()
+                return rffi.charp2str(res)
+            finally:
+                lltype.free(dstbuf, flavor='raw')
+        finally:
+            lltype.free(srcbuf, flavor='raw')
 
 def setdefaulttimeout(timeout):
     if timeout < 0.0:

Modified: pypy/dist/pypy/rlib/test/test_rsocket_rffi.py
==============================================================================
--- pypy/dist/pypy/rlib/test/test_rsocket_rffi.py	(original)
+++ pypy/dist/pypy/rlib/test/test_rsocket_rffi.py	Thu Sep 20 15:57:41 2007
@@ -276,6 +276,13 @@
 def test_inet_ntoa():
     assert inet_ntoa('\x01\x02\x03\x04') == '1.2.3.4'
 
+def test_inet_pton():
+    assert inet_pton(AF_INET, '1.2.3.5') == '\x01\x02\x03\x05'
+    py.test.raises(SocketError, inet_pton, AF_INET, '127.0.0.256')
+
+def test_inet_ntop():
+    assert inet_ntop(AF_INET, '\x01\x02\x03\x05') == '1.2.3.5'
+
 class TestTCP:
     PORT = 50007
     HOST = 'localhost'



More information about the Pypy-commit mailing list