From f03b94d84b93ef154bed3d8064b56fef9e0ead12 Mon Sep 17 00:00:00 2001 From: Federico Di Gregorio Date: Thu, 3 Nov 2005 01:35:17 +0000 Subject: [PATCH] Run-time check for Decimal on Python 2.3. --- ChangeLog | 5 +++++ psycopg/psycopgmodule.c | 5 +++++ setup.cfg | 5 +++++ setup.py | 17 ++++++++++++----- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index b5050ce3..f99137f5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2005-11-03 Federico Di Gregorio + + * Applied patch from Daniele Varazzo to enablÃeDecimal on Python + 2.3 when the module is available (run-time check, nice.) + 2005-10-26 Federico Di Gregorio * setup.cfg: added include_dirs line for SUSE 9.3. diff --git a/psycopg/psycopgmodule.c b/psycopg/psycopgmodule.c index 92b7a9dc..387d3d74 100644 --- a/psycopg/psycopgmodule.c +++ b/psycopg/psycopgmodule.c @@ -363,6 +363,11 @@ psyco_decimal_init(void) if (decimal) { decimalType = PyObject_GetAttrString(decimal, "Decimal"); } + else { + PyErr_Clear(); + decimalType = (PyObject *)&PyFloat_Type; + Py_INCREF(decimalType); + } #endif } diff --git a/setup.cfg b/setup.cfg index 4d5b98c1..0bb03d1f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -6,6 +6,11 @@ define=PSYCOPG_EXTENSIONS,PSYCOPG_DISPLAY_SIZE,HAVE_PQFREEMEM,HAVE_PQPROTOCOL3 # Set to 1 to use Python datatime objects for default date/time representation use_pydatetime=1 +# Set to 1 if you want to enable "Decimal" type on python 2.3. +# If the "decimal" module is found in the PYTHONPATH it will be used, +# else fall back on the float type. +use_decimal=0 + # "include_dirs" is the preferred method for locating postgresql headers, # but some extra checks on sys.platform will still be done in setup.py. # The next line is the default as used on psycopg author Debian laptop: diff --git a/setup.py b/setup.py index ac56bb41..3ded6e42 100644 --- a/setup.py +++ b/setup.py @@ -81,11 +81,13 @@ class psycopg_build_ext(build_ext): ('use-pg-dll', None, "Build against libpq.dll"), ('use-pydatetime', None, - "Use Python datatime objects for date and time representation.") + "Use Python datatime objects for date and time representation."), + ('use-decimal', None, + "Use Decimal type even on Python 2.3 if the module is provided."), ]) boolean_options = build_ext.boolean_options[:] - boolean_options.extend(('use-pg-dll', 'use-pydatetime')) + boolean_options.extend(('use-pg-dll', 'use-pydatetime', 'use-decimal')) # libpq directory in win32 source distribution: compiler dependant. libpqdir = None @@ -246,9 +248,7 @@ 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')) -if sys.version_info[0] >= 2 and sys.version_info[1] >= 4: - define_macros.append(('HAVE_DECIMAL','1')) - + # gather information to build the extension module ext = [] ; data_files = [] @@ -265,6 +265,13 @@ from ConfigParser import ConfigParser parser = ConfigParser() parser.read('setup.cfg') +# Choose if to use Decimal type +use_decimal = int(parser.get('build_ext', 'use_decimal')) +if sys.version_info[0] >= 2 and ( + sys.version_info[1] >= 4 or (sys.version_info[1] == 3 and use_decimal)): + define_macros.append(('HAVE_DECIMAL','1')) + version_flags.append('dec') + # Choose a datetime module have_pydatetime = False have_mxdatetime = False