Skip to content

Commit

Permalink
Merge pull request #19 from CDAT/issue_12_cmp_failing
Browse files Browse the repository at this point in the history
Mingle python 2 and 3 with ifdef
  • Loading branch information
dnadeau4 authored May 28, 2018
2 parents 5f31904 + 8475339 commit 5466869
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 23 deletions.
1 change: 0 additions & 1 deletion Include/py3c/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#if PY_MAJOR_VERSION >= 3

/***** Python 3 *****/

#define IS_PY3 1

/* Strings */
Expand Down
53 changes: 32 additions & 21 deletions Src/cdtimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -378,17 +379,18 @@ 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;

/* 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) {
Expand All @@ -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
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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");
}
Expand Down
36 changes: 36 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
general:
#branches:
# ignore:
# We only want to build pull requests for testing. If something is merged,
# then we are prepping for release an there is no need to build it again.
# - master
artifacts:
- tests_html
- tests_png

checkout:
post:
- ./ci-support/checkout_merge_commit.sh

#machine:
machine:
# xcode:
# version: 7.2
pre:
- sudo -H pip install --upgrade virtualenv
- ls
- pwd
- bash cdtime/ci-support/circleci_mac_machine_pre.sh
#services:
# - docker

dependencies:
override:
- bash ./ci-support/circleci_mac_dep.sh
# - docker pull cdat/conda:conda-forge-cdms2


test:
override:
- bash ./ci-support/circleci_mac.sh
# - docker run -it -v `pwd`:/git_repo -a STDOUT -a STDERR -P cdat/conda:conda-forge-cdms2 /git_repo/ci-support/circle.sh
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]
Expand Down
6 changes: 6 additions & 0 deletions tests/test_cdtime_comparisons.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ def testCmpRel(self):
self.assertEqual(self.smallRel.cmp(self.smallRel),0)
self.assertEqual(self.bigRel.cmp(self.smallComp),1)
self.assertEqual(self.bigRel.cmp(self.smallRel),1)
def testConversion(self):
bigTransComp = self.bigRel.tocomp()
self.assertTrue(bigTransComp == self.bigComp)
bigTransRel = self.bigComp.torel("days since 2020")
self.assertTrue(bigTransRel == self.bigRel)

if __name__ == '__main__':
unittest.main()

99 changes: 99 additions & 0 deletions tests/test_cdtime_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,45 @@
import cdtime

class CDTIMETest(unittest.TestCase):
def less(self, a, b):
print("Testing",a,b,"for less")
self.assertTrue(a<b)
self.assertTrue(a.__lt__(b))
self.assertFalse(b<a)
self.assertFalse(b.__lt__(a))
self.assertFalse(a<a)
self.assertFalse(a.__lt__(a))
def greater(self, a, b):
print("Testing",a,b,"for greater")
self.assertTrue(a>b)
self.assertTrue(a.__gt__(b))
self.assertFalse(b>a)
self.assertFalse(b.__gt__(a))
self.assertFalse(a>a)
self.assertFalse(a.__gt__(a))
def less_equal(self, a, b):
print("Testing",a,b,"for less equal")
self.assertTrue(a<=b)
self.assertTrue(a.__le__(b))
self.assertFalse(b<=a)
self.assertFalse(b.__le__(a))
self.assertTrue(a<=a)
self.assertTrue(a.__le__(a))
def greater_equal(self, a, b):
print("Testing",a,b,"for greater equal")
self.assertTrue(a>=b)
self.assertTrue(a.__ge__(b))
self.assertFalse(b>=a)
self.assertFalse(b.__ge__(a))
self.assertTrue(a>=a)
self.assertTrue(a.__ge__(a))

def equal(self, a, b):
print("Testing",a,b,"for equal")
self.assertTrue(a==b)
self.assertTrue(a.__eq__(b))
self.assertFalse(a!=b)
self.assertFalse(a.__ne__(b))

def testWrongUnits(self):
try:
Expand All @@ -21,6 +60,66 @@ def testCdtimeComponent(self):
value = 4.
tc.second = value

def testCdtimeCmp(self):

# Component Time
a = cdtime.comptime(2000)
b = cdtime.comptime(2001)
self.less(a,b)
self.less_equal(a,b)
self.greater(b,a)
self.greater_equal(b,a)
self.equal(a,a)

# Reltime
for units in ["years","months","days","minutes","seconds"]:
a = cdtime.reltime(1,"{} since 2000".format(units))
b = cdtime.reltime(2,"{} since 2000".format(units))
self.less(a,b)
self.less_equal(a,b)
self.greater(b,a)
self.greater_equal(b,a)
self.equal(a,a)

# Comp and rel
a = cdtime.comptime(2000)
for units in ["years","months","days","minutes","seconds"]:
b = cdtime.reltime(2,"{} since 2000".format(units))
self.less(a,b)
self.less_equal(a,b)
self.greater(b,a)
self.greater_equal(b,a)
self.equal(a,a)

# Rel and comp
b = cdtime.comptime(3000)
for units in ["years","months","days","minutes","seconds"]:
a = cdtime.reltime(2,"{} since 2000".format(units))
self.less(a,b)
self.less_equal(a,b)
self.greater(b,a)
self.greater_equal(b,a)
self.equal(a,a)
# Comp and rel

res = cdtime.comptime(2000)<cdtime.reltime(2,"days since 2000")
self.assertTrue(res)

res = cdtime.comptime(2000)>cdtime.reltime(2,"days since 1990")
self.assertTrue(res)

res = cdtime.comptime(2000)==cdtime.reltime(0,"days since 2000")
self.assertTrue(res)

res = cdtime.reltime(2,"days since 2000")>cdtime.comptime(2000)
self.assertTrue(res)

res = cdtime.reltime(2,"days since 2000")<cdtime.comptime(2000,1,4)
self.assertTrue(res)

res = cdtime.reltime(2,"days since 2000") == cdtime.comptime(2000,1,3)
self.assertTrue(res)

if __name__ == '__main__':
unittest.main()

0 comments on commit 5466869

Please sign in to comment.