[Python-checkins] cpython: Cleanup socketmodule.c

victor.stinner python-checkins at python.org
Mon Sep 12 05:50:28 EDT 2016


https://hg.python.org/cpython/rev/a951f8f30922
changeset:   103700:a951f8f30922
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Mon Sep 12 11:45:59 2016 +0200
summary:
  Cleanup socketmodule.c

Issue #27744:

* PEP 7: add {...} around if blocks
* assign variables and then check their value in if() to make the code easier
  to read and to debug.

files:
  Modules/socketmodule.c |  44 +++++++++++++++++++++--------
  1 files changed, 31 insertions(+), 13 deletions(-)


diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -3920,21 +3920,33 @@
 
     /* Fill in an iovec for each message part, and save the Py_buffer
        structs to release afterwards. */
-    if ((data_fast = PySequence_Fast(data_arg,
-                                     "sendmsg() argument 1 must be an "
-                                     "iterable")) == NULL)
+    data_fast = PySequence_Fast(data_arg,
+                                "sendmsg() argument 1 must be an "
+                                "iterable");
+    if (data_fast == NULL) {
         goto finally;
+    }
+
     ndataparts = PySequence_Fast_GET_SIZE(data_fast);
     if (ndataparts > INT_MAX) {
         PyErr_SetString(PyExc_OSError, "sendmsg() argument 1 is too long");
         goto finally;
     }
+
     msg->msg_iovlen = ndataparts;
-    if (ndataparts > 0 &&
-        ((msg->msg_iov = iovs = PyMem_New(struct iovec, ndataparts)) == NULL ||
-         (databufs = PyMem_New(Py_buffer, ndataparts)) == NULL)) {
-        PyErr_NoMemory();
-        goto finally;
+    if (ndataparts > 0) {
+        iovs = PyMem_New(struct iovec, ndataparts);
+        if (iovs == NULL) {
+            PyErr_NoMemory();
+            goto finally;
+        }
+        msg->msg_iov = iovs;
+
+        databufs = PyMem_New(Py_buffer, ndataparts);
+        if (iovs == NULL) {
+            PyErr_NoMemory();
+            goto finally;
+        }
     }
     for (; ndatabufs < ndataparts; ndatabufs++) {
         if (!PyArg_Parse(PySequence_Fast_GET_ITEM(data_fast, ndatabufs),
@@ -3970,7 +3982,7 @@
     Py_ssize_t i, ndatabufs = 0, ncmsgs, ncmsgbufs = 0;
     Py_buffer *databufs = NULL;
     sock_addr_t addrbuf;
-    struct msghdr msg = {0};
+    struct msghdr msg;
     struct cmsginfo {
         int level;
         int type;
@@ -3984,8 +3996,11 @@
     struct sock_sendmsg ctx;
 
     if (!PyArg_ParseTuple(args, "O|OiO:sendmsg",
-                          &data_arg, &cmsg_arg, &flags, &addr_arg))
+                          &data_arg, &cmsg_arg, &flags, &addr_arg)) {
         return NULL;
+    }
+
+    memset(&msg, 0, sizeof(msg));
 
     /* Parse destination address. */
     if (addr_arg != NULL && addr_arg != Py_None) {
@@ -4189,8 +4204,11 @@
                                      "|O$O!y*O!i:sendmsg_afalg", keywords,
                                      &data_arg,
                                      &PyLong_Type, &opobj, &iv,
-                                     &PyLong_Type, &assoclenobj, &flags))
+                                     &PyLong_Type, &assoclenobj, &flags)) {
         return NULL;
+    }
+
+    memset(&msg, 0, sizeof(msg));
 
     /* op is a required, keyword-only argument >= 0 */
     if (opobj != NULL) {
@@ -4229,7 +4247,6 @@
     }
     memset(controlbuf, 0, controllen);
 
-    memset(&msg, 0, sizeof(msg));
     msg.msg_controllen = controllen;
     msg.msg_control = controlbuf;
 
@@ -4287,8 +4304,9 @@
 
     ctx.msg = &msg;
     ctx.flags = flags;
-    if (sock_call(self, 1, sock_sendmsg_impl, &ctx) < 0)
+    if (sock_call(self, 1, sock_sendmsg_impl, &ctx) < 0) {
         goto finally;
+    }
 
     retval = PyLong_FromSsize_t(ctx.result);
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list