diff --git a/psycopg/psycopgmodule.c b/psycopg/psycopgmodule.c index 6c658fcb..d991a6ac 100644 --- a/psycopg/psycopgmodule.c +++ b/psycopg/psycopgmodule.c @@ -47,7 +47,6 @@ HIDDEN mxDateTimeModule_APIObject *mxDateTimeP = NULL; #endif /* some module-level variables, like the datetime module */ -#ifdef HAVE_PYDATETIME #include #include "psycopg/adapter_datetime.h" HIDDEN PyObject *pyDateTimeModuleP = NULL; @@ -55,7 +54,6 @@ HIDDEN PyObject *pyDateTypeP = NULL; HIDDEN PyObject *pyTimeTypeP = NULL; HIDDEN PyObject *pyDateTimeTypeP = NULL; HIDDEN PyObject *pyDeltaTypeP = NULL; -#endif /* pointers to the psycopg.tz classes */ HIDDEN PyObject *pyPsycopgTzModule = NULL; @@ -276,22 +274,13 @@ psyco_adapters_init(PyObject *mod) microprotocols_add(&PyFloat_Type, NULL, (PyObject*)&asisType); microprotocols_add(&PyInt_Type, NULL, (PyObject*)&asisType); microprotocols_add(&PyLong_Type, NULL, (PyObject*)&asisType); + microprotocols_add(&PyBool_Type, NULL, (PyObject*)&pbooleanType); microprotocols_add(&PyString_Type, NULL, (PyObject*)&qstringType); microprotocols_add(&PyUnicode_Type, NULL, (PyObject*)&qstringType); microprotocols_add(&PyBuffer_Type, NULL, (PyObject*)&binaryType); microprotocols_add(&PyList_Type, NULL, (PyObject*)&listType); -#ifdef HAVE_MXDATETIME - /* the module has already been initialized, so we can obtain the callable - objects directly from its dictionary :) */ - call = PyMapping_GetItemString(mod, "TimestampFromMx"); - microprotocols_add(mxDateTimeP->DateTime_Type, NULL, call); - call = PyMapping_GetItemString(mod, "TimeFromMx"); - microprotocols_add(mxDateTimeP->DateTimeDelta_Type, NULL, call); -#endif - -#ifdef HAVE_PYDATETIME /* as above, we use the callable objects from the psycopg module */ call = PyMapping_GetItemString(mod, "DateFromPy"); microprotocols_add((PyTypeObject*)pyDateTypeP, NULL, call); @@ -301,10 +290,14 @@ psyco_adapters_init(PyObject *mod) microprotocols_add((PyTypeObject*)pyDateTimeTypeP, NULL, call); call = PyMapping_GetItemString(mod, "IntervalFromPy"); microprotocols_add((PyTypeObject*)pyDeltaTypeP, NULL, call); -#endif -#ifdef HAVE_PYBOOL - microprotocols_add(&PyBool_Type, NULL, (PyObject*)&pbooleanType); +#ifdef HAVE_MXDATETIME + /* the module has already been initialized, so we can obtain the callable + objects directly from its dictionary :) */ + call = PyMapping_GetItemString(mod, "TimestampFromMx"); + microprotocols_add(mxDateTimeP->DateTime_Type, NULL, call); + call = PyMapping_GetItemString(mod, "TimeFromMx"); + microprotocols_add(mxDateTimeP->DateTimeDelta_Type, NULL, call); #endif #ifdef HAVE_DECIMAL @@ -659,6 +652,15 @@ static PyMethodDef psycopgMethods[] = { {"List", (PyCFunction)psyco_List, METH_VARARGS, psyco_List_doc}, + {"DateFromPy", (PyCFunction)psyco_DateFromPy, + METH_VARARGS, psyco_DateFromPy_doc}, + {"TimeFromPy", (PyCFunction)psyco_TimeFromPy, + METH_VARARGS, psyco_TimeFromPy_doc}, + {"TimestampFromPy", (PyCFunction)psyco_TimestampFromPy, + METH_VARARGS, psyco_TimestampFromPy_doc}, + {"IntervalFromPy", (PyCFunction)psyco_IntervalFromPy, + METH_VARARGS, psyco_IntervalFromPy_doc}, + #ifdef HAVE_MXDATETIME {"DateFromMx", (PyCFunction)psyco_DateFromMx, METH_VARARGS, psyco_DateFromMx_doc}, @@ -670,17 +672,6 @@ static PyMethodDef psycopgMethods[] = { METH_VARARGS, psyco_IntervalFromMx_doc}, #endif -#ifdef HAVE_PYDATETIME - {"DateFromPy", (PyCFunction)psyco_DateFromPy, - METH_VARARGS, psyco_DateFromPy_doc}, - {"TimeFromPy", (PyCFunction)psyco_TimeFromPy, - METH_VARARGS, psyco_TimeFromPy_doc}, - {"TimestampFromPy", (PyCFunction)psyco_TimestampFromPy, - METH_VARARGS, psyco_TimestampFromPy_doc}, - {"IntervalFromPy", (PyCFunction)psyco_IntervalFromPy, - METH_VARARGS, psyco_IntervalFromPy_doc}, -#endif - {NULL, NULL, 0, NULL} /* Sentinel */ }; @@ -694,7 +685,7 @@ init_psycopg(void) #ifdef PSYCOPG_DEBUG if (getenv("PSYCOPG_DEBUG")) - psycopg_debug_enabled = 1; + psycopg_debug_enabled = 1; #endif Dprintf("initpsycopg: initializing psycopg %s", PSYCOPG_VERSION); @@ -706,6 +697,7 @@ init_psycopg(void) qstringType.ob_type = &PyType_Type; binaryType.ob_type = &PyType_Type; isqlquoteType.ob_type = &PyType_Type; + pbooleanType.ob_type = &PyType_Type; asisType.ob_type = &PyType_Type; listType.ob_type = &PyType_Type; chunkType.ob_type = &PyType_Type; @@ -716,6 +708,7 @@ init_psycopg(void) if (PyType_Ready(&qstringType) == -1) return; if (PyType_Ready(&binaryType) == -1) return; if (PyType_Ready(&isqlquoteType) == -1) return; + if (PyType_Ready(&pbooleanType) == -1) return; if (PyType_Ready(&asisType) == -1) return; if (PyType_Ready(&listType) == -1) return; if (PyType_Ready(&chunkType) == -1) return; @@ -725,11 +718,6 @@ init_psycopg(void) if (PyType_Ready(&lobjectType) == -1) return; #endif -#ifdef HAVE_PYBOOL - pbooleanType.ob_type = &PyType_Type; - if (PyType_Ready(&pbooleanType) == -1) return; -#endif - /* import mx.DateTime module, if necessary */ #ifdef HAVE_MXDATETIME mxdatetimeType.ob_type = &PyType_Type; @@ -743,7 +731,6 @@ init_psycopg(void) #endif /* import python builtin datetime module, if available */ -#ifdef HAVE_PYDATETIME pyDateTimeModuleP = PyImport_ImportModule("datetime"); if (pyDateTimeModuleP == NULL) { Dprintf("initpsycopg: can't import datetime module"); @@ -759,7 +746,6 @@ init_psycopg(void) pyTimeTypeP = PyObject_GetAttrString(pyDateTimeModuleP, "time"); pyDateTimeTypeP = PyObject_GetAttrString(pyDateTimeModuleP, "datetime"); pyDeltaTypeP = PyObject_GetAttrString(pyDateTimeModuleP, "timedelta"); -#endif /* import psycopg2.tz anyway (TODO: replace with C-level module?) */ pyPsycopgTzModule = PyImport_ImportModule("psycopg2.tz"); @@ -828,14 +814,12 @@ init_psycopg(void) qstringType.tp_alloc = PyType_GenericAlloc; listType.tp_alloc = PyType_GenericAlloc; chunkType.tp_alloc = PyType_GenericAlloc; + pydatetimeType.tp_alloc = PyType_GenericAlloc; #ifdef PSYCOPG_EXTENSIONS lobjectType.tp_alloc = PyType_GenericAlloc; #endif -#ifdef HAVE_PYDATETIME - pydatetimeType.tp_alloc = PyType_GenericAlloc; -#endif #ifdef HAVE_MXDATETIME mxdatetimeType.tp_alloc = PyType_GenericAlloc; diff --git a/psycopg/typecast.c b/psycopg/typecast.c index f7869608..dcd9b376 100644 --- a/psycopg/typecast.c +++ b/psycopg/typecast.c @@ -167,21 +167,17 @@ typecast_parse_time(const char* s, const char** t, Py_ssize_t* len, /** include casting objects **/ #include "psycopg/typecast_basic.c" #include "psycopg/typecast_binary.c" +#include "psycopg/typecast_datetime.c" #ifdef HAVE_MXDATETIME #include "psycopg/typecast_mxdatetime.c" #endif -#ifdef HAVE_PYDATETIME -#include "psycopg/typecast_datetime.c" -#endif - #include "psycopg/typecast_array.c" #include "psycopg/typecast_builtins.c" /* a list of initializers, used to make the typecasters accessible anyway */ -#ifdef HAVE_PYDATETIME static typecastObject_initlist typecast_pydatetime[] = { {"PYDATETIME", typecast_DATETIME_types, typecast_PYDATETIME_cast}, {"PYTIME", typecast_TIME_types, typecast_PYTIME_cast}, @@ -189,7 +185,6 @@ static typecastObject_initlist typecast_pydatetime[] = { {"PYINTERVAL", typecast_INTERVAL_types, typecast_PYINTERVAL_cast}, {NULL, NULL, NULL} }; -#endif /* a list of initializers, used to make the typecasters accessible anyway */ #ifdef HAVE_MXDATETIME @@ -267,7 +262,6 @@ typecast_init(PyObject *dict) PyDict_SetItem(dict, t->name, (PyObject *)t); } #endif -#ifdef HAVE_PYDATETIME for (i = 0; typecast_pydatetime[i].name != NULL; i++) { typecastObject *t; Dprintf("typecast_init: initializing %s", typecast_pydatetime[i].name); @@ -275,7 +269,6 @@ typecast_init(PyObject *dict) if (t == NULL) return -1; PyDict_SetItem(dict, t->name, (PyObject *)t); } -#endif return 0; } diff --git a/setup.py b/setup.py index 79a90932..80c3e0f3 100644 --- a/setup.py +++ b/setup.py @@ -46,7 +46,7 @@ Operating System :: Unix import os import os.path import sys -import popen2 +import subprocess import ConfigParser from distutils.core import setup, Extension from distutils.errors import DistutilsFileError @@ -55,23 +55,19 @@ from distutils.sysconfig import get_python_inc from distutils.ccompiler import get_default_compiler PSYCOPG_VERSION = '2.0.8' -version_flags = [] +version_flags = ['dt'] PLATFORM_IS_WINDOWS = sys.platform.lower().startswith('win') -# to work around older distutil limitations -if sys.version < '2.2.3': - from distutils.dist import DistributionMetadata - DistributionMetadata.classifiers = None - DistributionMetadata.download_url = None - def get_pg_config(kind, pg_config="pg_config"): - if ' ' in pg_config: - pg_config = '"'+pg_config+'"' - p = popen2.popen3(pg_config + " --" + kind) - r = p[0].readline().strip() + p = subprocess.Popen([pg_config, "--" + kind], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + p.stdin.close() + r = p.stdout.readline().strip() if not r: - raise Warning(p[2].readline()) + raise Warning(p.stderr.readline()) return r class psycopg_build_ext(build_ext): @@ -311,14 +307,6 @@ class psycopg_build_ext(build_ext): define_macros = [] include_dirs = [] -# python version -define_macros.append(('PY_MAJOR_VERSION', str(sys.version_info[0]))) -define_macros.append(('PY_MINOR_VERSION', str(sys.version_info[1]))) - -# some macros related to python versions and features -if sys.version_info[0] >= 2 and sys.version_info[1] >= 3: - define_macros.append(('HAVE_PYBOOL','1')) - # gather information to build the extension module ext = [] ; data_files = [] @@ -330,7 +318,7 @@ sources = [ 'connection_type.c', 'connection_int.c', 'cursor_type.c', 'cursor_int.c', 'lobject_type.c', 'lobject_int.c', 'adapter_qstring.c', 'adapter_pboolean.c', 'adapter_binary.c', - 'adapter_asis.c', 'adapter_list.c', 'utils.c'] + 'adapter_asis.c', 'adapter_list.c', 'adapter_datetime.c', 'utils.c'] parser = ConfigParser.ConfigParser() parser.read('setup.cfg') @@ -343,7 +331,7 @@ if sys.version_info[0] >= 2 and ( version_flags.append('dec') # Choose a datetime module -have_pydatetime = False +have_pydatetime = True have_mxdatetime = False use_pydatetime = int(parser.get('build_ext', 'use_pydatetime')) @@ -359,16 +347,8 @@ if os.path.exists(mxincludedir): have_mxdatetime = True version_flags.append('mx') -# check for python datetime package -if os.path.exists(os.path.join(get_python_inc(plat_specific=1),"datetime.h")): - define_macros.append(('HAVE_PYDATETIME','1')) - sources.append('adapter_datetime.c') - have_pydatetime = True - version_flags.append('dt') - # now decide which package will be the default for date/time typecasts -if have_pydatetime and use_pydatetime \ - or have_pydatetime and not have_mxdatetime: +if have_pydatetime and (use_pydatetime or not have_mxdatetime): define_macros.append(('PSYCOPG_DEFAULT_PYDATETIME','1')) elif have_mxdatetime: define_macros.append(('PSYCOPG_DEFAULT_MXDATETIME','1'))