[pypy-commit] pypy stdlib-2.7.12: support negative read sizes w/ buffers

pjenvey pypy.commits at gmail.com
Sun Oct 2 16:57:40 EDT 2016


Author: Philip Jenvey <pjenvey at underboss.org>
Branch: stdlib-2.7.12
Changeset: r87527:f9c4e5079020
Date: 2016-10-02 13:53 -0700
http://bitbucket.org/pypy/pypy/changeset/f9c4e5079020/

Log:	support negative read sizes w/ buffers

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
@@ -407,8 +407,12 @@
 
         if w_buffer:
             rwbuffer = space.getarg_w('w*', w_buffer)
-            num_bytes = min(num_bytes, rwbuffer.getlength())
+            buflen = rwbuffer.getlength()
+            if not 0 < num_bytes <= buflen:
+                num_bytes = buflen
         else:
+            if num_bytes < 0:
+                raise oefmt(space.w_ValueError, "size should not be negative")
             rwbuffer = None
 
         with rffi.scoped_alloc_buffer(num_bytes) as buf:
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
@@ -221,6 +221,18 @@
         self.s.close()
         del ss; gc.collect()
 
+    def test_read_buffer(self):
+        import socket, gc
+        ss = socket.ssl(self.s)
+        raises(TypeError, ss.read, "foo")
+        ss.write("hello\n")
+        buf = bytearray(10)
+        read = ss.read(-1, buf)
+        assert read == 10
+        assert ss.pending() > 50 # many more bytes to read
+        self.s.close()
+        del ss; gc.collect()
+
     def test_shutdown(self):
         import socket, ssl, sys, gc
         ss = socket.ssl(self.s)


More information about the pypy-commit mailing list