3939#include "psycopg/adapter_datetime.h"
4040#include "psycopg/microprotocols_proto.h"
4141
42-
43- /* the pointer to the datetime module API is initialized by the module init
44- code, we just need to grab it */
45- extern HIDDEN PyObject * pyDateTimeModuleP ;
46- extern HIDDEN PyObject * pyDateTypeP ;
47- extern HIDDEN PyObject * pyTimeTypeP ;
48- extern HIDDEN PyObject * pyDateTimeTypeP ;
49- extern HIDDEN PyObject * pyDeltaTypeP ;
50-
5142extern HIDDEN PyObject * pyPsycopgTzModule ;
5243extern HIDDEN PyObject * pyPsycopgTzLOCAL ;
5344
45+ int
46+ psyco_adapter_datetime_init (void )
47+ {
48+ Dprintf ("psyco_adapter_datetime_init: datetime init" );
49+
50+ PyDateTime_IMPORT ;
51+
52+ if (!PyDateTimeAPI ) {
53+ PyErr_SetString (PyExc_ImportError , "datetime initialization failed" );
54+ return -1 ;
55+ }
56+ return 0 ;
57+ }
58+
5459/* datetime_str, datetime_getquoted - return result of quoting */
5560
5661static PyObject *
@@ -298,7 +303,8 @@ psyco_Date(PyObject *self, PyObject *args)
298303 if (!PyArg_ParseTuple (args , "iii" , & year , & month , & day ))
299304 return NULL ;
300305
301- obj = PyObject_CallFunction (pyDateTypeP , "iii" , year , month , day );
306+ obj = PyObject_CallFunction ((PyObject * )PyDateTimeAPI -> DateType ,
307+ "iii" , year , month , day );
302308
303309 if (obj ) {
304310 res = PyObject_CallFunction ((PyObject * )& pydatetimeType ,
@@ -327,10 +333,10 @@ psyco_Time(PyObject *self, PyObject *args)
327333 second = floor (second );
328334
329335 if (tzinfo == NULL )
330- obj = PyObject_CallFunction (pyTimeTypeP , "iiii" ,
336+ obj = PyObject_CallFunction (( PyObject * ) PyDateTimeAPI -> TimeType , "iiii" ,
331337 hours , minutes , (int )second , (int )round (micro ));
332338 else
333- obj = PyObject_CallFunction (pyTimeTypeP , "iiiiO" ,
339+ obj = PyObject_CallFunction (( PyObject * ) PyDateTimeAPI -> TimeType , "iiiiO" ,
334340 hours , minutes , (int )second , (int )round (micro ), tzinfo );
335341
336342 if (obj ) {
@@ -361,11 +367,13 @@ psyco_Timestamp(PyObject *self, PyObject *args)
361367 second = floor (second );
362368
363369 if (tzinfo == NULL )
364- obj = PyObject_CallFunction (pyDateTimeTypeP , "iiiiiii" ,
370+ obj = PyObject_CallFunction ((PyObject * )PyDateTimeAPI -> DateTimeType ,
371+ "iiiiiii" ,
365372 year , month , day , hour , minute , (int )second ,
366373 (int )round (micro ));
367374 else
368- obj = PyObject_CallFunction (pyDateTimeTypeP , "iiiiiiiO" ,
375+ obj = PyObject_CallFunction ((PyObject * )PyDateTimeAPI -> DateTimeType ,
376+ "iiiiiiiO" ,
369377 year , month , day , hour , minute , (int )second ,
370378 (int )round (micro ), tzinfo );
371379
@@ -462,7 +470,7 @@ psyco_DateFromPy(PyObject *self, PyObject *args)
462470{
463471 PyObject * obj ;
464472
465- if (!PyArg_ParseTuple (args , "O!" , pyDateTypeP , & obj ))
473+ if (!PyArg_ParseTuple (args , "O!" , PyDateTimeAPI -> DateType , & obj ))
466474 return NULL ;
467475
468476 return PyObject_CallFunction ((PyObject * )& pydatetimeType , "Oi" , obj ,
@@ -474,7 +482,7 @@ psyco_TimeFromPy(PyObject *self, PyObject *args)
474482{
475483 PyObject * obj ;
476484
477- if (!PyArg_ParseTuple (args , "O!" , pyTimeTypeP , & obj ))
485+ if (!PyArg_ParseTuple (args , "O!" , PyDateTimeAPI -> TimeType , & obj ))
478486 return NULL ;
479487
480488 return PyObject_CallFunction ((PyObject * )& pydatetimeType , "Oi" , obj ,
@@ -486,7 +494,7 @@ psyco_TimestampFromPy(PyObject *self, PyObject *args)
486494{
487495 PyObject * obj ;
488496
489- if (!PyArg_ParseTuple (args , "O!" , pyDateTimeTypeP , & obj ))
497+ if (!PyArg_ParseTuple (args , "O!" , PyDateTimeAPI -> DateTimeType , & obj ))
490498 return NULL ;
491499
492500 return PyObject_CallFunction ((PyObject * )& pydatetimeType , "Oi" , obj ,
@@ -498,7 +506,7 @@ psyco_IntervalFromPy(PyObject *self, PyObject *args)
498506{
499507 PyObject * obj ;
500508
501- if (!PyArg_ParseTuple (args , "O!" , pyDeltaTypeP , & obj ))
509+ if (!PyArg_ParseTuple (args , "O!" , PyDateTimeAPI -> DeltaType , & obj ))
502510 return NULL ;
503511
504512 return PyObject_CallFunction ((PyObject * )& pydatetimeType , "Oi" , obj ,
0 commit comments