-
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.
Fixed printing of nan floats/doubles in Python.
The second assert in _upb_EncodeRoundTripFloat is raised if val is a nan. This fix just returns the output of first spnprintf. I am not sure how changes to this repo are made so feel free to ignore this CL. To test this, you could 1. Define a proto with a float field message Test { float val = 1; } 2. In a python script, import the library and then set the val to nan and try to print it. proto = Test(val=float('nan')) print(proto) This will cause a coredump due to assertion error: assert.h assertion failed at third_party/upb/upb/lex/round_trip.c:46 in void _upb_EncodeRoundTripFloat(float, char *, size_t): strtof(buf, NULL) == val Added the corresponding change to double too PiperOrigin-RevId: 637127851
- Loading branch information
1 parent
ee98ba2
commit f651080
Showing
4 changed files
with
65 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include "upb/lex/round_trip.h" | ||
|
||
#include <math.h> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
namespace { | ||
|
||
TEST(RoundTripTest, Double) { | ||
char buf[32]; | ||
|
||
_upb_EncodeRoundTripDouble(0.123456789, buf, sizeof(buf)); | ||
EXPECT_STREQ(buf, "0.123456789"); | ||
|
||
_upb_EncodeRoundTripDouble(0.0, buf, sizeof(buf)); | ||
EXPECT_STREQ(buf, "0"); | ||
|
||
_upb_EncodeRoundTripDouble(nan(""), buf, sizeof(buf)); | ||
EXPECT_STREQ(buf, "nan"); | ||
} | ||
|
||
TEST(RoundTripTest, Float) { | ||
char buf[32]; | ||
|
||
_upb_EncodeRoundTripFloat(0.123456, buf, sizeof(buf)); | ||
EXPECT_STREQ(buf, "0.123456"); | ||
|
||
_upb_EncodeRoundTripFloat(0.0, buf, sizeof(buf)); | ||
EXPECT_STREQ(buf, "0"); | ||
|
||
_upb_EncodeRoundTripFloat(nan(""), buf, sizeof(buf)); | ||
EXPECT_STREQ(buf, "nan"); | ||
} | ||
|
||
} // namespace |