Skip to content

Commit

Permalink
Remove conditionals on support for booleans and datetimes, since they
Browse files Browse the repository at this point in the history
are guaranteed to be available in Python 2.4.
  • Loading branch information
jhenstridge committed Dec 26, 2008
1 parent e7b8d65 commit 345a254
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 77 deletions.
58 changes: 21 additions & 37 deletions psycopg/psycopgmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,13 @@ HIDDEN mxDateTimeModule_APIObject *mxDateTimeP = NULL;
#endif

/* some module-level variables, like the datetime module */
#ifdef HAVE_PYDATETIME
#include <datetime.h>
#include "psycopg/adapter_datetime.h"
HIDDEN PyObject *pyDateTimeModuleP = NULL;
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;
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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},
Expand All @@ -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 */
};

Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 1 addition & 8 deletions psycopg/typecast.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,29 +167,24 @@ 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},
{"PYDATE", typecast_DATE_types, typecast_PYDATE_cast},
{"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
Expand Down Expand Up @@ -267,15 +262,13 @@ 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);
t = (typecastObject *)typecast_from_c(&(typecast_pydatetime[i]), dict);
if (t == NULL) return -1;
PyDict_SetItem(dict, t->name, (PyObject *)t);
}
#endif

return 0;
}
Expand Down
44 changes: 12 additions & 32 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import os
import os.path
import sys
import popen2
import subprocess
import ConfigParser
from distutils.core import setup, Extension
from distutils.errors import DistutilsFileError
Expand All @@ -55,23 +55,19 @@
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):
Expand Down Expand Up @@ -311,14 +307,6 @@ def autodetect_pg_config_path_windows(self):
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 = []

Expand All @@ -330,7 +318,7 @@ def autodetect_pg_config_path_windows(self):
'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')
Expand All @@ -343,7 +331,7 @@ def autodetect_pg_config_path_windows(self):
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'))

Expand All @@ -359,16 +347,8 @@ def autodetect_pg_config_path_windows(self):
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'))
Expand Down

0 comments on commit 345a254

Please sign in to comment.