From 9ab38ee8c5faf1241adaec0467ff6d83d1af6434 Mon Sep 17 00:00:00 2001 From: Oleksandr Shulgin Date: Wed, 14 Oct 2015 18:39:48 +0200 Subject: [PATCH] Add psyco_curs_datetime_init --- psycopg/cursor.h | 2 ++ psycopg/cursor_type.c | 18 +++++++++++++++--- psycopg/psycopgmodule.c | 1 + 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/psycopg/cursor.h b/psycopg/cursor.h index 432425f5..3f125998 100644 --- a/psycopg/cursor.h +++ b/psycopg/cursor.h @@ -107,6 +107,8 @@ HIDDEN void curs_reset(cursorObject *self); HIDDEN int psyco_curs_withhold_set(cursorObject *self, PyObject *pyvalue); HIDDEN int psyco_curs_scrollable_set(cursorObject *self, PyObject *pyvalue); +RAISES_NEG int psyco_curs_datetime_init(void); + /* exception-raising macros */ #define EXC_IF_CURS_CLOSED(self) \ do { \ diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c index c797c264..f4598873 100644 --- a/psycopg/cursor_type.c +++ b/psycopg/cursor_type.c @@ -1780,6 +1780,21 @@ psyco_curs_flush_replication_feedback(cursorObject *self, PyObject *args, PyObje return curs_flush_replication_feedback(self, reply); } + +RAISES_NEG int +psyco_curs_datetime_init(void) +{ + Dprintf("psyco_curs_datetime_init: datetime init"); + + PyDateTime_IMPORT; + + if (!PyDateTimeAPI) { + PyErr_SetString(PyExc_ImportError, "datetime initialization failed"); + return -1; + } + return 0; +} + #define psyco_curs_replication_io_timestamp_doc \ "replication_io_timestamp -- the timestamp of latest IO with the server" @@ -1791,9 +1806,6 @@ psyco_curs_get_replication_io_timestamp(cursorObject *self) EXC_IF_CURS_CLOSED(self); - // TODO: move to a one-call init function - PyDateTime_IMPORT; - seconds = self->repl_last_io.tv_sec + self->repl_last_io.tv_usec / 1.0e6; tval = Py_BuildValue("(d)", seconds); diff --git a/psycopg/psycopgmodule.c b/psycopg/psycopgmodule.c index 543b0c1b..7d3c73d9 100644 --- a/psycopg/psycopgmodule.c +++ b/psycopg/psycopgmodule.c @@ -939,6 +939,7 @@ INIT_MODULE(_psycopg)(void) /* Initialize the PyDateTimeAPI everywhere is used */ PyDateTime_IMPORT; if (psyco_adapter_datetime_init()) { goto exit; } + if (psyco_curs_datetime_init()) { goto exit; } if (psyco_replmsg_datetime_init()) { goto exit; } Py_TYPE(&pydatetimeType) = &PyType_Type;