[pypy-svn] r46752 - in pypy/dist/pypy: rlib rlib/test rpython/lltypesystem

arigo at codespeak.net arigo at codespeak.net
Wed Sep 19 22:36:44 CEST 2007


Author: arigo
Date: Wed Sep 19 22:36:43 2007
New Revision: 46752

Modified:
   pypy/dist/pypy/rlib/_rsocket_rffi.py
   pypy/dist/pypy/rlib/rsocket_rffi.py
   pypy/dist/pypy/rlib/test/test_rsocket_rffi.py
   pypy/dist/pypy/rpython/lltypesystem/rffi.py
Log:
* rsocket_rffi.gethostname()
* rffi.charp2strn(p, maxlen)


Modified: pypy/dist/pypy/rlib/_rsocket_rffi.py
==============================================================================
--- pypy/dist/pypy/rlib/_rsocket_rffi.py	(original)
+++ pypy/dist/pypy/rlib/_rsocket_rffi.py	Wed Sep 19 22:36:43 2007
@@ -446,7 +446,8 @@
 sendto = external('sendto', [socketfd_type, rffi.VOIDP, size_t, rffi.INT,
                                     sockaddr_ptr, socklen_t], ssize_t)
 shutdown = external('shutdown', [socketfd_type, rffi.INT], rffi.INT)
-gethostname = external('gethostname', [rffi.CCHARP, rffi.INT], rffi.INT)
+gethostname = external('gethostname', [rffi.CCHARP, rffi.INT], rffi.INT,
+                       stringpolicy='noauto')
 gethostbyname = external('gethostbyname', [rffi.CCHARP],
                                 lltype.Ptr(cConfig.hostent))
 gethostbyaddr = external('gethostbyaddr', [rffi.VOIDP, rffi.INT, rffi.INT], lltype.Ptr(cConfig.hostent))

Modified: pypy/dist/pypy/rlib/rsocket_rffi.py
==============================================================================
--- pypy/dist/pypy/rlib/rsocket_rffi.py	(original)
+++ pypy/dist/pypy/rlib/rsocket_rffi.py	Wed Sep 19 22:36:43 2007
@@ -914,12 +914,15 @@
     return defaults.timeout
 
 def gethostname():
-    buf = create_string_buffer(1024)
-    res = _c.gethostname(buf, sizeof(buf)-1)
-    if res < 0:
-        raise last_error()
-    buf[sizeof(buf)-1] = '\x00'
-    return buf.value
+    size = 1024
+    buf = lltype.malloc(rffi.CCHARP.TO, size, flavor='raw')
+    try:
+        res = _c.gethostname(buf, size)
+        if res < 0:
+            raise last_error()
+        return rffi.charp2strn(buf, size)
+    finally:
+        lltype.free(buf, flavor='raw')
 
 def gethostbyname(name):
     # this is explicitly not working with IPv6, because the docs say it

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	Wed Sep 19 22:36:43 2007
@@ -35,7 +35,6 @@
     assert a.get_groups() == group_mask
     
 def test_gethostname():
-    py.test.skip("in-progress")
     s = gethostname()
     assert isinstance(s, str)
 

Modified: pypy/dist/pypy/rpython/lltypesystem/rffi.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/rffi.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/rffi.py	Wed Sep 19 22:36:43 2007
@@ -274,6 +274,15 @@
         i += 1
     return "".join(l)
 
+# char* -> str, with an upper bound on the length in case there is no \x00
+def charp2strn(cp, maxlen):
+    l = []
+    i = 0
+    while i < maxlen and cp[i] != '\x00':
+        l.append(cp[i])
+        i += 1
+    return "".join(l)
+
 # char**
 CCHARPP = lltype.Ptr(lltype.Array(CCHARP, hints={'nolength': True}))
 



More information about the Pypy-commit mailing list