From d0047c43d955174d79a2df623dbb4007965252b5 Mon Sep 17 00:00:00 2001 From: "kenton@google.com" Date: Wed, 23 Dec 2009 02:01:01 +0000 Subject: In Python, avoid relying on float('inf') and float('nan') as these don't work on Windows with Python pre-2.6. --- .../protobuf/compiler/python/python_generator.cc | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'src') 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::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::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::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::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); } -- cgit v1.2.3