Remove conditionals on support for booleans and datetimes, since they

are guaranteed to be available in Python 2.4.
This commit is contained in:
James Henstridge 2008-12-26 23:46:35 +09:00
parent e7b8d6505e
commit 345a254ca0
3 changed files with 34 additions and 77 deletions

View File

@ -47,7 +47,6 @@ HIDDEN mxDateTimeModule_APIObject *mxDateTimeP = NULL;
#endif #endif
/* some module-level variables, like the datetime module */ /* some module-level variables, like the datetime module */
#ifdef HAVE_PYDATETIME
#include <datetime.h> #include <datetime.h>
#include "psycopg/adapter_datetime.h" #include "psycopg/adapter_datetime.h"
HIDDEN PyObject *pyDateTimeModuleP = NULL; HIDDEN PyObject *pyDateTimeModuleP = NULL;
@ -55,7 +54,6 @@ HIDDEN PyObject *pyDateTypeP = NULL;
HIDDEN PyObject *pyTimeTypeP = NULL; HIDDEN PyObject *pyTimeTypeP = NULL;
HIDDEN PyObject *pyDateTimeTypeP = NULL; HIDDEN PyObject *pyDateTimeTypeP = NULL;
HIDDEN PyObject *pyDeltaTypeP = NULL; HIDDEN PyObject *pyDeltaTypeP = NULL;
#endif
/* pointers to the psycopg.tz classes */ /* pointers to the psycopg.tz classes */
HIDDEN PyObject *pyPsycopgTzModule = NULL; HIDDEN PyObject *pyPsycopgTzModule = NULL;
@ -276,22 +274,13 @@ psyco_adapters_init(PyObject *mod)
microprotocols_add(&PyFloat_Type, NULL, (PyObject*)&asisType); microprotocols_add(&PyFloat_Type, NULL, (PyObject*)&asisType);
microprotocols_add(&PyInt_Type, NULL, (PyObject*)&asisType); microprotocols_add(&PyInt_Type, NULL, (PyObject*)&asisType);
microprotocols_add(&PyLong_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(&PyString_Type, NULL, (PyObject*)&qstringType);
microprotocols_add(&PyUnicode_Type, NULL, (PyObject*)&qstringType); microprotocols_add(&PyUnicode_Type, NULL, (PyObject*)&qstringType);
microprotocols_add(&PyBuffer_Type, NULL, (PyObject*)&binaryType); microprotocols_add(&PyBuffer_Type, NULL, (PyObject*)&binaryType);
microprotocols_add(&PyList_Type, NULL, (PyObject*)&listType); 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 */ /* as above, we use the callable objects from the psycopg module */
call = PyMapping_GetItemString(mod, "DateFromPy"); call = PyMapping_GetItemString(mod, "DateFromPy");
microprotocols_add((PyTypeObject*)pyDateTypeP, NULL, call); microprotocols_add((PyTypeObject*)pyDateTypeP, NULL, call);
@ -301,10 +290,14 @@ psyco_adapters_init(PyObject *mod)
microprotocols_add((PyTypeObject*)pyDateTimeTypeP, NULL, call); microprotocols_add((PyTypeObject*)pyDateTimeTypeP, NULL, call);
call = PyMapping_GetItemString(mod, "IntervalFromPy"); call = PyMapping_GetItemString(mod, "IntervalFromPy");
microprotocols_add((PyTypeObject*)pyDeltaTypeP, NULL, call); microprotocols_add((PyTypeObject*)pyDeltaTypeP, NULL, call);
#endif
#ifdef HAVE_PYBOOL #ifdef HAVE_MXDATETIME
microprotocols_add(&PyBool_Type, NULL, (PyObject*)&pbooleanType); /* 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 #endif
#ifdef HAVE_DECIMAL #ifdef HAVE_DECIMAL
@ -659,6 +652,15 @@ static PyMethodDef psycopgMethods[] = {
{"List", (PyCFunction)psyco_List, {"List", (PyCFunction)psyco_List,
METH_VARARGS, psyco_List_doc}, 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 #ifdef HAVE_MXDATETIME
{"DateFromMx", (PyCFunction)psyco_DateFromMx, {"DateFromMx", (PyCFunction)psyco_DateFromMx,
METH_VARARGS, psyco_DateFromMx_doc}, METH_VARARGS, psyco_DateFromMx_doc},
@ -670,17 +672,6 @@ static PyMethodDef psycopgMethods[] = {
METH_VARARGS, psyco_IntervalFromMx_doc}, METH_VARARGS, psyco_IntervalFromMx_doc},
#endif #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 */ {NULL, NULL, 0, NULL} /* Sentinel */
}; };
@ -694,7 +685,7 @@ init_psycopg(void)
#ifdef PSYCOPG_DEBUG #ifdef PSYCOPG_DEBUG
if (getenv("PSYCOPG_DEBUG")) if (getenv("PSYCOPG_DEBUG"))
psycopg_debug_enabled = 1; psycopg_debug_enabled = 1;
#endif #endif
Dprintf("initpsycopg: initializing psycopg %s", PSYCOPG_VERSION); Dprintf("initpsycopg: initializing psycopg %s", PSYCOPG_VERSION);
@ -706,6 +697,7 @@ init_psycopg(void)
qstringType.ob_type = &PyType_Type; qstringType.ob_type = &PyType_Type;
binaryType.ob_type = &PyType_Type; binaryType.ob_type = &PyType_Type;
isqlquoteType.ob_type = &PyType_Type; isqlquoteType.ob_type = &PyType_Type;
pbooleanType.ob_type = &PyType_Type;
asisType.ob_type = &PyType_Type; asisType.ob_type = &PyType_Type;
listType.ob_type = &PyType_Type; listType.ob_type = &PyType_Type;
chunkType.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(&qstringType) == -1) return;
if (PyType_Ready(&binaryType) == -1) return; if (PyType_Ready(&binaryType) == -1) return;
if (PyType_Ready(&isqlquoteType) == -1) return; if (PyType_Ready(&isqlquoteType) == -1) return;
if (PyType_Ready(&pbooleanType) == -1) return;
if (PyType_Ready(&asisType) == -1) return; if (PyType_Ready(&asisType) == -1) return;
if (PyType_Ready(&listType) == -1) return; if (PyType_Ready(&listType) == -1) return;
if (PyType_Ready(&chunkType) == -1) return; if (PyType_Ready(&chunkType) == -1) return;
@ -725,11 +718,6 @@ init_psycopg(void)
if (PyType_Ready(&lobjectType) == -1) return; if (PyType_Ready(&lobjectType) == -1) return;
#endif #endif
#ifdef HAVE_PYBOOL
pbooleanType.ob_type = &PyType_Type;
if (PyType_Ready(&pbooleanType) == -1) return;
#endif
/* import mx.DateTime module, if necessary */ /* import mx.DateTime module, if necessary */
#ifdef HAVE_MXDATETIME #ifdef HAVE_MXDATETIME
mxdatetimeType.ob_type = &PyType_Type; mxdatetimeType.ob_type = &PyType_Type;
@ -743,7 +731,6 @@ init_psycopg(void)
#endif #endif
/* import python builtin datetime module, if available */ /* import python builtin datetime module, if available */
#ifdef HAVE_PYDATETIME
pyDateTimeModuleP = PyImport_ImportModule("datetime"); pyDateTimeModuleP = PyImport_ImportModule("datetime");
if (pyDateTimeModuleP == NULL) { if (pyDateTimeModuleP == NULL) {
Dprintf("initpsycopg: can't import datetime module"); Dprintf("initpsycopg: can't import datetime module");
@ -759,7 +746,6 @@ init_psycopg(void)
pyTimeTypeP = PyObject_GetAttrString(pyDateTimeModuleP, "time"); pyTimeTypeP = PyObject_GetAttrString(pyDateTimeModuleP, "time");
pyDateTimeTypeP = PyObject_GetAttrString(pyDateTimeModuleP, "datetime"); pyDateTimeTypeP = PyObject_GetAttrString(pyDateTimeModuleP, "datetime");
pyDeltaTypeP = PyObject_GetAttrString(pyDateTimeModuleP, "timedelta"); pyDeltaTypeP = PyObject_GetAttrString(pyDateTimeModuleP, "timedelta");
#endif
/* import psycopg2.tz anyway (TODO: replace with C-level module?) */ /* import psycopg2.tz anyway (TODO: replace with C-level module?) */
pyPsycopgTzModule = PyImport_ImportModule("psycopg2.tz"); pyPsycopgTzModule = PyImport_ImportModule("psycopg2.tz");
@ -828,14 +814,12 @@ init_psycopg(void)
qstringType.tp_alloc = PyType_GenericAlloc; qstringType.tp_alloc = PyType_GenericAlloc;
listType.tp_alloc = PyType_GenericAlloc; listType.tp_alloc = PyType_GenericAlloc;
chunkType.tp_alloc = PyType_GenericAlloc; chunkType.tp_alloc = PyType_GenericAlloc;
pydatetimeType.tp_alloc = PyType_GenericAlloc;
#ifdef PSYCOPG_EXTENSIONS #ifdef PSYCOPG_EXTENSIONS
lobjectType.tp_alloc = PyType_GenericAlloc; lobjectType.tp_alloc = PyType_GenericAlloc;
#endif #endif
#ifdef HAVE_PYDATETIME
pydatetimeType.tp_alloc = PyType_GenericAlloc;
#endif
#ifdef HAVE_MXDATETIME #ifdef HAVE_MXDATETIME
mxdatetimeType.tp_alloc = PyType_GenericAlloc; mxdatetimeType.tp_alloc = PyType_GenericAlloc;

View File

@ -167,21 +167,17 @@ typecast_parse_time(const char* s, const char** t, Py_ssize_t* len,
/** include casting objects **/ /** include casting objects **/
#include "psycopg/typecast_basic.c" #include "psycopg/typecast_basic.c"
#include "psycopg/typecast_binary.c" #include "psycopg/typecast_binary.c"
#include "psycopg/typecast_datetime.c"
#ifdef HAVE_MXDATETIME #ifdef HAVE_MXDATETIME
#include "psycopg/typecast_mxdatetime.c" #include "psycopg/typecast_mxdatetime.c"
#endif #endif
#ifdef HAVE_PYDATETIME
#include "psycopg/typecast_datetime.c"
#endif
#include "psycopg/typecast_array.c" #include "psycopg/typecast_array.c"
#include "psycopg/typecast_builtins.c" #include "psycopg/typecast_builtins.c"
/* a list of initializers, used to make the typecasters accessible anyway */ /* a list of initializers, used to make the typecasters accessible anyway */
#ifdef HAVE_PYDATETIME
static typecastObject_initlist typecast_pydatetime[] = { static typecastObject_initlist typecast_pydatetime[] = {
{"PYDATETIME", typecast_DATETIME_types, typecast_PYDATETIME_cast}, {"PYDATETIME", typecast_DATETIME_types, typecast_PYDATETIME_cast},
{"PYTIME", typecast_TIME_types, typecast_PYTIME_cast}, {"PYTIME", typecast_TIME_types, typecast_PYTIME_cast},
@ -189,7 +185,6 @@ static typecastObject_initlist typecast_pydatetime[] = {
{"PYINTERVAL", typecast_INTERVAL_types, typecast_PYINTERVAL_cast}, {"PYINTERVAL", typecast_INTERVAL_types, typecast_PYINTERVAL_cast},
{NULL, NULL, NULL} {NULL, NULL, NULL}
}; };
#endif
/* a list of initializers, used to make the typecasters accessible anyway */ /* a list of initializers, used to make the typecasters accessible anyway */
#ifdef HAVE_MXDATETIME #ifdef HAVE_MXDATETIME
@ -267,7 +262,6 @@ typecast_init(PyObject *dict)
PyDict_SetItem(dict, t->name, (PyObject *)t); PyDict_SetItem(dict, t->name, (PyObject *)t);
} }
#endif #endif
#ifdef HAVE_PYDATETIME
for (i = 0; typecast_pydatetime[i].name != NULL; i++) { for (i = 0; typecast_pydatetime[i].name != NULL; i++) {
typecastObject *t; typecastObject *t;
Dprintf("typecast_init: initializing %s", typecast_pydatetime[i].name); Dprintf("typecast_init: initializing %s", typecast_pydatetime[i].name);
@ -275,7 +269,6 @@ typecast_init(PyObject *dict)
if (t == NULL) return -1; if (t == NULL) return -1;
PyDict_SetItem(dict, t->name, (PyObject *)t); PyDict_SetItem(dict, t->name, (PyObject *)t);
} }
#endif
return 0; return 0;
} }

View File

@ -46,7 +46,7 @@ Operating System :: Unix
import os import os
import os.path import os.path
import sys import sys
import popen2 import subprocess
import ConfigParser import ConfigParser
from distutils.core import setup, Extension from distutils.core import setup, Extension
from distutils.errors import DistutilsFileError from distutils.errors import DistutilsFileError
@ -55,23 +55,19 @@ from distutils.sysconfig import get_python_inc
from distutils.ccompiler import get_default_compiler from distutils.ccompiler import get_default_compiler
PSYCOPG_VERSION = '2.0.8' PSYCOPG_VERSION = '2.0.8'
version_flags = [] version_flags = ['dt']
PLATFORM_IS_WINDOWS = sys.platform.lower().startswith('win') 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"): def get_pg_config(kind, pg_config="pg_config"):
if ' ' in pg_config: p = subprocess.Popen([pg_config, "--" + kind],
pg_config = '"'+pg_config+'"' stdin=subprocess.PIPE,
p = popen2.popen3(pg_config + " --" + kind) stdout=subprocess.PIPE,
r = p[0].readline().strip() stderr=subprocess.PIPE)
p.stdin.close()
r = p.stdout.readline().strip()
if not r: if not r:
raise Warning(p[2].readline()) raise Warning(p.stderr.readline())
return r return r
class psycopg_build_ext(build_ext): class psycopg_build_ext(build_ext):
@ -311,14 +307,6 @@ class psycopg_build_ext(build_ext):
define_macros = [] define_macros = []
include_dirs = [] 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 # gather information to build the extension module
ext = [] ; data_files = [] ext = [] ; data_files = []
@ -330,7 +318,7 @@ sources = [
'connection_type.c', 'connection_int.c', 'cursor_type.c', 'cursor_int.c', 'connection_type.c', 'connection_int.c', 'cursor_type.c', 'cursor_int.c',
'lobject_type.c', 'lobject_int.c', 'lobject_type.c', 'lobject_int.c',
'adapter_qstring.c', 'adapter_pboolean.c', 'adapter_binary.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 = ConfigParser.ConfigParser()
parser.read('setup.cfg') parser.read('setup.cfg')
@ -343,7 +331,7 @@ if sys.version_info[0] >= 2 and (
version_flags.append('dec') version_flags.append('dec')
# Choose a datetime module # Choose a datetime module
have_pydatetime = False have_pydatetime = True
have_mxdatetime = False have_mxdatetime = False
use_pydatetime = int(parser.get('build_ext', 'use_pydatetime')) use_pydatetime = int(parser.get('build_ext', 'use_pydatetime'))
@ -359,16 +347,8 @@ if os.path.exists(mxincludedir):
have_mxdatetime = True have_mxdatetime = True
version_flags.append('mx') 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 # now decide which package will be the default for date/time typecasts
if have_pydatetime and use_pydatetime \ if have_pydatetime and (use_pydatetime or not have_mxdatetime):
or have_pydatetime and not have_mxdatetime:
define_macros.append(('PSYCOPG_DEFAULT_PYDATETIME','1')) define_macros.append(('PSYCOPG_DEFAULT_PYDATETIME','1'))
elif have_mxdatetime: elif have_mxdatetime:
define_macros.append(('PSYCOPG_DEFAULT_MXDATETIME','1')) define_macros.append(('PSYCOPG_DEFAULT_MXDATETIME','1'))