[pypy-svn] r39864 - in pypy/dist/pypy/module/rsocket: . test

ac at codespeak.net ac at codespeak.net
Sun Mar 4 12:25:20 CET 2007


Author: ac
Date: Sun Mar  4 12:25:17 2007
New Revision: 39864

Modified:
   pypy/dist/pypy/module/rsocket/interp_socket.py
   pypy/dist/pypy/module/rsocket/test/test_sock_app.py
Log:
issue251 resolved
issue256 in-progress

Make _socket.socket.{send,sendall,sendto} accept a buffer object as data.



Modified: pypy/dist/pypy/module/rsocket/interp_socket.py
==============================================================================
--- pypy/dist/pypy/module/rsocket/interp_socket.py	(original)
+++ pypy/dist/pypy/module/rsocket/interp_socket.py	Sun Mar  4 12:25:17 2007
@@ -7,6 +7,19 @@
 from pypy.interpreter.error import OperationError
 from pypy.interpreter import gateway
 
+class State(object):
+    "Cache the lookup of the buffer type"
+    def __init__(self, space):
+        w__builtin__ = space.getbuiltinmodule('__builtin__')
+        self.w_buffer = space.getattr(w__builtin__, space.wrap('buffer'))
+        
+def coerce_to_str_w(space, w_obj):
+    "Accept an applevel string or buffer and return a string."
+    w_buffer = space.fromcache(State).w_buffer
+    if space.is_true(space.isinstance(w_obj, w_buffer)):
+        w_obj = space.str(w_obj)
+    return space.str_w(w_obj)
+
 class W_RSocket(Wrappable, RSocket):
     def accept_w(self, space):
         """accept() -> (socket object, address info)
@@ -251,13 +264,14 @@
             raise converted_error(space, e)
     recvfrom_w.unwrap_spec = ['self', ObjSpace, int, int]
 
-    def send_w(self, space, data, flags=0):
+    def send_w(self, space, w_data, flags=0):
         """send(data[, flags]) -> count
 
         Send a data string to the socket.  For the optional flags
         argument, see the Unix manual.  Return the number of bytes
         sent; this may be less than len(data) if the network is busy.
         """
+        data = coerce_to_str_w(space, w_data)
         try:
             GIL = space.threadlocals.getGIL()
             if GIL is not None: GIL.release()
@@ -268,9 +282,9 @@
         except SocketError, e:
             raise converted_error(space, e)
         return space.wrap(count)
-    send_w.unwrap_spec = ['self', ObjSpace, str, int]
+    send_w.unwrap_spec = ['self', ObjSpace, W_Root, int]
 
-    def sendall_w(self, space, data, flags=0):
+    def sendall_w(self, space, w_data, flags=0):
         """sendall(data[, flags])
 
         Send a data string to the socket.  For the optional flags
@@ -278,6 +292,7 @@
         until all data is sent.  If an error occurs, it's impossible
         to tell how much data has been sent.
         """
+        data = coerce_to_str_w(space, w_data)
         try:
             GIL = space.threadlocals.getGIL()
             if GIL is not None: GIL.release()
@@ -287,14 +302,15 @@
                 if GIL is not None: GIL.acquire(True)
         except SocketError, e:
             raise converted_error(space, e)
-    sendall_w.unwrap_spec = ['self', ObjSpace, str, int]
+    sendall_w.unwrap_spec = ['self', ObjSpace, W_Root, int]
 
-    def sendto_w(self, space, data, w_param2, w_param3=NoneNotWrapped):
+    def sendto_w(self, space, w_data, w_param2, w_param3=NoneNotWrapped):
         """sendto(data[, flags], address) -> count
 
         Like send(data, flags) but allows specifying the destination address.
         For IP sockets, the address is a pair (hostaddr, port).
         """
+        data = coerce_to_str_w(space, w_data)
         if w_param3 is None:
             # 2 args version
             flags = 0
@@ -314,7 +330,7 @@
         except SocketError, e:
             raise converted_error(space, e)
         return space.wrap(count)
-    sendto_w.unwrap_spec = ['self', ObjSpace, str, W_Root, W_Root]
+    sendto_w.unwrap_spec = ['self', ObjSpace, W_Root, W_Root, W_Root]
 
     def setblocking_w(self, space, flag):
         """setblocking(flag)

Modified: pypy/dist/pypy/module/rsocket/test/test_sock_app.py
==============================================================================
--- pypy/dist/pypy/module/rsocket/test/test_sock_app.py	(original)
+++ pypy/dist/pypy/module/rsocket/test/test_sock_app.py	Sun Mar  4 12:25:17 2007
@@ -385,6 +385,22 @@
         assert s.getsockname() == s2.getsockname()
     
 
+    def test_buffer(self):
+        # Test that send/sendall/sendto accept a buffer as argument
+        import _socket, os
+        s = _socket.socket(_socket.AF_INET, _socket.SOCK_STREAM, 0)
+        # XXX temporarily we use codespeak to test, will have more robust tests in
+        # the absence of a network connection later when mroe parts of the socket
+        # API are implemented.
+        s.connect(("codespeak.net", 80))
+        s.send(buffer(''))
+        s.sendall(buffer(''))
+        s.close()
+        s = _socket.socket(_socket.AF_INET, _socket.SOCK_DGRAM, 0)
+        s.sendto(buffer(''), ('localhost', 9)) # Send to discard port.
+        s.close()
+        
+
 class AppTestSocketTCP:
     def setup_class(cls):
         cls.space = space



More information about the Pypy-commit mailing list