mx.DateTime module initialized as it is supposed to be.

No need to pass the api pointer around. Dropped compiler warnings.
This commit is contained in:
Daniele Varrazzo 2010-11-19 00:17:24 +00:00
parent 3e3aa676a9
commit 163cf5bfb4
6 changed files with 92 additions and 26 deletions

View File

@ -9,6 +9,8 @@
* datetime module initialized at is supposed to be. * datetime module initialized at is supposed to be.
* mx.DateTime module initialized at is supposed to be.
2010-11-17 Daniele Varrazzo <daniele.varrazzo@gmail.com> 2010-11-17 Daniele Varrazzo <daniele.varrazzo@gmail.com>
* psycopg/connection_type.c: don't clobber exception if * psycopg/connection_type.c: don't clobber exception if

View File

@ -37,9 +37,17 @@
#include "psycopg/adapter_mxdatetime.h" #include "psycopg/adapter_mxdatetime.h"
#include "psycopg/microprotocols_proto.h" #include "psycopg/microprotocols_proto.h"
/* the pointer to the mxDateTime API is initialized by the module init code, int
we just need to grab it */ psyco_adapter_mxdatetime_init(void)
extern HIDDEN mxDateTimeModule_APIObject *mxDateTimeP; {
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 */ /* 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)) if (!PyArg_ParseTuple(args, "iii", &year, &month, &day))
return NULL; 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; if (mx == NULL) return NULL;
res = PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx, 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)) if (!PyArg_ParseTuple(args, "iid", &hours, &minutes, &seconds))
return NULL; return NULL;
mx = mxDateTimeP->DateTimeDelta_FromTime(hours, minutes, seconds); mx = mxDateTime.DateTimeDelta_FromTime(hours, minutes, seconds);
if (mx == NULL) return NULL; if (mx == NULL) return NULL;
res = PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx, res = PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx,
@ -340,7 +348,7 @@ psyco_Timestamp(PyObject *self, PyObject *args)
&hour, &minute, &second)) &hour, &minute, &second))
return NULL; return NULL;
mx = mxDateTimeP->DateTime_FromDateAndTime(year, month, day, mx = mxDateTime.DateTime_FromDateAndTime(year, month, day,
hour, minute, second); hour, minute, second);
if (mx == NULL) return NULL; if (mx == NULL) return NULL;
@ -359,7 +367,7 @@ psyco_DateFromTicks(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args,"d", &ticks)) if (!PyArg_ParseTuple(args,"d", &ticks))
return NULL; return NULL;
if (!(mx = mxDateTimeP->DateTime_FromTicks(ticks))) if (!(mx = mxDateTime.DateTime_FromTicks(ticks)))
return NULL; return NULL;
res = PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx, res = PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx,
@ -377,10 +385,10 @@ psyco_TimeFromTicks(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args,"d", &ticks)) if (!PyArg_ParseTuple(args,"d", &ticks))
return NULL; return NULL;
if (!(dt = mxDateTimeP->DateTime_FromTicks(ticks))) if (!(dt = mxDateTime.DateTime_FromTicks(ticks)))
return NULL; return NULL;
if (!(mx = mxDateTimeP->DateTimeDelta_FromDaysAndSeconds( if (!(mx = mxDateTime.DateTimeDelta_FromDaysAndSeconds(
0, ((mxDateTimeObject*)dt)->abstime))) 0, ((mxDateTimeObject*)dt)->abstime)))
{ {
Py_DECREF(dt); Py_DECREF(dt);
@ -403,7 +411,7 @@ psyco_TimestampFromTicks(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "d", &ticks)) if (!PyArg_ParseTuple(args, "d", &ticks))
return NULL; return NULL;
if (!(mx = mxDateTimeP->DateTime_FromTicks(ticks))) if (!(mx = mxDateTime.DateTime_FromTicks(ticks)))
return NULL; return NULL;
res = PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx, res = PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx,
@ -419,7 +427,7 @@ psyco_DateFromMx(PyObject *self, PyObject *args)
{ {
PyObject *mx; PyObject *mx;
if (!PyArg_ParseTuple(args, "O!", mxDateTimeP->DateTime_Type, &mx)) if (!PyArg_ParseTuple(args, "O!", mxDateTime.DateTime_Type, &mx))
return NULL; return NULL;
return PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx, return PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx,
@ -431,7 +439,7 @@ psyco_TimeFromMx(PyObject *self, PyObject *args)
{ {
PyObject *mx; PyObject *mx;
if (!PyArg_ParseTuple(args, "O!", mxDateTimeP->DateTimeDelta_Type, &mx)) if (!PyArg_ParseTuple(args, "O!", mxDateTime.DateTimeDelta_Type, &mx))
return NULL; return NULL;
return PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx, return PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx,
@ -443,7 +451,7 @@ psyco_TimestampFromMx(PyObject *self, PyObject *args)
{ {
PyObject *mx; PyObject *mx;
if (!PyArg_ParseTuple(args, "O!", mxDateTimeP->DateTime_Type, &mx)) if (!PyArg_ParseTuple(args, "O!", mxDateTime.DateTime_Type, &mx))
return NULL; return NULL;
return PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx, return PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx,
@ -455,7 +463,7 @@ psyco_IntervalFromMx(PyObject *self, PyObject *args)
{ {
PyObject *mx; PyObject *mx;
if (!PyArg_ParseTuple(args, "O!", mxDateTimeP->DateTime_Type, &mx)) if (!PyArg_ParseTuple(args, "O!", mxDateTime.DateTime_Type, &mx))
return NULL; return NULL;
return PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx, return PyObject_CallFunction((PyObject *)&mxdatetimeType, "Oi", mx,

View File

@ -78,6 +78,8 @@ HIDDEN PyObject *psyco_TimestampFromTicks(PyObject *module, PyObject *args);
#endif /* PSYCOPG_DEFAULT_MXDATETIME */ #endif /* PSYCOPG_DEFAULT_MXDATETIME */
HIDDEN int psyco_adapter_mxdatetime_init(void);
HIDDEN PyObject *psyco_DateFromMx(PyObject *module, PyObject *args); HIDDEN PyObject *psyco_DateFromMx(PyObject *module, PyObject *args);
#define psyco_DateFromMx_doc \ #define psyco_DateFromMx_doc \
"DateFromMx(mx) -> new date" "DateFromMx(mx) -> new date"

View File

@ -53,7 +53,7 @@
#ifdef HAVE_MXDATETIME #ifdef HAVE_MXDATETIME
#include <mxDateTime.h> #include <mxDateTime.h>
#include "psycopg/adapter_mxdatetime.h" #include "psycopg/adapter_mxdatetime.h"
HIDDEN mxDateTimeModule_APIObject *mxDateTimeP = NULL; #include "psycopg/typecast_mxdatetime.h"
#endif #endif
/* some module-level variables, like the datetime module */ /* some module-level variables, like the datetime module */
@ -320,9 +320,9 @@ psyco_adapters_init(PyObject *mod)
#ifdef HAVE_MXDATETIME #ifdef HAVE_MXDATETIME
/* as above, we use the callable objects from the psycopg module */ /* as above, we use the callable objects from the psycopg module */
call = PyMapping_GetItemString(mod, "TimestampFromMx"); call = PyMapping_GetItemString(mod, "TimestampFromMx");
microprotocols_add(mxDateTimeP->DateTime_Type, NULL, call); microprotocols_add(mxDateTime.DateTime_Type, NULL, call);
call = PyMapping_GetItemString(mod, "TimeFromMx"); call = PyMapping_GetItemString(mod, "TimeFromMx");
microprotocols_add(mxDateTimeP->DateTimeDelta_Type, NULL, call); microprotocols_add(mxDateTime.DateTimeDelta_Type, NULL, call);
#endif #endif
} }
@ -763,7 +763,8 @@ init_psycopg(void)
PyErr_SetString(PyExc_ImportError, "can't import mx.DateTime module"); PyErr_SetString(PyExc_ImportError, "can't import mx.DateTime module");
return; return;
} }
mxDateTimeP = &mxDateTime; if (psyco_adapter_mxdatetime_init()) { return; }
if (psyco_typecast_mxdatetime_init()) { return; }
#endif #endif
/* import python builtin datetime module, if available */ /* import python builtin datetime module, if available */

View File

@ -25,9 +25,18 @@
#include "mxDateTime.h" #include "mxDateTime.h"
/* the pointer to the mxDateTime API is initialized by the module init code, int
we just need to grab it */ psyco_typecast_mxdatetime_init(void)
extern HIDDEN mxDateTimeModule_APIObject *mxDateTimeP; {
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 **/ /** 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 */ /* check for infinity */
if (!strcmp(str, "infinity") || !strcmp(str, "-infinity")) { if (!strcmp(str, "infinity") || !strcmp(str, "-infinity")) {
if (str[0] == '-') { if (str[0] == '-') {
return mxDateTimeP->DateTime_FromDateAndTime(-999998,1,1, 0,0,0); return mxDateTime.DateTime_FromDateAndTime(-999998,1,1, 0,0,0);
} }
else { 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", Dprintf("typecast_MXDATE_cast: fractionary seconds: %lf",
(double)ss + (double)us/(double)1000000.0); (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); (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", Dprintf("typecast_MXTIME_cast: fractionary seconds: %lf",
(double)ss + (double)us/(double)1000000.0); (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); (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", Dprintf("typecast_MXINTERVAL_cast: days = %ld, seconds = %f",
days, seconds); days, seconds);
return mxDateTimeP->DateTimeDelta_FromDaysAndSeconds(days, seconds); return mxDateTime.DateTimeDelta_FromDaysAndSeconds(days, seconds);
} }
/* psycopg defaults to using mx types */ /* psycopg defaults to using mx types */

View File

@ -0,0 +1,44 @@
/* typecast_mxdatetime.h - definitions for mx.DateTime typecasters
*
* Copyright (C) 2010 Daniele Varrazzo <daniele.varrazzo@gmail.com>
*
* 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 <Python.h>
#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) */