From 432ddb9faab79d6a5a6863b9758c09218152090e Mon Sep 17 00:00:00 2001 From: Denis Nadeau Date: Mon, 7 May 2018 16:26:45 -0700 Subject: [PATCH] Mkngle python 2 and 3 with ifdef --- Include/py3c/compat.h | 1 - Src/cdtimemodule.c | 53 ++++++++++++++++++++------------- setup.py | 2 +- tests/test_cdtime_components.py | 31 +++++++++++++++++++ 4 files changed, 64 insertions(+), 23 deletions(-) diff --git a/Include/py3c/compat.h b/Include/py3c/compat.h index 72ae2da..a32451d 100644 --- a/Include/py3c/compat.h +++ b/Include/py3c/compat.h @@ -9,7 +9,6 @@ #if PY_MAJOR_VERSION >= 3 /***** Python 3 *****/ - #define IS_PY3 1 /* Strings */ diff --git a/Src/cdtimemodule.c b/Src/cdtimemodule.c index 36f3c97..f8f88ae 100644 --- a/Src/cdtimemodule.c +++ b/Src/cdtimemodule.c @@ -198,7 +198,7 @@ reltime_torel(PyCdReltimeObject *self, char *outunits, cdCalenType calendar) { return (PyObject *) newreltimeobject(outTime, outunits); } -static PyObject * +static int reltime_cmp2(PyCdReltimeObject *self, PyObject *other, cdCalenType calendar) { int saveCalendar; int comparison; @@ -207,9 +207,10 @@ reltime_cmp2(PyCdReltimeObject *self, PyObject *other, cdCalenType calendar) { /* The compare is done by the module methods, which get the value of the calendar from "DefaultCalendar", so temporarily override this. */ - - saveCalendar = GET_CALENDAR; - SET_CALENDAR(calendar); +// saveCalendar = GET_CALENDAR; +// SET_CALENDAR(calendar); + //saveCalendar = calendar; + calendar = GET_CALENDAR; if (is_comptimeobject(other)) { /* Coerce comptime to reltime */ @@ -222,7 +223,6 @@ reltime_cmp2(PyCdReltimeObject *self, PyObject *other, cdCalenType calendar) { } //#define VALCMP(a,b) ((a)<(b)?-1:(b)<(a)?1:0) - if (PyCdReltime_Compare(self, (PyObject *) otherReltime, Py_EQ) == Py_True) { comparison = 0; Py_XDECREF(otherReltime); @@ -233,8 +233,9 @@ reltime_cmp2(PyCdReltimeObject *self, PyObject *other, cdCalenType calendar) { } else { comparison = 1; } - SET_CALENDAR(saveCalendar); - return Py_BuildValue("i", comparison); + // SET_CALENDAR(saveCalendar); + // return Py_BuildValue("i", comparison); + return(comparison); } #ifdef PYTHON2 @@ -378,7 +379,7 @@ comptime_tocomp(PyCdComptimeObject *self, cdCalenType calendar) { return (PyObject *) newcomptimeobject(self->year, self->month, self->day, self->hour, self->minute, self->second); } -static PyObject * +static int comptime_cmp2(PyCdComptimeObject *self, PyObject *other, cdCalenType calendar) { int saveCalendar; int comparison; @@ -386,9 +387,10 @@ comptime_cmp2(PyCdComptimeObject *self, PyObject *other, cdCalenType calendar) { /* The comparisons are done by the module functions, which get the calendar from the value of "DefaultCalendar", so set this temporarily */ - saveCalendar = GET_CALENDAR; - SET_CALENDAR(calendar); - +// saveCalendar = GET_CALENDAR; +// SET_CALENDAR(calendar); + //saveCalendar = calendar; + calendar = GET_CALENDAR; if (PyCdComptime_Compare(self, other, Py_EQ) == Py_True) { comparison = 0; } else if (PyCdComptime_Compare(self, other, Py_LT) == Py_True) { @@ -397,8 +399,9 @@ comptime_cmp2(PyCdComptimeObject *self, PyObject *other, cdCalenType calendar) { comparison = 1; } - SET_CALENDAR(saveCalendar); - return Py_BuildValue("i", comparison); + //SET_CALENDAR(saveCalendar); + // return Py_BuildValue("i", comparison); + return(comparison); } #ifdef Python2 @@ -508,11 +511,10 @@ PyCdReltime_Cmp(PyCdReltimeObject *self, PyObject *args) { PyObject *other; calendar = GET_CALENDAR; - if (!PyArg_ParseTuple(args, "O|i", &other, &calendar)) return NULL; + return Py_BuildValue("i", reltime_cmp2(self, other, calendar)); - return reltime_cmp2(self, other, calendar); } static struct PyMethodDef reltime_instance_methods[] = { /* instance methods */ @@ -600,8 +602,7 @@ PyCdComptime_Cmp(PyCdComptimeObject *self, PyObject *args) { if (!PyArg_ParseTuple(args, "O|i", &other, &calendar)) return NULL; - return comptime_cmp2(self, other, calendar); - + return Py_BuildValue("i",comptime_cmp2(self, other, calendar)); } static struct PyMethodDef comptime_instance_methods[] = { /* instance methods */ @@ -992,7 +993,11 @@ sizeof(PyCdReltimeObject), /* tp_basicsize */ (printfunc) PyCdReltime_Print, /* tp_print "print x" */ 0, /* tp_getattr "x.attr" */ 0, /* tp_setattr "x.attr=v" */ -0, /* tp_compare "x > y" */ +#if IS_PY3 == 1 +0, /* tp_compare */ +#else +(cmpfunc) reltime_cmp2, /* tp_compare "x > y" */ +#endif (reprfunc) PyCdReltime_Repr, /* tp_repr `x`, print x */ /* type categories */ @@ -1030,7 +1035,11 @@ sizeof(PyCdComptimeObject), /* tp_basicsize */ (printfunc) PyCdComptime_Print, /* tp_print "print x" */ 0, /* tp_getattr "x.attr" */ 0, /* tp_setattr "x.attr=v" */ -0, /* tp_compare "x > y" */ +#if IS_PY3 == 1 +0, /* tp_compare */ +#else +(cmpfunc) comptime_cmp2, /* tp_compare "x > y" */ +#endif (reprfunc) PyCdComptime_Repr, /* tp_repr `x`, print x */ /* type categories */ @@ -1163,9 +1172,11 @@ PyCdtime_Compare(PyObject *self, PyObject *args) { /* on "x = cdtime.cmp()" */ return NULL; if (is_reltimeobject(t1)) { - return reltime_cmp2((PyCdReltimeObject *) t1, t2, calendar); + return Py_BuildValue("i", reltime_cmp2((PyCdReltimeObject *) t1, + t2, calendar)); } else if (is_comptimeobject(t1)) { - return comptime_cmp2((PyCdComptimeObject *) t1, t2, calendar); + return Py_BuildValue("i", comptime_cmp2((PyCdComptimeObject *) t1, + t2, calendar)); } else onError("Argument is not a time"); } diff --git a/setup.py b/setup.py index 939d3b3..bf35685 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ include_dirs = ['Include', 'Include/py3c'] + cdat_info.cdunif_include_directories, ext_modules = [Extension('cdtime', ['Src/cdtimemodule.c'], - extra_compile_args = [ "-g","-O0"], + extra_compile_args = [ "-fPIC", "-g","-O0"], library_dirs = cdat_info.cdunif_library_directories, libraries = cdat_info.cdunif_libraries) ] diff --git a/tests/test_cdtime_components.py b/tests/test_cdtime_components.py index 3e8872a..bd8d92b 100644 --- a/tests/test_cdtime_components.py +++ b/tests/test_cdtime_components.py @@ -21,6 +21,37 @@ def testCdtimeComponent(self): value = 4. tc.second = value + def testCdtimeCmp(self): + res = cdtime.comptime(2000)cdtime.reltime(1,'hours since 2000') + self.assertFalse(res) + + res = cdtime.reltime(0,'hours since 2000')cdtime.comptime(2001) + self.assertFalse(res) + + res = cdtime.comptime(2000)cdtime.reltime(1,'years since 2000') + self.assertFalse(res) + + res = cdtime.reltime(0,'years since 2000')cdtime.comptime(2000,1,1,1) + self.assertFalse(res) + + res = cdtime.comptime(2000,1,1,0)