Fixed "historical" reference leak in TimestampFromTicks

Added an internal function with C signature to avoid the creation of a
tuple to be later unpacked. When the tuple was decref'd, Python 2.4 64
bits regularly segfaulted; Python 2.5 less regularly; don't know about
other versions.
This commit is contained in:
Daniele Varrazzo 2011-01-02 17:25:18 +01:00
parent 8b0af283f6
commit 5888b03608

View File

@ -348,20 +348,13 @@ psyco_Time(PyObject *self, PyObject *args)
return res; return res;
} }
PyObject * static PyObject *
psyco_Timestamp(PyObject *self, PyObject *args) _psyco_Timestamp(int year, int month, int day,
int hour, int minute, double second, PyObject *tzinfo)
{ {
double micro;
PyObject *obj;
PyObject *res = NULL; PyObject *res = NULL;
PyObject *tzinfo = NULL;
int year, month, day;
int hour=0, minute=0; /* default to midnight */
double micro, second=0.0;
PyObject* obj = NULL;
if (!PyArg_ParseTuple(args, "lii|iidO", &year, &month, &day,
&hour, &minute, &second, &tzinfo))
return NULL;
micro = (second - floor(second)) * 1000000.0; micro = (second - floor(second)) * 1000000.0;
second = floor(second); second = floor(second);
@ -386,6 +379,21 @@ psyco_Timestamp(PyObject *self, PyObject *args)
return res; return res;
} }
PyObject *
psyco_Timestamp(PyObject *self, PyObject *args)
{
PyObject *tzinfo = NULL;
int year, month, day;
int hour=0, minute=0; /* default to midnight */
double second=0.0;
if (!PyArg_ParseTuple(args, "lii|iidO", &year, &month, &day,
&hour, &minute, &second, &tzinfo))
return NULL;
return _psyco_Timestamp(year, month, day, hour, minute, second, tzinfo);
}
PyObject * PyObject *
psyco_DateFromTicks(PyObject *self, PyObject *args) psyco_DateFromTicks(PyObject *self, PyObject *args)
{ {
@ -446,18 +454,10 @@ psyco_TimestampFromTicks(PyObject *self, PyObject *args)
t = (time_t)floor(ticks); t = (time_t)floor(ticks);
ticks -= (double)t; ticks -= (double)t;
if (localtime_r(&t, &tm)) { if (localtime_r(&t, &tm)) {
PyObject *value = Py_BuildValue("iiiiidO", res = _psyco_Timestamp(
tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_hour, tm.tm_min, (double)tm.tm_sec + ticks,
(double)tm.tm_sec + ticks,
pyPsycopgTzLOCAL); pyPsycopgTzLOCAL);
if (value) {
/* FIXME: not decref'ing the value here is a memory leak
but, on the other hand, if we decref we get a clean nice
segfault (on my 64 bit Python 2.4 box). So this leaks
will stay until after 2.0.7 when we'll try to plug it */
res = psyco_Timestamp(self, value);
}
} }
return res; return res;