]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Remove PyArg_Parse usage from time module. (An extra set of eyeballs on
authorSkip Montanaro <skip@pobox.com>
Fri, 24 Aug 2007 21:11:00 +0000 (21:11 +0000)
committerSkip Montanaro <skip@pobox.com>
Fri, 24 Aug 2007 21:11:00 +0000 (21:11 +0000)
this would be nice.  I'm a little rusty.)

Include/structseq.h
Modules/timemodule.c

index e662916fe4ca157404f5a7188c65aa0057964750..a482bd8491ada758d33163ab4bfe6859a7db8c33 100644 (file)
@@ -35,6 +35,10 @@ typedef struct {
 #define PyStructSequence_SET_ITEM(op, i, v) \
        (((PyStructSequence *)(op))->ob_item[i] = v)
 
+#define PyStructSequence_GET_ITEM(op, i) \
+       (((PyStructSequence *)(op))->ob_item[i])
+
+
 #ifdef __cplusplus
 }
 #endif
index 8ce6667cb601bf0ae1735728386734e8e2faed10..c7acf3a9b548e649ad00fb93b5bbe4e4b6660698 100644 (file)
@@ -254,6 +254,29 @@ tmtotuple(struct tm *p)
        return v;
 }
 
+static PyObject *
+structtime_totuple(PyObject *t)
+{
+       PyObject *x = NULL;
+       unsigned int i;
+       PyObject *v = PyTuple_New(9);
+       if (v == NULL)
+               return NULL;
+
+       for (i=0; i<9; i++) {
+               x = PyStructSequence_GET_ITEM(t, i);
+               Py_INCREF(x);
+               PyTuple_SET_ITEM(v, i, x);
+       }
+
+       if (PyErr_Occurred()) {
+               Py_XDECREF(v);
+               return NULL;
+       }
+
+       return v;
+}
+
 static PyObject *
 time_convert(double when, struct tm * (*function)(const time_t *))
 {
@@ -332,18 +355,36 @@ gettmarg(PyObject *args, struct tm *p)
 {
        int y;
        memset((void *) p, '\0', sizeof(struct tm));
+       PyObject *t = NULL;
 
-       if (!PyArg_Parse(args, "(iiiiiiiii)",
-                        &y,
-                        &p->tm_mon,
-                        &p->tm_mday,
-                        &p->tm_hour,
-                        &p->tm_min,
-                        &p->tm_sec,
-                        &p->tm_wday,
-                        &p->tm_yday,
-                        &p->tm_isdst))
+       if (PyTuple_Check(args)) {
+               t = args;
+               Py_INCREF(t);
+       }
+       else if (Py_Type(args) == &StructTimeType) {
+               t = structtime_totuple(args);
+       }
+       else {
+               PyErr_SetString(PyExc_TypeError,
+                               "Tuple or struct_time argument required");
                return 0;
+       }
+
+       if (t == NULL || !PyArg_ParseTuple(t, "iiiiiiiii",
+                                          &y,
+                                          &p->tm_mon,
+                                          &p->tm_mday,
+                                          &p->tm_hour,
+                                          &p->tm_min,
+                                          &p->tm_sec,
+                                          &p->tm_wday,
+                                          &p->tm_yday,
+                                          &p->tm_isdst)) {
+               Py_XDECREF(t);
+               return 0;
+       }
+       Py_DECREF(t);
+
        if (y < 1900) {
                PyObject *accept = PyDict_GetItemString(moddict,
                                                        "accept2dyear");