Skip to content

Commit

Permalink
Mkngle python 2 and 3 with ifdef
Browse files Browse the repository at this point in the history
  • Loading branch information
dnadeau4 committed May 7, 2018
1 parent 7703b9a commit 432ddb9
Show file tree
Hide file tree
Showing 4 changed files with 64 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
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
31 changes: 31 additions & 0 deletions tests/test_cdtime_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,37 @@ def testCdtimeComponent(self):
value = 4.
tc.second = value

def testCdtimeCmp(self):
res = cdtime.comptime(2000)<cdtime.comptime(2001)
self.assertTrue(res)

res = cdtime.comptime(2000).__lt__(cdtime.comptime(2001))
self.assertTrue(res)

res =cdtime.reltime(0,'hours since 2000')>cdtime.reltime(1,'hours since 2000')
self.assertFalse(res)

res = cdtime.reltime(0,'hours since 2000')<cdtime.reltime(1,'hours since 2000')
self.assertTrue(res)

res = cdtime.comptime(2000)>cdtime.comptime(2001)
self.assertFalse(res)

res = cdtime.comptime(2000)<cdtime.comptime(2001)
self.assertTrue(res)

res = cdtime.reltime(0,'years since 2000')>cdtime.reltime(1,'years since 2000')
self.assertFalse(res)

res = cdtime.reltime(0,'years since 2000')<cdtime.reltime(1,'years since 2000')
self.assertTrue(res)

res = cdtime.comptime(2000,1,1,0)>cdtime.comptime(2000,1,1,1)
self.assertFalse(res)

res = cdtime.comptime(2000,1,1,0)<cdtime.comptime(2000,1,1,1)
self.assertTrue(res)

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

0 comments on commit 432ddb9

Please sign in to comment.