-
Notifications
You must be signed in to change notification settings - Fork 15.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Treat -0.0 as consistently distinct from 0.0 in pure python
The previous treatment was a conformance violation, where implicit present float fields with a non-default value of -0.0 could get dropped. PiperOrigin-RevId: 705728806
- Loading branch information
1 parent
5188672
commit bc16fe8
Showing
7 changed files
with
75 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,7 @@ | |
__author__ = '[email protected] (Kenton Varda)' | ||
|
||
import math | ||
import numbers | ||
import struct | ||
|
||
from google.protobuf import message | ||
|
@@ -71,6 +72,27 @@ | |
_DecodeError = message.DecodeError | ||
|
||
|
||
def IsDefaultScalarValue(value): | ||
"""Returns whether or not a scalar value is the default value of its type. | ||
Specifically, this should be used to determine presence of implicit-presence | ||
fields, where we disallow custom defaults. | ||
Args: | ||
value: A scalar value to check. | ||
Returns: | ||
True if the value is equivalent to a default value, False otherwise. | ||
""" | ||
if isinstance(value, numbers.Number) and math.copysign(1.0, value) < 0: | ||
# Special case for negative zero, where "truthiness" fails to give the right | ||
# answer. | ||
return False | ||
|
||
# Normally, we can just use Python's boolean conversion. | ||
return not value | ||
|
||
|
||
def _VarintDecoder(mask, result_type): | ||
"""Return an encoder for a basic varint value (does not include tag). | ||
|
@@ -237,7 +259,7 @@ def DecodeField(buffer, pos, end, message, field_dict): | |
(new_value, pos) = decode_value(buffer, pos) | ||
if pos > end: | ||
raise _DecodeError('Truncated message.') | ||
if clear_if_default and not new_value: | ||
if clear_if_default and IsDefaultScalarValue(new_value): | ||
field_dict.pop(key, None) | ||
else: | ||
field_dict[key] = new_value | ||
|
@@ -478,7 +500,7 @@ def DecodeField(buffer, pos, end, message, field_dict): | |
(enum_value, pos) = _DecodeSignedVarint32(buffer, pos) | ||
if pos > end: | ||
raise _DecodeError('Truncated message.') | ||
if clear_if_default and not enum_value: | ||
if clear_if_default and IsDefaultScalarValue(enum_value): | ||
field_dict.pop(key, None) | ||
return pos | ||
# pylint: disable=protected-access | ||
|
@@ -573,7 +595,7 @@ def DecodeField(buffer, pos, end, message, field_dict): | |
new_pos = pos + size | ||
if new_pos > end: | ||
raise _DecodeError('Truncated string.') | ||
if clear_if_default and not size: | ||
if clear_if_default and IsDefaultScalarValue(size): | ||
field_dict.pop(key, None) | ||
else: | ||
field_dict[key] = _ConvertToUnicode(buffer[pos:new_pos]) | ||
|
@@ -614,7 +636,7 @@ def DecodeField(buffer, pos, end, message, field_dict): | |
new_pos = pos + size | ||
if new_pos > end: | ||
raise _DecodeError('Truncated string.') | ||
if clear_if_default and not size: | ||
if clear_if_default and IsDefaultScalarValue(size): | ||
field_dict.pop(key, None) | ||
else: | ||
field_dict[key] = buffer[pos:new_pos].tobytes() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters