From a9640bd9e15568632a5b3e256a67980a4a7eaba6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tin=20Tvrtkovi=C4=87?= Date: Mon, 14 May 2018 22:30:10 +0200 Subject: [PATCH] [BREAKING CHANGE] Python: handle bool table fields properly. (#4736) * Python: handle bool table fields properly. * Small refactor. * Use snake_case instead of camelCase. Use auto. --- src/idl_gen_python.cpp | 11 ++++++++--- tests/MyGame/Example/Monster.py | 4 ++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/idl_gen_python.cpp b/src/idl_gen_python.cpp index 17db56585..ba7dd5624 100644 --- a/src/idl_gen_python.cpp +++ b/src/idl_gen_python.cpp @@ -141,9 +141,14 @@ static void GetScalarFieldOfTable(const StructDef &struct_def, code += MakeCamel(field.name); code += "(self):"; code += OffsetPrefix(field); - code += Indent + Indent + Indent + "return " + getter; - code += "o + self._tab.Pos)\n"; - code += Indent + Indent + "return " + field.value.constant + "\n\n"; + getter += "o + self._tab.Pos)"; + auto is_bool = field.value.type.base_type == BASE_TYPE_BOOL; + if (is_bool) { + getter = "bool(" + getter + ")"; + } + code += Indent + Indent + Indent + "return " + getter + "\n"; + auto defaultValue = (is_bool ? "False" : field.value.constant); + code += Indent + Indent + "return " + defaultValue + "\n\n"; } // Get a struct by initializing an existing struct. diff --git a/tests/MyGame/Example/Monster.py b/tests/MyGame/Example/Monster.py index 1e6a9658a..493836e77 100644 --- a/tests/MyGame/Example/Monster.py +++ b/tests/MyGame/Example/Monster.py @@ -201,8 +201,8 @@ class Monster(object): def Testbool(self): o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(34)) if o != 0: - return self._tab.Get(flatbuffers.number_types.BoolFlags, o + self._tab.Pos) - return 0 + return bool(self._tab.Get(flatbuffers.number_types.BoolFlags, o + self._tab.Pos)) + return False # Monster def Testhashs32Fnv1(self):