Skip to content

Commit 41cfce9

Browse files
author
Skip Montanaro
committed
Remove PyArg_Parse usage from time module. (An extra set of eyeballs on
this would be nice. I'm a little rusty.)
1 parent b382b84 commit 41cfce9

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

Include/structseq.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ typedef struct {
3535
#define PyStructSequence_SET_ITEM(op, i, v) \
3636
(((PyStructSequence *)(op))->ob_item[i] = v)
3737

38+
#define PyStructSequence_GET_ITEM(op, i) \
39+
(((PyStructSequence *)(op))->ob_item[i])
40+
41+
3842
#ifdef __cplusplus
3943
}
4044
#endif

Modules/timemodule.c

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,29 @@ tmtotuple(struct tm *p)
254254
return v;
255255
}
256256

257+
static PyObject *
258+
structtime_totuple(PyObject *t)
259+
{
260+
PyObject *x = NULL;
261+
unsigned int i;
262+
PyObject *v = PyTuple_New(9);
263+
if (v == NULL)
264+
return NULL;
265+
266+
for (i=0; i<9; i++) {
267+
x = PyStructSequence_GET_ITEM(t, i);
268+
Py_INCREF(x);
269+
PyTuple_SET_ITEM(v, i, x);
270+
}
271+
272+
if (PyErr_Occurred()) {
273+
Py_XDECREF(v);
274+
return NULL;
275+
}
276+
277+
return v;
278+
}
279+
257280
static PyObject *
258281
time_convert(double when, struct tm * (*function)(const time_t *))
259282
{
@@ -332,18 +355,36 @@ gettmarg(PyObject *args, struct tm *p)
332355
{
333356
int y;
334357
memset((void *) p, '\0', sizeof(struct tm));
358+
PyObject *t = NULL;
335359

336-
if (!PyArg_Parse(args, "(iiiiiiiii)",
337-
&y,
338-
&p->tm_mon,
339-
&p->tm_mday,
340-
&p->tm_hour,
341-
&p->tm_min,
342-
&p->tm_sec,
343-
&p->tm_wday,
344-
&p->tm_yday,
345-
&p->tm_isdst))
360+
if (PyTuple_Check(args)) {
361+
t = args;
362+
Py_INCREF(t);
363+
}
364+
else if (Py_Type(args) == &StructTimeType) {
365+
t = structtime_totuple(args);
366+
}
367+
else {
368+
PyErr_SetString(PyExc_TypeError,
369+
"Tuple or struct_time argument required");
346370
return 0;
371+
}
372+
373+
if (t == NULL || !PyArg_ParseTuple(t, "iiiiiiiii",
374+
&y,
375+
&p->tm_mon,
376+
&p->tm_mday,
377+
&p->tm_hour,
378+
&p->tm_min,
379+
&p->tm_sec,
380+
&p->tm_wday,
381+
&p->tm_yday,
382+
&p->tm_isdst)) {
383+
Py_XDECREF(t);
384+
return 0;
385+
}
386+
Py_DECREF(t);
387+
347388
if (y < 1900) {
348389
PyObject *accept = PyDict_GetItemString(moddict,
349390
"accept2dyear");

0 commit comments

Comments
 (0)