diff --git a/dtool/src/interrogate/interfaceMakerPythonNative.cxx b/dtool/src/interrogate/interfaceMakerPythonNative.cxx index bf0ff5d333..2856c95fba 100644 --- a/dtool/src/interrogate/interfaceMakerPythonNative.cxx +++ b/dtool/src/interrogate/interfaceMakerPythonNative.cxx @@ -2570,6 +2570,8 @@ write_module_class(ostream &out, Object *obj) { out << " return nullptr;\n"; out << " }\n\n"; + bool have_eq = false; + bool have_ne = false; for (Function *func : obj->_methods) { std::set remaps; if (!func) { @@ -2590,8 +2592,10 @@ write_module_class(ostream &out, Object *obj) { op_type = "Py_LE"; } else if (fname == "operator ==") { op_type = "Py_EQ"; + have_eq = true; } else if (fname == "operator !=") { op_type = "Py_NE"; + have_ne = true; } else if (fname == "operator >") { op_type = "Py_GT"; } else if (fname == "operator >=") { @@ -2616,6 +2620,43 @@ write_module_class(ostream &out, Object *obj) { } if (has_local_richcompare) { + if (have_eq && !have_ne) { + // Generate a not-equal function from the equal function. + for (Function *func : obj->_methods) { + std::set remaps; + if (!func) { + continue; + } + const string &fname = func->_ifunc.get_name(); + if (fname != "operator ==") { + continue; + } + for (FunctionRemap *remap : func->_remaps) { + if (is_remap_legal(remap) && remap->_has_this && (remap->_args_type == AT_single_arg)) { + remaps.insert(remap); + } + } + out << " case Py_NE: // from Py_EQ\n"; + out << " {\n"; + + string expected_params; + write_function_forset(out, remaps, 1, 1, expected_params, 6, true, false, + AT_single_arg, RF_pyobject | RF_invert_bool | RF_err_null, false); + + out << " break;\n"; + out << " }\n"; + } + } + else if (!have_eq && !slots.count("tp_compare")) { + // Generate an equals function. + out << " case Py_EQ:\n"; + out << " return PyBool_FromLong(DtoolInstance_Check(arg) && DtoolInstance_VOID_PTR(self) == DtoolInstance_VOID_PTR(arg));\n"; + if (!have_ne) { + out << " case Py_NE:\n"; + out << " return PyBool_FromLong(!DtoolInstance_Check(arg) || DtoolInstance_VOID_PTR(self) != DtoolInstance_VOID_PTR(arg));\n"; + } + } + // End of switch block out << " }\n\n"; out << " if (_PyErr_OCCURRED()) {\n"; @@ -6048,8 +6089,13 @@ write_function_instance(ostream &out, FunctionRemap *remap, return_flags &= ~RF_pyobject; } else if (return_null && TypeManager::is_bool(remap->_return_type->get_new_type())) { - indent(out, indent_level) - << "return Dtool_Return_Bool(" << return_expr << ");\n"; + if (return_flags & RF_invert_bool) { + indent(out, indent_level) + << "return Dtool_Return_Bool(!(" << return_expr << "));\n"; + } else { + indent(out, indent_level) + << "return Dtool_Return_Bool(" << return_expr << ");\n"; + } return_flags &= ~RF_pyobject; } else if (return_null && TypeManager::is_pointer_to_PyObject(remap->_return_type->get_new_type())) { @@ -6415,7 +6461,12 @@ pack_return_value(ostream &out, int indent_level, FunctionRemap *remap, indent(out, indent_level) << "return PyString_FromStringAndSize((char *)return_value.data(), (Py_ssize_t)return_value.size());\n"; out << "#endif\n"; - } else { + } + else if (return_flags & RF_invert_bool) { + indent(out, indent_level) + << "return Dtool_WrapValue(!(" << return_expr << "));\n"; + } + else { indent(out, indent_level) << "return Dtool_WrapValue(" << return_expr << ");\n"; } diff --git a/dtool/src/interrogate/interfaceMakerPythonNative.h b/dtool/src/interrogate/interfaceMakerPythonNative.h index c667ab22fa..b387cf0001 100644 --- a/dtool/src/interrogate/interfaceMakerPythonNative.h +++ b/dtool/src/interrogate/interfaceMakerPythonNative.h @@ -115,6 +115,9 @@ private: // This raises a KeyError on falsey (or -1) return value. RF_raise_keyerror = 0x4000, + + // Invert boolean return value. + RF_invert_bool = 0x8000, }; class SlottedFunctionDef {