[Python-3000-checkins] r57416 - in python/branches/py3k: Include/structseq.h Modules/timemodule.c

skip.montanaro python-3000-checkins at python.org
Fri Aug 24 23:11:01 CEST 2007


Author: skip.montanaro
Date: Fri Aug 24 23:11:00 2007
New Revision: 57416

Modified:
   python/branches/py3k/Include/structseq.h
   python/branches/py3k/Modules/timemodule.c
Log:
Remove PyArg_Parse usage from time module.  (An extra set of eyeballs on
this would be nice.  I'm a little rusty.)


Modified: python/branches/py3k/Include/structseq.h
==============================================================================
--- python/branches/py3k/Include/structseq.h	(original)
+++ python/branches/py3k/Include/structseq.h	Fri Aug 24 23:11:00 2007
@@ -35,6 +35,10 @@
 #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

Modified: python/branches/py3k/Modules/timemodule.c
==============================================================================
--- python/branches/py3k/Modules/timemodule.c	(original)
+++ python/branches/py3k/Modules/timemodule.c	Fri Aug 24 23:11:00 2007
@@ -255,6 +255,29 @@
 }
 
 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 *))
 {
 	struct tm *p;
@@ -332,18 +355,36 @@
 {
 	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");


More information about the Python-3000-checkins mailing list