From: Martin Date: Sat, 2 Jul 2016 22:12:14 +0000 (+0200) Subject: Py3: round compatible with py2 X-Git-Tag: v1.15.0~36^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F187%2Fhead;p=thirdparty%2Fdnspython.git Py3: round compatible with py2 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 --- diff --git a/dns/_compat.py b/dns/_compat.py index a94b8422..956f9a13 100644 --- a/dns/_compat.py +++ b/dns/_compat.py @@ -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) diff --git a/dns/rdtypes/ANY/LOC.py b/dns/rdtypes/ANY/LOC.py index a32d6db9..f2544c33 100644 --- a/dns/rdtypes/ANY/LOC.py +++ b/dns/rdtypes/ANY/LOC.py @@ -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) diff --git a/pylintrc b/pylintrc index 74e21961..5ab42a1f 100644 --- a/pylintrc +++ b/pylintrc @@ -31,7 +31,6 @@ disable= no-member, protected-access, redefined-builtin, - round-builtin, too-many-lines, unused-argument, unused-variable,