[Python-checkins] bpo-35059: Cleanup usage of Python macros (GH-10648)

Victor Stinner webhook-mailer at python.org
Wed Nov 21 21:37:54 EST 2018


https://github.com/python/cpython/commit/b37672daf61740fe1ff9d805f6d74bc5ef04012b
commit: b37672daf61740fe1ff9d805f6d74bc5ef04012b
branch: master
author: Victor Stinner <vstinner at redhat.com>
committer: GitHub <noreply at github.com>
date: 2018-11-22T03:37:50+01:00
summary:

bpo-35059: Cleanup usage of Python macros (GH-10648)

Don't pass complex expressions but regular variables to Python
macros.

* _datetimemodule.c: split single large "if" into two "if"
  in date_new(), time_new() and datetime_new().
* _pickle.c, load_extension(): flatten complex "if" expression into
  more regular C code.
* _ssl.c: addbool() now uses a temporary bool_obj to only evaluate
  the value once.
* weakrefobject.c: replace "Py_INCREF(result = proxy);"
  with "result = proxy; Py_INCREF(result);"

files:
M Modules/_datetimemodule.c
M Modules/_pickle.c
M Modules/_ssl.c
M Objects/weakrefobject.c

diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index 371bfaec3a05..0054ea83c462 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -2798,20 +2798,22 @@ date_new(PyTypeObject *type, PyObject *args, PyObject *kw)
     int day;
 
     /* Check for invocation from pickle with __getstate__ state */
-    if (PyTuple_GET_SIZE(args) == 1 &&
-        PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) &&
-        PyBytes_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE &&
-        MONTH_IS_SANE(PyBytes_AS_STRING(state)[2]))
-    {
-        PyDateTime_Date *me;
-
-        me = (PyDateTime_Date *) (type->tp_alloc(type, 0));
-        if (me != NULL) {
-            char *pdata = PyBytes_AS_STRING(state);
-            memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE);
-            me->hashcode = -1;
+    if (PyTuple_GET_SIZE(args) == 1) {
+        state = PyTuple_GET_ITEM(args, 0);
+        if (PyBytes_Check(state) &&
+            PyBytes_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE &&
+            MONTH_IS_SANE(PyBytes_AS_STRING(state)[2]))
+        {
+            PyDateTime_Date *me;
+
+            me = (PyDateTime_Date *) (type->tp_alloc(type, 0));
+            if (me != NULL) {
+                char *pdata = PyBytes_AS_STRING(state);
+                memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE);
+                me->hashcode = -1;
+            }
+            return (PyObject *)me;
         }
-        return (PyObject *)me;
     }
 
     if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws,
@@ -3913,43 +3915,46 @@ time_new(PyTypeObject *type, PyObject *args, PyObject *kw)
 
     /* Check for invocation from pickle with __getstate__ state */
     if (PyTuple_GET_SIZE(args) >= 1 &&
-        PyTuple_GET_SIZE(args) <= 2 &&
-        PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) &&
-        PyBytes_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE &&
-        (0x7F & ((unsigned char) (PyBytes_AS_STRING(state)[0]))) < 24)
+        PyTuple_GET_SIZE(args) <= 2)
     {
-        PyDateTime_Time *me;
-        char aware;
-
-        if (PyTuple_GET_SIZE(args) == 2) {
-            tzinfo = PyTuple_GET_ITEM(args, 1);
-            if (check_tzinfo_subclass(tzinfo) < 0) {
-                PyErr_SetString(PyExc_TypeError, "bad "
-                    "tzinfo state arg");
-                return NULL;
-            }
-        }
-        aware = (char)(tzinfo != Py_None);
-        me = (PyDateTime_Time *) (type->tp_alloc(type, aware));
-        if (me != NULL) {
-            char *pdata = PyBytes_AS_STRING(state);
-
-            memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE);
-            me->hashcode = -1;
-            me->hastzinfo = aware;
-            if (aware) {
-                Py_INCREF(tzinfo);
-                me->tzinfo = tzinfo;
-            }
-            if (pdata[0] & (1 << 7)) {
-                me->data[0] -= 128;
-                me->fold = 1;
+        state = PyTuple_GET_ITEM(args, 0);
+        if (PyBytes_Check(state) &&
+            PyBytes_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE &&
+            (0x7F & ((unsigned char) (PyBytes_AS_STRING(state)[0]))) < 24)
+        {
+            PyDateTime_Time *me;
+            char aware;
+
+            if (PyTuple_GET_SIZE(args) == 2) {
+                tzinfo = PyTuple_GET_ITEM(args, 1);
+                if (check_tzinfo_subclass(tzinfo) < 0) {
+                    PyErr_SetString(PyExc_TypeError, "bad "
+                        "tzinfo state arg");
+                    return NULL;
+                }
             }
-            else {
-                me->fold = 0;
+            aware = (char)(tzinfo != Py_None);
+            me = (PyDateTime_Time *) (type->tp_alloc(type, aware));
+            if (me != NULL) {
+                char *pdata = PyBytes_AS_STRING(state);
+
+                memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE);
+                me->hashcode = -1;
+                me->hastzinfo = aware;
+                if (aware) {
+                    Py_INCREF(tzinfo);
+                    me->tzinfo = tzinfo;
+                }
+                if (pdata[0] & (1 << 7)) {
+                    me->data[0] -= 128;
+                    me->fold = 1;
+                }
+                else {
+                    me->fold = 0;
+                }
             }
+            return (PyObject *)me;
         }
-        return (PyObject *)me;
     }
 
     if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO$i", time_kws,
@@ -4552,43 +4557,46 @@ datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw)
 
     /* Check for invocation from pickle with __getstate__ state */
     if (PyTuple_GET_SIZE(args) >= 1 &&
-        PyTuple_GET_SIZE(args) <= 2 &&
-        PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) &&
-        PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE &&
-        MONTH_IS_SANE(PyBytes_AS_STRING(state)[2] & 0x7F))
+        PyTuple_GET_SIZE(args) <= 2)
     {
-        PyDateTime_DateTime *me;
-        char aware;
-
-        if (PyTuple_GET_SIZE(args) == 2) {
-            tzinfo = PyTuple_GET_ITEM(args, 1);
-            if (check_tzinfo_subclass(tzinfo) < 0) {
-                PyErr_SetString(PyExc_TypeError, "bad "
-                    "tzinfo state arg");
-                return NULL;
-            }
-        }
-        aware = (char)(tzinfo != Py_None);
-        me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware));
-        if (me != NULL) {
-            char *pdata = PyBytes_AS_STRING(state);
-
-            memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE);
-            me->hashcode = -1;
-            me->hastzinfo = aware;
-            if (aware) {
-                Py_INCREF(tzinfo);
-                me->tzinfo = tzinfo;
-            }
-            if (pdata[2] & (1 << 7)) {
-                me->data[2] -= 128;
-                me->fold = 1;
+        state = PyTuple_GET_ITEM(args, 0);
+        if (PyBytes_Check(state) &&
+            PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE &&
+            MONTH_IS_SANE(PyBytes_AS_STRING(state)[2] & 0x7F))
+        {
+            PyDateTime_DateTime *me;
+            char aware;
+
+            if (PyTuple_GET_SIZE(args) == 2) {
+                tzinfo = PyTuple_GET_ITEM(args, 1);
+                if (check_tzinfo_subclass(tzinfo) < 0) {
+                    PyErr_SetString(PyExc_TypeError, "bad "
+                        "tzinfo state arg");
+                    return NULL;
+                }
             }
-            else {
-                me->fold = 0;
+            aware = (char)(tzinfo != Py_None);
+            me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware));
+            if (me != NULL) {
+                char *pdata = PyBytes_AS_STRING(state);
+
+                memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE);
+                me->hashcode = -1;
+                me->hastzinfo = aware;
+                if (aware) {
+                    Py_INCREF(tzinfo);
+                    me->tzinfo = tzinfo;
+                }
+                if (pdata[2] & (1 << 7)) {
+                    me->data[2] -= 128;
+                    me->fold = 1;
+                }
+                else {
+                    me->fold = 0;
+                }
             }
+            return (PyObject *)me;
         }
-        return (PyObject *)me;
     }
 
     if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO$i", datetime_kws,
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
index 2166d296abe4..3a7700553397 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -5858,14 +5858,20 @@ load_extension(UnpicklerObject *self, int nbytes)
     /* Since the extension registry is manipulable via Python code,
      * confirm that pair is really a 2-tuple of strings.
      */
-    if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
-        !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
-        !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
-        Py_DECREF(py_code);
-        PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
-                     "isn't a 2-tuple of strings", code);
-        return -1;
+    if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2) {
+        goto error;
+    }
+
+    module_name = PyTuple_GET_ITEM(pair, 0);
+    if (!PyUnicode_Check(module_name)) {
+        goto error;
+    }
+
+    class_name = PyTuple_GET_ITEM(pair, 1);
+    if (!PyUnicode_Check(class_name)) {
+        goto error;
     }
+
     /* Load the object. */
     obj = find_class(self, module_name, class_name);
     if (obj == NULL) {
@@ -5881,6 +5887,12 @@ load_extension(UnpicklerObject *self, int nbytes)
     }
     PDATA_PUSH(self->stack, obj, -1);
     return 0;
+
+error:
+    Py_DECREF(py_code);
+    PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
+                 "isn't a 2-tuple of strings", code);
+    return -1;
 }
 
 static int
diff --git a/Modules/_ssl.c b/Modules/_ssl.c
index 93498f475609..85819f5b0509 100644
--- a/Modules/_ssl.c
+++ b/Modules/_ssl.c
@@ -5983,9 +5983,12 @@ PyInit__ssl(void)
     PyModule_AddIntConstant(m, "PROTO_TLSv1_2", PY_PROTO_TLSv1_2);
     PyModule_AddIntConstant(m, "PROTO_TLSv1_3", PY_PROTO_TLSv1_3);
 
-#define addbool(m, v, b) \
-    Py_INCREF((b) ? Py_True : Py_False); \
-    PyModule_AddObject((m), (v), (b) ? Py_True : Py_False);
+#define addbool(m, key, value) \
+    do { \
+        PyObject *bool_obj = (value) ? Py_True : Py_False; \
+        Py_INCREF(bool_obj); \
+        PyModule_AddObject((m), (key), bool_obj); \
+    } while (0)
 
 #if HAVE_SNI
     addbool(m, "HAS_SNI", 1);
diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c
index 9f492e4b25e4..9227aa688f47 100644
--- a/Objects/weakrefobject.c
+++ b/Objects/weakrefobject.c
@@ -833,7 +833,8 @@ PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
                        to avoid violating the invariants of the list
                        of weakrefs for ob. */
                     Py_DECREF(result);
-                    Py_INCREF(result = proxy);
+                    result = proxy;
+                    Py_INCREF(result);
                     goto skip_insert;
                 }
                 prev = ref;



More information about the Python-checkins mailing list