diff --git a/psycopg/adapter_datetime.c b/psycopg/adapter_datetime.c index 67697fba..32cc6fcf 100644 --- a/psycopg/adapter_datetime.c +++ b/psycopg/adapter_datetime.c @@ -35,9 +35,6 @@ #include -extern HIDDEN PyObject *pyPsycopgTzModule; -extern HIDDEN PyObject *pyPsycopgTzLOCAL; - int psyco_adapter_datetime_init(void) { @@ -392,9 +389,9 @@ psyco_DateFromTicks(PyObject *self, PyObject *args) Py_DECREF(args); } } - else { - PyErr_SetString(InterfaceError, "failed localtime call"); - } + else { + PyErr_SetString(InterfaceError, "failed localtime call"); + } return res; } @@ -420,9 +417,9 @@ psyco_TimeFromTicks(PyObject *self, PyObject *args) Py_DECREF(args); } } - else { - PyErr_SetString(InterfaceError, "failed localtime call"); - } + else { + PyErr_SetString(InterfaceError, "failed localtime call"); + } return res; } @@ -430,6 +427,8 @@ psyco_TimeFromTicks(PyObject *self, PyObject *args) PyObject * psyco_TimestampFromTicks(PyObject *self, PyObject *args) { + PyObject *m = NULL; + PyObject *tz = NULL; PyObject *res = NULL; struct tm tm; time_t t; @@ -438,18 +437,25 @@ psyco_TimestampFromTicks(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "d", &ticks)) return NULL; + /* get psycopg2.tz.LOCAL from pythonland */ + if (!(m = PyImport_ImportModule("psycopg2.tz"))) { goto exit; } + if (!(tz = PyObject_GetAttrString(m, "LOCAL"))) { goto exit; } + t = (time_t)floor(ticks); ticks -= (double)t; - if (localtime_r(&t, &tm)) { - res = _psyco_Timestamp( - tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, - tm.tm_hour, tm.tm_min, (double)tm.tm_sec + ticks, - pyPsycopgTzLOCAL); + if (!localtime_r(&t, &tm)) { + PyErr_SetString(InterfaceError, "failed localtime call"); + goto exit; } - else { - PyErr_SetString(InterfaceError, "failed localtime call"); - } + res = _psyco_Timestamp( + tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, + tm.tm_hour, tm.tm_min, (double)tm.tm_sec + ticks, + tz); + +exit: + Py_DECREF(tz); + Py_XDECREF(m); return res; } diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c index 5650e437..a7591ee9 100644 --- a/psycopg/cursor_type.c +++ b/psycopg/cursor_type.c @@ -39,9 +39,6 @@ #include -extern PyObject *pyPsycopgTzFixedOffsetTimezone; - - /** DBAPI methods **/ /* close method - close the cursor */ @@ -1842,8 +1839,17 @@ cursor_setup(cursorObject *self, connectionObject *conn, const char *name) self->tuple_factory = Py_None; /* default tzinfo factory */ - Py_INCREF(pyPsycopgTzFixedOffsetTimezone); - self->tzinfo_factory = pyPsycopgTzFixedOffsetTimezone; + { + PyObject *m = NULL; + if ((m = PyImport_ImportModule("psycopg2.tz"))) { + self->tzinfo_factory = PyObject_GetAttrString( + m, "FixedOffsetTimezone"); + Py_DECREF(m); + } + if (!self->tzinfo_factory) { + return -1; + } + } Dprintf("cursor_setup: good cursor object at %p, refcnt = " FORMAT_CODE_PY_SSIZE_T, diff --git a/psycopg/psycopgmodule.c b/psycopg/psycopgmodule.c index 98cc995b..e47c0e0f 100644 --- a/psycopg/psycopgmodule.c +++ b/psycopg/psycopgmodule.c @@ -58,11 +58,6 @@ #include "psycopg/adapter_datetime.h" HIDDEN PyObject *pyDateTimeModuleP = NULL; -/* pointers to the psycopg.tz classes */ -HIDDEN PyObject *pyPsycopgTzModule = NULL; -HIDDEN PyObject *pyPsycopgTzLOCAL = NULL; -HIDDEN PyObject *pyPsycopgTzFixedOffsetTimezone = NULL; - HIDDEN PyObject *psycoEncodings = NULL; #ifdef PSYCOPG_DEBUG @@ -859,18 +854,6 @@ INIT_MODULE(_psycopg)(void) Py_TYPE(&pydatetimeType) = &PyType_Type; if (PyType_Ready(&pydatetimeType) == -1) goto exit; - /* import psycopg2.tz anyway (TODO: replace with C-level module?) */ - pyPsycopgTzModule = PyImport_ImportModule("psycopg2.tz"); - if (pyPsycopgTzModule == NULL) { - Dprintf("initpsycopg: can't import psycopg2.tz module"); - PyErr_SetString(PyExc_ImportError, "can't import psycopg2.tz module"); - goto exit; - } - pyPsycopgTzLOCAL = - PyObject_GetAttrString(pyPsycopgTzModule, "LOCAL"); - pyPsycopgTzFixedOffsetTimezone = - PyObject_GetAttrString(pyPsycopgTzModule, "FixedOffsetTimezone"); - /* initialize the module and grab module's dictionary */ #if PY_MAJOR_VERSION < 3 module = Py_InitModule("_psycopg", psycopgMethods);