diff options
author | Vladimir Glavnyy <31897320+vglavnyy@users.noreply.github.com> | 2019-01-15 00:23:42 +0700 |
---|---|---|
committer | Wouter van Oortmerssen <aardappel@gmail.com> | 2019-01-14 09:23:42 -0800 |
commit | 4fa4d367069748c318bfff041d71184dc1199270 (patch) | |
tree | 63c23e003bbd678e13d07f949676c96ec7ef25b0 /src/idl_gen_python.cpp | |
parent | 46208b1e918cfa7e92abc109264e2bc3b3627711 (diff) | |
download | flatbuffers-4fa4d367069748c318bfff041d71184dc1199270.tar.gz flatbuffers-4fa4d367069748c318bfff041d71184dc1199270.tar.bz2 flatbuffers-4fa4d367069748c318bfff041d71184dc1199270.zip |
Add default NaN/Inf to C#, Java, Python (#5107)
* Add default NaN/Inf to C#, Java, Python
* Python NaN/Inf test added
* Remove MSVC2010/13 dependent code
Diffstat (limited to 'src/idl_gen_python.cpp')
-rw-r--r-- | src/idl_gen_python.cpp | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/src/idl_gen_python.cpp b/src/idl_gen_python.cpp index 11491c00..fbb0805c 100644 --- a/src/idl_gen_python.cpp +++ b/src/idl_gen_python.cpp @@ -36,7 +36,8 @@ class PythonGenerator : public BaseGenerator { PythonGenerator(const Parser &parser, const std::string &path, const std::string &file_name) : BaseGenerator(parser, path, file_name, "" /* not used */, - "" /* not used */){ + "" /* not used */), + float_const_gen_("float('nan')", "float('inf')", "float('-inf')") { static const char * const keywords[] = { "False", "None", @@ -191,7 +192,7 @@ class PythonGenerator : public BaseGenerator { code += "(self):"; code += OffsetPrefix(field); getter += "o + self._tab.Pos)"; - auto is_bool = field.value.type.base_type == BASE_TYPE_BOOL; + auto is_bool = IsBool(field.value.type.base_type); if (is_bool) { getter = "bool(" + getter + ")"; } @@ -200,7 +201,9 @@ class PythonGenerator : public BaseGenerator { if (is_bool) { default_value = field.value.constant == "0" ? "False" : "True"; } else { - default_value = field.value.constant; + default_value = IsFloat(field.value.type.base_type) + ? float_const_gen_.GenFloatConstant(field) + : field.value.constant; } code += Indent + Indent + "return " + default_value + "\n\n"; } @@ -452,7 +455,10 @@ class PythonGenerator : public BaseGenerator { } else { code += MakeCamel(NormalizedName(field), false); } - code += ", " + field.value.constant; + code += ", "; + code += IsFloat(field.value.type.base_type) + ? float_const_gen_.GenFloatConstant(field) + : field.value.constant; code += ")\n"; } @@ -715,6 +721,7 @@ class PythonGenerator : public BaseGenerator { } private: std::unordered_set<std::string> keywords_; + const SimpleFloatConstantGenerator float_const_gen_; }; } // namespace python |