diff options
author | kenton@google.com <kenton@google.com@630680e5-0e50-0410-840e-4b1c322b438d> | 2009-12-23 02:01:01 +0000 |
---|---|---|
committer | kenton@google.com <kenton@google.com@630680e5-0e50-0410-840e-4b1c322b438d> | 2009-12-23 02:01:01 +0000 |
commit | d0047c43d955174d79a2df623dbb4007965252b5 (patch) | |
tree | 43b2b4dbffe90e2ca7b00da247c6a631484d0bde /src | |
parent | eef5f8396dd527c17ab7e419ca8781052031d05d (diff) | |
download | protobuf-d0047c43d955174d79a2df623dbb4007965252b5.tar.gz protobuf-d0047c43d955174d79a2df623dbb4007965252b5.tar.bz2 protobuf-d0047c43d955174d79a2df623dbb4007965252b5.zip |
In Python, avoid relying on float('inf') and float('nan') as these don't work on Windows with Python pre-2.6.
Diffstat (limited to 'src')
-rw-r--r-- | src/google/protobuf/compiler/python/python_generator.cc | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/src/google/protobuf/compiler/python/python_generator.cc b/src/google/protobuf/compiler/python/python_generator.cc index 54ab0a2d..2f7d0981 100644 --- a/src/google/protobuf/compiler/python/python_generator.cc +++ b/src/google/protobuf/compiler/python/python_generator.cc @@ -168,11 +168,15 @@ string StringifyDefaultValue(const FieldDescriptor& field) { case FieldDescriptor::CPPTYPE_DOUBLE: { double value = field.default_value_double(); if (value == numeric_limits<double>::infinity()) { - return "float('inf')"; + // Python pre-2.6 on Windows does not parse "inf" correctly. However, + // parsing a number that is too big for a double will return infinity. + return "float('1e10000')"; } else if (value == -numeric_limits<double>::infinity()) { - return "float('-inf')"; + // See above. + return "float('-1e10000')"; } else if (value != value) { - return "float('nan')"; + // infinity * 0 = nan + return "(float('1e10000') * 0)"; } else { return SimpleDtoa(value); } @@ -180,11 +184,15 @@ string StringifyDefaultValue(const FieldDescriptor& field) { case FieldDescriptor::CPPTYPE_FLOAT: { float value = field.default_value_float(); if (value == numeric_limits<float>::infinity()) { - return "float('inf')"; + // Python pre-2.6 on Windows does not parse "inf" correctly. However, + // parsing a number that is too big for a double will return infinity. + return "float('1e10000')"; } else if (value == -numeric_limits<float>::infinity()) { - return "float('-inf')"; + // See above. + return "float('-1e10000')"; } else if (value != value) { - return "float('nan')"; + // infinity - infinity = nan + return "(float('1e10000') - float('1e10000'))"; } else { return SimpleFtoa(value); } |