mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 12:05:50 +00:00
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
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
46208b1e91
commit
4fa4d36706
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user