Package dns :: Module _compat
[hide private]
[frames] | no frames]

Source Code for Module dns._compat

 1  import sys 
 2  import decimal 
 3  from decimal import Context 
 4   
 5  PY3 = sys.version_info[0] == 3 
 6  PY2 = sys.version_info[0] == 2 
 7   
 8   
 9  if PY3: 
10      long = int 
11      xrange = range 
12  else: 
13      long = long  # pylint: disable=long-builtin 
14      xrange = xrange  # pylint: disable=xrange-builtin 
15   
16  # unicode / binary types 
17  if PY3: 
18      text_type = str 
19      binary_type = bytes 
20      string_types = (str,) 
21      unichr = chr 
22 - def maybe_decode(x):
23 return x.decode()
24 - def maybe_encode(x):
25 return x.encode()
26 - def maybe_chr(x):
27 return x
28 - def maybe_ord(x):
29 return x
30 else: 31 text_type = unicode # pylint: disable=unicode-builtin, undefined-variable 32 binary_type = str 33 string_types = ( 34 basestring, # pylint: disable=basestring-builtin, undefined-variable 35 ) 36 unichr = unichr # pylint: disable=unichr-builtin
37 - def maybe_decode(x):
38 return x
39 - def maybe_encode(x):
40 return x
41 - def maybe_chr(x):
42 return chr(x)
43 - def maybe_ord(x):
44 return ord(x)
45 46
47 -def round_py2_compat(what):
48 """ 49 Python 2 and Python 3 use different rounding strategies in round(). This 50 function ensures that results are python2/3 compatible and backward 51 compatible with previous py2 releases 52 :param what: float 53 :return: rounded long 54 """ 55 d = Context( 56 prec=len(str(long(what))), # round to integer with max precision 57 rounding=decimal.ROUND_HALF_UP 58 ).create_decimal(str(what)) # str(): python 2.6 compat 59 return long(d)
60