[ovs-dev,1/3] Ensure significand remains an integer in Python3 json parser
diff mbox

Message ID 1464740092-4333-2-git-send-email-twilson@redhat.com
State Superseded
Headers show

Commit Message

Terry Wilson June 1, 2016, 12:14 a.m. UTC
The / operation in Python 2 is "floor division" for int/long types
while in Python 3 is "true division". This means that the
significand can become a float with the existing code in Python 3.
This, in turn, can result in a parse of something like [1.10e1]
returning 11 in Python 2 and 11.0 in Python 3. Switching to the
// operator resolves this difference.

The JSON tests do not catch this difference because the built-in
serializer prints floats with the %.15g format which will convert
floats with no fractional part to an integer representation.
---
 python/ovs/json.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Ben Pfaff June 7, 2016, 3:34 p.m. UTC | #1
On Tue, May 31, 2016 at 07:14:50PM -0500, Terry Wilson wrote:
> The / operation in Python 2 is "floor division" for int/long types
> while in Python 3 is "true division". This means that the
> significand can become a float with the existing code in Python 3.
> This, in turn, can result in a parse of something like [1.10e1]
> returning 11 in Python 2 and 11.0 in Python 3. Switching to the
> // operator resolves this difference.
> 
> The JSON tests do not catch this difference because the built-in
> serializer prints floats with the %.15g format which will convert
> floats with no fractional part to an integer representation.

Applied, thanks!
Ben Pfaff June 7, 2016, 3:36 p.m. UTC | #2
On Tue, Jun 07, 2016 at 08:34:53AM -0700, Ben Pfaff wrote:
> On Tue, May 31, 2016 at 07:14:50PM -0500, Terry Wilson wrote:
> > The / operation in Python 2 is "floor division" for int/long types
> > while in Python 3 is "true division". This means that the
> > significand can become a float with the existing code in Python 3.
> > This, in turn, can result in a parse of something like [1.10e1]
> > returning 11 in Python 2 and 11.0 in Python 3. Switching to the
> > // operator resolves this difference.
> > 
> > The JSON tests do not catch this difference because the built-in
> > serializer prints floats with the %.15g format which will convert
> > floats with no fractional part to an integer representation.
> 
> Applied, thanks!

Oops, I had to un-apply this due to lack of Signed-off-by.

Patch
diff mbox

diff --git a/python/ovs/json.py b/python/ovs/json.py
index 42e697d..ff986ea 100644
--- a/python/ovs/json.py
+++ b/python/ovs/json.py
@@ -280,7 +280,7 @@  class Parser(object):
                     significand *= 10
                     pow10 -= 1
                 while pow10 < 0 and significand % 10 == 0:
-                    significand /= 10
+                    significand //= 10
                     pow10 += 1
                 if (pow10 == 0 and
                     ((not sign and significand < 2 ** 63) or