diff --git a/dtool/src/interrogate/interfaceMakerPythonNative.cxx b/dtool/src/interrogate/interfaceMakerPythonNative.cxx index 7ff500199e..a4d957d8a2 100755 --- a/dtool/src/interrogate/interfaceMakerPythonNative.cxx +++ b/dtool/src/interrogate/interfaceMakerPythonNative.cxx @@ -1414,11 +1414,14 @@ void InterfaceMakerPythonNative::write_module_class(ostream &out, Object *obj) } } + // compare and hash work together in PY inherit behavior hmm grrr // __hash__ if(has_local_hash == true) { out << " // __hash__\n"; out << " Dtool_" << ClassName <<".As_PyTypeObject().tp_hash = &DTool_HashKey_"<ob_type, v2->ob_type)) @@ -753,5 +753,58 @@ inline int DTOOL_PyObject_Compare(PyObject *v1, PyObject *v2) return 0; } + +inline int DTOOL_PyObject_Compare(PyObject *v1, PyObject *v2) +{ + // First try compare to function.. + PyObject * func = PyObject_GetAttrString(v1, "compareTo"); + if (func == NULL) + { + PyErr_Clear(); + } + else + { + PyObject * res = NULL; + PyObject * args = Py_BuildValue("(O)", v2); + if (args != NULL) + { + res = PyObject_Call(func, args, NULL); + Py_DECREF(args); + } + Py_DECREF(func); + PyErr_Clear(); // just in case the function threw an error + // only use if the cuntion return an INT... hmm + if(res != NULL && PyInt_Check(res)) + { + int answer = PyInt_AsLong(res); + Py_DECREF(res); + return answer; + } + if(res != NULL) + Py_DECREF(res); + + }; + + // try this compare + void * v1_this = DTOOL_Call_GetPointerThis(v1); + void * v2_this = DTOOL_Call_GetPointerThis(v2); + if(v1_this != NULL && v2_this != NULL) // both are our types... + { + if(v1_this < v2_this) + return -1; + + if(v1_this > v2_this) + return 1; + return 0; + } + + // ok self compare... + if(v1 < v2) + return -1; + if(v1 > v2) + return 1; + return 0; +} + #endif // PY_PANDA_H_