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 int_types = tuple([int]) 59
60 - def formatExceptionTrace(e):
61 """Return exception information formatted as string""" 62 return str(e)
63 64 else: 65 # Python 2.6 requires strings instead of bytearrays in a couple places, 66 # so we define this function so it does the conversion if needed. 67 # same thing with very old 2.7 versions 68 if sys.version_info < (2, 7) or sys.version_info < (2, 7, 4):
69 - def compat26Str(x): return str(x)
70 else:
71 - def compat26Str(x): return x
72 73 # So, python 2.6 requires strings, python 3 requires 'bytes', 74 # and python 2.7 can handle bytearrays...
75 - def compatHMAC(x): return compat26Str(x)
76
77 - def a2b_hex(s):
78 try: 79 b = bytearray(binascii.a2b_hex(s)) 80 except Exception as e: 81 raise SyntaxError("base16 error: %s" % e) 82 return b
83
84 - def a2b_base64(s):
85 try: 86 b = bytearray(binascii.a2b_base64(s)) 87 except Exception as e: 88 raise SyntaxError("base64 error: %s" % e) 89 return b
90
91 - def b2a_hex(b):
92 return binascii.b2a_hex(compat26Str(b))
93
94 - def b2a_base64(b):
95 return binascii.b2a_base64(compat26Str(b))
96
97 - def compatLong(num):
98 return long(num)
99 100 int_types = (int, long) 101 102 # pylint on Python3 goes nuts for the sys dereferences... 103 104 #pylint: disable=no-member
105 - def formatExceptionTrace(e):
106 """Return exception information formatted as string""" 107 newStr = "".join(traceback.format_exception(sys.exc_type, 108 sys.exc_value, 109 sys.exc_traceback)) 110 return newStr
111 #pylint: enable=no-member 112 113 try: 114 # Fedora and Red Hat Enterprise Linux versions have small curves removed 115 getattr(ecdsa, 'NIST192p') 116 except AttributeError: 117 ecdsaAllCurves = False 118 else: 119 ecdsaAllCurves = True 120