[Python-checkins] r84465 - in python/branches/release31-maint: Lib/ssl.py Modules/_ssl.c

antoine.pitrou python-checkins at python.org
Fri Sep 3 20:39:47 CEST 2010


Author: antoine.pitrou
Date: Fri Sep  3 20:39:47 2010
New Revision: 84465

Log:
Merged revisions 84464 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r84464 | antoine.pitrou | 2010-09-03 20:38:17 +0200 (ven., 03 sept. 2010) | 3 lines
  
  Issue #3805: clean up implementation of the _read method in _ssl.c.
........


Modified:
   python/branches/release31-maint/   (props changed)
   python/branches/release31-maint/Lib/ssl.py
   python/branches/release31-maint/Modules/_ssl.c

Modified: python/branches/release31-maint/Lib/ssl.py
==============================================================================
--- python/branches/release31-maint/Lib/ssl.py	(original)
+++ python/branches/release31-maint/Lib/ssl.py	Fri Sep  3 20:39:47 2010
@@ -162,14 +162,14 @@
 
         self._checkClosed()
         try:
-            if buffer:
-                v = self._sslobj.read(buffer, len)
+            if buffer is not None:
+                v = self._sslobj.read(len, buffer)
             else:
                 v = self._sslobj.read(len or 1024)
             return v
         except SSLError as x:
             if x.args[0] == SSL_ERROR_EOF and self.suppress_ragged_eofs:
-                if buffer:
+                if buffer is not None:
                     return 0
                 else:
                     return b''

Modified: python/branches/release31-maint/Modules/_ssl.c
==============================================================================
--- python/branches/release31-maint/Modules/_ssl.c	(original)
+++ python/branches/release31-maint/Modules/_ssl.c	Fri Sep  3 20:39:47 2010
@@ -1277,11 +1277,9 @@
 {
     PyObject *dest = NULL;
     Py_buffer buf;
-    int buf_passed = 0;
-    int count = -1;
     char *mem;
-    /* XXX this should use Py_ssize_t */
-    int len = 1024;
+    int len, count;
+    int buf_passed = 0;
     int sockstate;
     int err;
     int nonblocking;
@@ -1295,26 +1293,28 @@
     }
     Py_INCREF(sock);
 
-    if (!PyArg_ParseTuple(args, "|Oi:read", &dest, &count))
+    buf.obj = NULL;
+    buf.buf = NULL;
+    if (!PyArg_ParseTuple(args, "i|w*:read", &len, &buf))
         goto error;
 
-    if ((dest == NULL) || (dest == Py_None)) {
-        if (!(dest = PyByteArray_FromStringAndSize((char *) 0, len)))
-            goto error;
-        mem = PyByteArray_AS_STRING(dest);
-    } else if (PyLong_Check(dest)) {
-        len = PyLong_AS_LONG(dest);
-        if (!(dest = PyByteArray_FromStringAndSize((char *) 0, len)))
-            goto error;
-        mem = PyByteArray_AS_STRING(dest);
-    } else {
-        if (PyObject_GetBuffer(dest, &buf, PyBUF_CONTIG) < 0)
+    if ((buf.buf == NULL) && (buf.obj == NULL)) {
+        dest = PyBytes_FromStringAndSize(NULL, len);
+        if (dest == NULL)
             goto error;
-        mem = buf.buf;
-        len = buf.len;
-        if ((count > 0) && (count <= len))
-            len = count;
+        mem = PyBytes_AS_STRING(dest);
+    }
+    else {
         buf_passed = 1;
+        mem = buf.buf;
+        if (len <= 0 || len > buf.len) {
+            len = (int) buf.len;
+            if (buf.len != len) {
+                PyErr_SetString(PyExc_OverflowError,
+                                "maximum length can't fit in a C 'int'");
+                goto error;
+            }
+        }
     }
 
     /* just in case the blocking state of the socket has been changed */
@@ -1375,23 +1375,24 @@
         PySSL_SetError(self, count, __FILE__, __LINE__);
         goto error;
     }
-  done:
+
+done:
     Py_DECREF(sock);
     if (!buf_passed) {
-        PyObject *res = PyBytes_FromStringAndSize(mem, count);
-        Py_DECREF(dest);
-        return res;
-    } else {
+        _PyBytes_Resize(&dest, count);
+        return dest;
+    }
+    else {
         PyBuffer_Release(&buf);
         return PyLong_FromLong(count);
     }
-  error:
+
+error:
     Py_DECREF(sock);
-    if (!buf_passed) {
+    if (!buf_passed)
         Py_XDECREF(dest);
-    } else {
+    else
         PyBuffer_Release(&buf);
-    }
     return NULL;
 }
 


More information about the Python-checkins mailing list