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