Python tp_compare functions must return -1, 0 or 1. In addition, the original code assumes that the difference of two pointers fits into an int, which isn't true on all platforms. diff -x config.log -x config.status -ru tmp/talloc-2.1.6/pytalloc.c work/talloc-2.1.6/pytalloc.c --- tmp/talloc-2.1.6/pytalloc.c 2016-03-08 13:37:39.000000000 +0000 +++ work/talloc-2.1.6/pytalloc.c 2016-03-09 13:18:36.382047572 +0000 @@ -17,6 +17,7 @@ along with this program. If not, see . */ +#include #include #include #include @@ -133,14 +134,23 @@ return Py_NotImplemented; } #else +static int signof(ptrdiff_t val) +{ + if (val == 0) + return 0; + else if (val < 0) + return -1; + else + return 1; +} static int pytalloc_default_cmp(PyObject *_obj1, PyObject *_obj2) { pytalloc_Object *obj1 = (pytalloc_Object *)_obj1, *obj2 = (pytalloc_Object *)_obj2; if (obj1->ob_type != obj2->ob_type) - return ((char *)obj1->ob_type - (char *)obj2->ob_type); + return signof((char *)obj1->ob_type - (char *)obj2->ob_type); - return ((char *)pytalloc_get_ptr(obj1) - (char *)pytalloc_get_ptr(obj2)); + return signof((char *)pytalloc_get_ptr(obj1) - (char *)pytalloc_get_ptr(obj2)); } #endif @@ -218,9 +228,9 @@ pytalloc_BaseObject *obj1 = (pytalloc_BaseObject *)_obj1, *obj2 = (pytalloc_BaseObject *)_obj2; if (obj1->ob_type != obj2->ob_type) - return ((char *)obj1->ob_type - (char *)obj2->ob_type); + return signof((char *)obj1->ob_type - (char *)obj2->ob_type); - return ((char *)pytalloc_get_ptr(obj1) - (char *)pytalloc_get_ptr(obj2)); + return signof((char *)pytalloc_get_ptr(obj1) - (char *)pytalloc_get_ptr(obj2)); } #endif