]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
Py3: round compatible with py2 187/head
authorMartin <martin.basti@gmail.com>
Sat, 2 Jul 2016 22:12:14 +0000 (00:12 +0200)
committerMartin <martin.basti@gmail.com>
Sat, 2 Jul 2016 22:15:06 +0000 (00:15 +0200)
Python 3 and Python 2 uses different rounding strategies in round().
Function round_py2_compat() do rounding in py2 compatible strategy for both py2 and py3

dns/_compat.py
dns/rdtypes/ANY/LOC.py
pylintrc

index a94b8422d9af5e4de3be01b45e73131732f89e3c..956f9a13324c6354f2aefdb6a548bbd2fbabaa96 100644 (file)
@@ -1,5 +1,6 @@
 import sys
-
+import decimal
+from decimal import Context
 
 if sys.version_info > (3,):
     long = int
@@ -29,3 +30,18 @@ else:
         return x
     def maybe_encode(x):
         return x
+
+
+def round_py2_compat(what):
+    """
+    Python 2 and Python 3 use different rounding strategies in round(). This
+    function ensures that results are python2/3 compatible and backward
+    compatible with previous py2 releases
+    :param what: float
+    :return: rounded long
+    """
+    d = Context(
+        prec=len(str(long(what))),  # round to integer with max precision
+        rounding=decimal.ROUND_HALF_UP
+    ).create_decimal(str(what))  # str(): python 2.6 compat
+    return long(d)
index a32d6db900852cd4565ccbb76f326de147c886f1..f2544c33b6d5a05b3e408825ac1e986f22cd3e68 100644 (file)
@@ -19,7 +19,7 @@ import struct
 
 import dns.exception
 import dns.rdata
-from dns._compat import long, xrange
+from dns._compat import long, xrange, round_py2_compat
 
 
 _pows = tuple(long(10**i) for i in range(0, 11))
@@ -49,7 +49,7 @@ def _float_to_tuple(what):
         what *= -1
     else:
         sign = 1
-    what = long(round(what * 3600000))
+    what = round_py2_compat(what * 3600000)
     degrees = int(what // 3600000)
     what -= degrees * 3600000
     minutes = int(what // 60000)
index 74e21961a1eb7019cee5c9367f2dc80a66a24e71..5ab42a1fc594df8a318ede9ded26dce85bf6b66b 100644 (file)
--- a/pylintrc
+++ b/pylintrc
@@ -31,7 +31,6 @@ disable=
     no-member,
     protected-access,
     redefined-builtin,
-    round-builtin,
     too-many-lines,
     unused-argument,
     unused-variable,