Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3k
Changeset: r58771:bf18a5b4ac22
Date: 2012-11-07 00:14 +0100
http://bitbucket.org/pypy/pypy/changeset/bf18a5b4ac22/

Log:    _ssl._SSLSocket.read() is also a readinto method.

diff --git a/pypy/module/_ssl/interp_ssl.py b/pypy/module/_ssl/interp_ssl.py
--- a/pypy/module/_ssl/interp_ssl.py
+++ b/pypy/module/_ssl/interp_ssl.py
@@ -400,7 +400,7 @@
         return space.wrap(count)
 
     @unwrap_spec(num_bytes=int)
-    def read(self, space, num_bytes=1024):
+    def read(self, space, num_bytes, w_buf=None):
         """read([len]) -> string
 
         Read up to len bytes from the SSL socket."""
@@ -419,6 +419,12 @@
                     return space.wrapbytes('')
                 raise ssl_error(space, "Socket closed without SSL shutdown 
handshake")
 
+        rwbuffer = None
+        if not space.is_none(w_buf):
+            rwbuffer = space.rwbuffer_w(w_buf)
+            lgt = rwbuffer.getlength()
+            if num_bytes < 0 or num_bytes > lgt:
+                num_bytes = lgt
         raw_buf, gc_buf = rffi.alloc_buffer(num_bytes)
         while True:
             err = 0
@@ -453,7 +459,11 @@
 
         result = rffi.str_from_buffer(raw_buf, gc_buf, num_bytes, count)
         rffi.keep_buffer_alive_until_here(raw_buf, gc_buf)
-        return space.wrapbytes(result)
+        if rwbuffer is not None:
+            rwbuffer.setslice(0, result)
+            return space.wrap(count)
+        else:
+            return space.wrapbytes(result)
 
     def _get_socket(self, space):
         w_socket = self.w_socket()
diff --git a/pypy/module/_ssl/test/test_ssl.py 
b/pypy/module/_ssl/test/test_ssl.py
--- a/pypy/module/_ssl/test/test_ssl.py
+++ b/pypy/module/_ssl/test/test_ssl.py
@@ -118,6 +118,16 @@
         self.s.close()
         del ss; gc.collect()
 
+    def test_read_into(self):
+        import ssl, gc
+        ss = ssl.wrap_socket(self.s)
+        ss.write(b"hello\n")
+        b = bytearray(8)
+        read = ss.read(10, b)
+        assert read == 8
+        self.s.close()
+        del ss; gc.collect()
+
     def test_shutdown(self):
         import socket, ssl, sys, gc
         ss = ssl.wrap_socket(self.s)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to