From 163cf5bfb472e3f1b59b8a8d7a0f9169ec04bc7f Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Fri, 19 Nov 2010 00:17:24 +0000 Subject: [PATCH] mx.DateTime module initialized as it is supposed to be. No need to pass the api pointer around. Dropped compiler warnings. --- ChangeLog | 2 ++ psycopg/adapter_mxdatetime.c | 36 +++++++++++++++++----------- psycopg/adapter_mxdatetime.h | 2 ++ psycopg/psycopgmodule.c | 9 +++---- psycopg/typecast_mxdatetime.c | 25 +++++++++++++------- psycopg/typecast_mxdatetime.h | 44 +++++++++++++++++++++++++++++++++++ 6 files changed, 92 insertions(+), 26 deletions(-) create mode 100644 psycopg/typecast_mxdatetime.h diff --git a/ChangeLog b/ChangeLog index 2305813b..ac2a64af 100644 --- a/ChangeLog +++ b/ChangeLog @@ -9,6 +9,8 @@ * datetime module initialized at is supposed to be. + * mx.DateTime module initialized at is supposed to be. + 2010-11-17 Daniele Varrazzo * psycopg/connection_type.c: don't clobber exception if diff --git a/psycopg/adapter_mxdatetime.c b/psycopg/adapter_mxdatetime.c index ffa81aac..c87d8a66 100644 --- a/psycopg/adapter_mxdatetime.c +++ b/psycopg/adapter_mxdatetime.c @@ -37,9 +37,17 @@ #include "psycopg/adapter_mxdatetime.h" #include "psycopg/microprotocols_proto.h" -/* the pointer to the mxDateTime API is initialized by the module init code, - we just need to grab it */ -extern HIDDEN mxDateTimeModule_APIObject *mxDateTimeP; +int +psyco_adapter_mxdatetime_init(void) +{ + Dprintf("psyco_adapter_mxdatetime_init: mx.DateTime init"); + + if(mxDateTime_ImportModuleAndAPI()) { + PyErr_SetString(PyExc_ImportError, "mx.DateTime initialization failed"); + return -1; + } + return 0; +} /* mxdatetime_str, mxdatetime_getquoted - return result of quoting */ @@ -300,7 +308,7 @@ psyco_Date(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "iii", &year, &month, &day)) return NULL; - mx = mxDateTimeP->DateTime_FromDateAndTime(year, month, day, 0, 0, 0.0); + mx = mxDateTime.DateTime_FromDateAndTime(year, month, day, 0, 0, 0.0); if (mx == NULL) return NULL; res = PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx, @@ -319,7 +327,7 @@ psyco_Time(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "iid", &hours, &minutes, &seconds)) return NULL; - mx = mxDateTimeP->DateTimeDelta_FromTime(hours, minutes, seconds); + mx = mxDateTime.DateTimeDelta_FromTime(hours, minutes, seconds); if (mx == NULL) return NULL; res = PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx, @@ -340,7 +348,7 @@ psyco_Timestamp(PyObject *self, PyObject *args) &hour, &minute, &second)) return NULL; - mx = mxDateTimeP->DateTime_FromDateAndTime(year, month, day, + mx = mxDateTime.DateTime_FromDateAndTime(year, month, day, hour, minute, second); if (mx == NULL) return NULL; @@ -359,7 +367,7 @@ psyco_DateFromTicks(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args,"d", &ticks)) return NULL; - if (!(mx = mxDateTimeP->DateTime_FromTicks(ticks))) + if (!(mx = mxDateTime.DateTime_FromTicks(ticks))) return NULL; res = PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx, @@ -377,10 +385,10 @@ psyco_TimeFromTicks(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args,"d", &ticks)) return NULL; - if (!(dt = mxDateTimeP->DateTime_FromTicks(ticks))) + if (!(dt = mxDateTime.DateTime_FromTicks(ticks))) return NULL; - if (!(mx = mxDateTimeP->DateTimeDelta_FromDaysAndSeconds( + if (!(mx = mxDateTime.DateTimeDelta_FromDaysAndSeconds( 0, ((mxDateTimeObject*)dt)->abstime))) { Py_DECREF(dt); @@ -403,7 +411,7 @@ psyco_TimestampFromTicks(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "d", &ticks)) return NULL; - if (!(mx = mxDateTimeP->DateTime_FromTicks(ticks))) + if (!(mx = mxDateTime.DateTime_FromTicks(ticks))) return NULL; res = PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx, @@ -419,7 +427,7 @@ psyco_DateFromMx(PyObject *self, PyObject *args) { PyObject *mx; - if (!PyArg_ParseTuple(args, "O!", mxDateTimeP->DateTime_Type, &mx)) + if (!PyArg_ParseTuple(args, "O!", mxDateTime.DateTime_Type, &mx)) return NULL; return PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx, @@ -431,7 +439,7 @@ psyco_TimeFromMx(PyObject *self, PyObject *args) { PyObject *mx; - if (!PyArg_ParseTuple(args, "O!", mxDateTimeP->DateTimeDelta_Type, &mx)) + if (!PyArg_ParseTuple(args, "O!", mxDateTime.DateTimeDelta_Type, &mx)) return NULL; return PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx, @@ -443,7 +451,7 @@ psyco_TimestampFromMx(PyObject *self, PyObject *args) { PyObject *mx; - if (!PyArg_ParseTuple(args, "O!", mxDateTimeP->DateTime_Type, &mx)) + if (!PyArg_ParseTuple(args, "O!", mxDateTime.DateTime_Type, &mx)) return NULL; return PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx, @@ -455,7 +463,7 @@ psyco_IntervalFromMx(PyObject *self, PyObject *args) { PyObject *mx; - if (!PyArg_ParseTuple(args, "O!", mxDateTimeP->DateTime_Type, &mx)) + if (!PyArg_ParseTuple(args, "O!", mxDateTime.DateTime_Type, &mx)) return NULL; return PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx, diff --git a/psycopg/adapter_mxdatetime.h b/psycopg/adapter_mxdatetime.h index 38f8f5d6..5fdeb960 100644 --- a/psycopg/adapter_mxdatetime.h +++ b/psycopg/adapter_mxdatetime.h @@ -78,6 +78,8 @@ HIDDEN PyObject *psyco_TimestampFromTicks(PyObject *module, PyObject *args); #endif /* PSYCOPG_DEFAULT_MXDATETIME */ +HIDDEN int psyco_adapter_mxdatetime_init(void); + HIDDEN PyObject *psyco_DateFromMx(PyObject *module, PyObject *args); #define psyco_DateFromMx_doc \ "DateFromMx(mx) -> new date" diff --git a/psycopg/psycopgmodule.c b/psycopg/psycopgmodule.c index 26e17e09..d0a77351 100644 --- a/psycopg/psycopgmodule.c +++ b/psycopg/psycopgmodule.c @@ -53,7 +53,7 @@ #ifdef HAVE_MXDATETIME #include #include "psycopg/adapter_mxdatetime.h" -HIDDEN mxDateTimeModule_APIObject *mxDateTimeP = NULL; +#include "psycopg/typecast_mxdatetime.h" #endif /* some module-level variables, like the datetime module */ @@ -320,9 +320,9 @@ psyco_adapters_init(PyObject *mod) #ifdef HAVE_MXDATETIME /* as above, we use the callable objects from the psycopg module */ call = PyMapping_GetItemString(mod, "TimestampFromMx"); - microprotocols_add(mxDateTimeP->DateTime_Type, NULL, call); + microprotocols_add(mxDateTime.DateTime_Type, NULL, call); call = PyMapping_GetItemString(mod, "TimeFromMx"); - microprotocols_add(mxDateTimeP->DateTimeDelta_Type, NULL, call); + microprotocols_add(mxDateTime.DateTimeDelta_Type, NULL, call); #endif } @@ -763,7 +763,8 @@ init_psycopg(void) PyErr_SetString(PyExc_ImportError, "can't import mx.DateTime module"); return; } - mxDateTimeP = &mxDateTime; + if (psyco_adapter_mxdatetime_init()) { return; } + if (psyco_typecast_mxdatetime_init()) { return; } #endif /* import python builtin datetime module, if available */ diff --git a/psycopg/typecast_mxdatetime.c b/psycopg/typecast_mxdatetime.c index 3bedbdea..a0c8b5b4 100644 --- a/psycopg/typecast_mxdatetime.c +++ b/psycopg/typecast_mxdatetime.c @@ -25,9 +25,18 @@ #include "mxDateTime.h" -/* the pointer to the mxDateTime API is initialized by the module init code, - we just need to grab it */ -extern HIDDEN mxDateTimeModule_APIObject *mxDateTimeP; +int +psyco_typecast_mxdatetime_init(void) +{ + Dprintf("psyco_typecast_mxdatetime_init: mx.DateTime init"); + + if(mxDateTime_ImportModuleAndAPI()) { + PyErr_SetString(PyExc_ImportError, "mx.DateTime initialization failed"); + return -1; + } + return 0; +} + /** DATE - cast a date into mx.DateTime python object **/ @@ -45,10 +54,10 @@ typecast_MXDATE_cast(const char *str, Py_ssize_t len, PyObject *curs) /* check for infinity */ if (!strcmp(str, "infinity") || !strcmp(str, "-infinity")) { if (str[0] == '-') { - return mxDateTimeP->DateTime_FromDateAndTime(-999998,1,1, 0,0,0); + return mxDateTime.DateTime_FromDateAndTime(-999998,1,1, 0,0,0); } else { - return mxDateTimeP->DateTime_FromDateAndTime(999999,12,31, 0,0,0); + return mxDateTime.DateTime_FromDateAndTime(999999,12,31, 0,0,0); } } @@ -75,7 +84,7 @@ typecast_MXDATE_cast(const char *str, Py_ssize_t len, PyObject *curs) Dprintf("typecast_MXDATE_cast: fractionary seconds: %lf", (double)ss + (double)us/(double)1000000.0); - return mxDateTimeP->DateTime_FromDateAndTime(y, m, d, hh, mm, + return mxDateTime.DateTime_FromDateAndTime(y, m, d, hh, mm, (double)ss + (double)us/(double)1000000.0); } @@ -102,7 +111,7 @@ typecast_MXTIME_cast(const char *str, Py_ssize_t len, PyObject *curs) Dprintf("typecast_MXTIME_cast: fractionary seconds: %lf", (double)ss + (double)us/(double)1000000.0); - return mxDateTimeP->DateTimeDelta_FromTime(hh, mm, + return mxDateTime.DateTimeDelta_FromTime(hh, mm, (double)ss + (double)us/(double)1000000.0); } @@ -225,7 +234,7 @@ typecast_MXINTERVAL_cast(const char *str, Py_ssize_t len, PyObject *curs) Dprintf("typecast_MXINTERVAL_cast: days = %ld, seconds = %f", days, seconds); - return mxDateTimeP->DateTimeDelta_FromDaysAndSeconds(days, seconds); + return mxDateTime.DateTimeDelta_FromDaysAndSeconds(days, seconds); } /* psycopg defaults to using mx types */ diff --git a/psycopg/typecast_mxdatetime.h b/psycopg/typecast_mxdatetime.h new file mode 100644 index 00000000..2c14f81d --- /dev/null +++ b/psycopg/typecast_mxdatetime.h @@ -0,0 +1,44 @@ +/* typecast_mxdatetime.h - definitions for mx.DateTime typecasters + * + * Copyright (C) 2010 Daniele Varrazzo + * + * This file is part of psycopg. + * + * psycopg2 is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * In addition, as a special exception, the copyright holders give + * permission to link this program with the OpenSSL library (or with + * modified versions of OpenSSL that use the same license as OpenSSL), + * and distribute linked combinations including the two. + * + * You must obey the GNU Lesser General Public License in all respects for + * all of the code used other than OpenSSL. + * + * psycopg2 is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + */ + +#ifndef PSYCOPG_TYPECAST_MXDATETIME_H +#define PSYCOPG_TYPECAST_MXDATETIME_H 1 + +#define PY_SSIZE_T_CLEAN +#include + +#include "psycopg/config.h" + +#ifdef __cplusplus +extern "C" { +#endif + +HIDDEN int psyco_typecast_mxdatetime_init(void); + +#ifdef __cplusplus +} +#endif + +#endif /* !defined(PSYCOPG_TYPECAST_MXDATETIME_H) */