diff --git a/src/idl_gen_python.cpp b/src/idl_gen_python.cpp index 38a5bc100..76d3dfe9b 100644 --- a/src/idl_gen_python.cpp +++ b/src/idl_gen_python.cpp @@ -1141,6 +1141,23 @@ class PythonGenerator : public BaseGenerator { code += "\n"; } + void GenCompareOperator(const StructDef &struct_def, + std::string *code_ptr) const { + auto &code = *code_ptr; + code += GenIndents(1) + "def __eq__(self, other):"; + code += GenIndents(2) + "return type(self) == type(other)"; + for (auto it = struct_def.fields.vec.begin(); + it != struct_def.fields.vec.end(); ++it) { + auto &field = **it; + if (field.deprecated) continue; + + // Wrties the comparison statement for this field. + const auto field_field = namer_.Field(field); + code += " and \\" + GenIndents(3) + "self." + field_field + " == " + "other." + field_field; + } + code += "\n"; + } + void GenUnPackForStruct(const StructDef &struct_def, const FieldDef &field, std::string *code_ptr) const { auto &code = *code_ptr; @@ -1623,6 +1640,10 @@ class PythonGenerator : public BaseGenerator { InitializeFromObjForObject(struct_def, &code); + if (parser_.opts.gen_compare) { + GenCompareOperator(struct_def, &code); + } + GenUnPack(struct_def, &code); if (struct_def.fixed) { diff --git a/tests/MyGame/MonsterExtra.py b/tests/MyGame/MonsterExtra.py index b30ee6e78..90916523c 100644 --- a/tests/MyGame/MonsterExtra.py +++ b/tests/MyGame/MonsterExtra.py @@ -217,6 +217,19 @@ class MonsterExtraT(object): x._UnPack(monsterExtra) return x + def __eq__(self, other): + return type(self) == type(other) and \ + self.d0 == other.d0 and \ + self.d1 == other.d1 and \ + self.d2 == other.d2 and \ + self.d3 == other.d3 and \ + self.f0 == other.f0 and \ + self.f1 == other.f1 and \ + self.f2 == other.f2 and \ + self.f3 == other.f3 and \ + self.dvec == other.dvec and \ + self.fvec == other.fvec + # MonsterExtraT def _UnPack(self, monsterExtra): if monsterExtra is None: