Package tlslite :: Package utils :: Module compat
[hide private]
[frames] | no frames]

Source Code for Module tlslite.utils.compat

  1  # Author: Trevor Perrin 
  2  # See the LICENSE file for legal information regarding use of this file. 
  3   
  4  """Miscellaneous functions to mask Python version differences.""" 
  5   
  6  import sys 
  7  import os 
  8  import math 
  9  import binascii 
 10  import traceback 
 11  import ecdsa 
 12   
 13  if sys.version_info >= (3,0): 
 14   
15 - def compat26Str(x): return x
16 17 # Python 3 requires bytes instead of bytearrays for HMAC 18 19 # So, python 2.6 requires strings, python 3 requires 'bytes', 20 # and python 2.7 can handle bytearrays...
21 - def compatHMAC(x): return bytes(x)
22
23 - def raw_input(s):
24 return input(s)
25 26 # So, the python3 binascii module deals with bytearrays, and python2 27 # deals with strings... I would rather deal with the "a" part as 28 # strings, and the "b" part as bytearrays, regardless of python version, 29 # so...
30 - def a2b_hex(s):
31 try: 32 b = bytearray(binascii.a2b_hex(bytearray(s, "ascii"))) 33 except Exception as e: 34 raise SyntaxError("base16 error: %s" % e) 35 return b
36
37 - def a2b_base64(s):
38 try: 39 if isinstance(s, str): 40 s = bytearray(s, "ascii") 41 b = bytearray(binascii.a2b_base64(s)) 42 except Exception as e: 43 raise SyntaxError("base64 error: %s" % e) 44 return b
45
46 - def b2a_hex(b):
47 return binascii.b2a_hex(b).decode("ascii")
48
49 - def b2a_base64(b):
50 return binascii.b2a_base64(b).decode("ascii")
51
52 - def readStdinBinary():
53 return sys.stdin.buffer.read()
54
55 - def compatLong(num):
56 return int(num)
57
58 - def formatExceptionTrace(e):
59 """Return exception information formatted as string""" 60 return str(e)
61 62 else: 63 # Python 2.6 requires strings instead of bytearrays in a couple places, 64 # so we define this function so it does the conversion if needed. 65 # same thing with very old 2.7 versions 66 if sys.version_info < (2, 7) or sys.version_info < (2, 7, 4):
67 - def compat26Str(x): return str(x)
68 else:
69 - def compat26Str(x): return x
70 71 # So, python 2.6 requires strings, python 3 requires 'bytes', 72 # and python 2.7 can handle bytearrays...
73 - def compatHMAC(x): return compat26Str(x)
74
75 - def a2b_hex(s):
76 try: 77 b = bytearray(binascii.a2b_hex(s)) 78 except Exception as e: 79 raise SyntaxError("base16 error: %s" % e) 80 return b
81
82 - def a2b_base64(s):
83 try: 84 b = bytearray(binascii.a2b_base64(s)) 85 except Exception as e: 86 raise SyntaxError("base64 error: %s" % e) 87 return b
88
89 - def b2a_hex(b):
90 return binascii.b2a_hex(compat26Str(b))
91
92 - def b2a_base64(b):
93 return binascii.b2a_base64(compat26Str(b))
94
95 - def compatLong(num):
96 return long(num)
97 98 # pylint on Python3 goes nuts for the sys dereferences... 99 100 #pylint: disable=no-member
101 - def formatExceptionTrace(e):
102 """Return exception information formatted as string""" 103 newStr = "".join(traceback.format_exception(sys.exc_type, 104 sys.exc_value, 105 sys.exc_traceback)) 106 return newStr
107 #pylint: enable=no-member 108 109 try: 110 # Fedora and Red Hat Enterprise Linux versions have small curves removed 111 getattr(ecdsa, 'NIST192p') 112 except AttributeError: 113 ecdsaAllCurves = False 114 else: 115 ecdsaAllCurves = True 116