From 0a95ae4041017f0cdc28777538896aa99fda53dc Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Fri, 13 Dec 2024 14:17:55 -0800 Subject: [PATCH] Update pure python float checking. Our C++ strtod was updated to handle text-format rounding about a month after it was ported to python. PiperOrigin-RevId: 706000143 --- conformance/text_format_failure_list_python.txt | 9 --------- python/google/protobuf/internal/type_checkers.py | 5 +++++ 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/conformance/text_format_failure_list_python.txt b/conformance/text_format_failure_list_python.txt index 2a0aed5d5dec..e25f68052b52 100644 --- a/conformance/text_format_failure_list_python.txt +++ b/conformance/text_format_failure_list_python.txt @@ -7,12 +7,3 @@ Required.*.TextFormatInput.StringLiteralBasicEscapesBytes.ProtobufOutput Required.*.TextFormatInput.StringLiteralBasicEscapesBytes.TextFormatOutput # Output was not equivalent to reference message: modified: optional_bytes: "\007\010\014\n\r\t\013?\\\'\"" -> "\007\010\014\n\r\t Required.*.TextFormatInput.StringLiteralBasicEscapesString.ProtobufOutput # Output was not equivalent to reference message: modified: optional_string: "\007\010\014\n\r\t\013?\\\'\"" -> "\007\010\014\n\r\ Required.*.TextFormatInput.StringLiteralBasicEscapesString.TextFormatOutput # Output was not equivalent to reference message: modified: optional_string: "\007\010\014\n\r\t\013?\\\'\"" -> "\007\010\014\n\r\ - -# Optional float interpreted as `inf` -Required.*.TextFormatInput.FloatFieldMaxValue.ProtobufOutput # Output was not equivalent to reference message -Required.*.TextFormatInput.FloatFieldMaxValue.TextFormatOutput # Output was not equivalent to reference message -Required.*.TextFormatInput.FloatFieldMaxValue_f.ProtobufOutput # Output was not equivalent to reference message -Required.*.TextFormatInput.FloatFieldMaxValue_f.TextFormatOutput # Output was not equivalent to reference message -Required.*.TextFormatInput.FloatFieldMaxValue_F.ProtobufOutput # Output was not equivalent to reference message -Required.*.TextFormatInput.FloatFieldMaxValue_F.TextFormatOutput # Output was not equivalent to reference message - diff --git a/python/google/protobuf/internal/type_checkers.py b/python/google/protobuf/internal/type_checkers.py index 04ccc9850018..0a932944acbd 100755 --- a/python/google/protobuf/internal/type_checkers.py +++ b/python/google/protobuf/internal/type_checkers.py @@ -231,6 +231,7 @@ class Uint64ValueChecker(IntValueChecker): # The max 4 bytes float is about 3.4028234663852886e+38 _FLOAT_MAX = float.fromhex('0x1.fffffep+127') _FLOAT_MIN = -_FLOAT_MAX +_MAX_FLOAT_AS_DOUBLE_ROUNDED = 3.4028235677973366e38 _INF = float('inf') _NEG_INF = float('-inf') @@ -269,8 +270,12 @@ def CheckValue(self, proposed_value): converted_value = super().CheckValue(proposed_value) # This inf rounding matches the C++ proto SafeDoubleToFloat logic. if converted_value > _FLOAT_MAX: + if converted_value <= _MAX_FLOAT_AS_DOUBLE_ROUNDED: + return _FLOAT_MAX return _INF if converted_value < _FLOAT_MIN: + if converted_value >= -_MAX_FLOAT_AS_DOUBLE_ROUNDED: + return _FLOAT_MIN return _NEG_INF return TruncateToFourByteFloat(converted_value)