]> granicus.if.org Git - python/commitdiff
build_struct_time() uses Py_BuildValue()
authorVictor Stinner <victor.stinner@gmail.com>
Thu, 8 Dec 2016 23:38:16 +0000 (00:38 +0100)
committerVictor Stinner <victor.stinner@gmail.com>
Thu, 8 Dec 2016 23:38:16 +0000 (00:38 +0100)
Issue #28915: Avoid calling _PyObject_CallMethodId() with "(...)" format to
avoid the creation of a temporary tuple: use Py_BuildValue() with
_PyObject_CallMethodIdObjArgs().

Modules/_datetimemodule.c

index 0999218693ce3c973e464ca7c945ca044efba61e..77caba1ddce540a62ec106009a0b58a8365399d7 100644 (file)
@@ -1385,21 +1385,30 @@ static PyObject *
 build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag)
 {
     PyObject *time;
-    PyObject *result = NULL;
+    PyObject *result;
+    _Py_IDENTIFIER(struct_time);
+    PyObject *args;
+
 
     time = PyImport_ImportModuleNoBlock("time");
-    if (time != NULL) {
-        _Py_IDENTIFIER(struct_time);
-
-        result = _PyObject_CallMethodId(time, &PyId_struct_time,
-                                        "((iiiiiiiii))",
-                                        y, m, d,
-                                        hh, mm, ss,
-                                        weekday(y, m, d),
-                                        days_before_month(y, m) + d,
-                                        dstflag);
+    if (time == NULL) {
+        return NULL;
+    }
+
+    args = Py_BuildValue("iiiiiiiii",
+                         y, m, d,
+                         hh, mm, ss,
+                         weekday(y, m, d),
+                         days_before_month(y, m) + d,
+                         dstflag);
+    if (args == NULL) {
         Py_DECREF(time);
+        return NULL;
     }
+
+    result = _PyObject_CallMethodIdObjArgs(time, &PyId_struct_time,
+                                           args, NULL);
+    Py_DECREF(time);
     return result;
 }