OLD | NEW |
1 # Author: Trevor Perrin | 1 # Author: Trevor Perrin |
2 # See the LICENSE file for legal information regarding use of this file. | 2 # See the LICENSE file for legal information regarding use of this file. |
3 | 3 |
4 """Miscellaneous functions to mask Python version differences.""" | 4 """Miscellaneous functions to mask Python version differences.""" |
5 | 5 |
6 import sys | 6 import sys |
7 import os | 7 import os |
8 import math | 8 import math |
9 import binascii | 9 import binascii |
10 | 10 |
(...skipping 27 matching lines...) Expand all Loading... |
38 except Exception as e: | 38 except Exception as e: |
39 raise SyntaxError("base64 error: %s" % e) | 39 raise SyntaxError("base64 error: %s" % e) |
40 return b | 40 return b |
41 | 41 |
42 def b2a_hex(b): | 42 def b2a_hex(b): |
43 return binascii.b2a_hex(b).decode("ascii") | 43 return binascii.b2a_hex(b).decode("ascii") |
44 | 44 |
45 def b2a_base64(b): | 45 def b2a_base64(b): |
46 return binascii.b2a_base64(b).decode("ascii") | 46 return binascii.b2a_base64(b).decode("ascii") |
47 | 47 |
48 def b2a_base32(b): | |
49 return base64.b32encode(b).decode("ascii") | |
50 | |
51 def readStdinBinary(): | 48 def readStdinBinary(): |
52 return sys.stdin.buffer.read() | 49 return sys.stdin.buffer.read() |
53 | 50 |
54 def long(n): | |
55 return n | |
56 | |
57 else: | 51 else: |
58 # Python 2.6 requires strings instead of bytearrays in a couple places, | 52 # Python 2.6 requires strings instead of bytearrays in a couple places, |
59 # so we define this function so it does the conversion if needed. | 53 # so we define this function so it does the conversion if needed. |
60 if sys.version_info < (2,7): | 54 if sys.version_info < (2,7): |
61 def compat26Str(x): return str(x) | 55 def compat26Str(x): return str(x) |
62 else: | 56 else: |
63 def compat26Str(x): return x | 57 def compat26Str(x): return x |
64 | 58 |
65 # So, python 2.6 requires strings, python 3 requires 'bytes', | 59 # So, python 2.6 requires strings, python 3 requires 'bytes', |
66 # and python 2.7 can handle bytearrays... | 60 # and python 2.7 can handle bytearrays... |
(...skipping 12 matching lines...) Expand all Loading... |
79 except Exception as e: | 73 except Exception as e: |
80 raise SyntaxError("base64 error: %s" % e) | 74 raise SyntaxError("base64 error: %s" % e) |
81 return b | 75 return b |
82 | 76 |
83 def b2a_hex(b): | 77 def b2a_hex(b): |
84 return binascii.b2a_hex(compat26Str(b)) | 78 return binascii.b2a_hex(compat26Str(b)) |
85 | 79 |
86 def b2a_base64(b): | 80 def b2a_base64(b): |
87 return binascii.b2a_base64(compat26Str(b)) | 81 return binascii.b2a_base64(compat26Str(b)) |
88 | 82 |
89 def b2a_base32(b): | |
90 return base64.b32encode(str(b)) | |
91 | |
92 import traceback | 83 import traceback |
93 def formatExceptionTrace(e): | 84 def formatExceptionTrace(e): |
94 newStr = "".join(traceback.format_exception(sys.exc_type, sys.exc_value, sys
.exc_traceback)) | 85 newStr = "".join(traceback.format_exception(sys.exc_type, sys.exc_value, sys
.exc_traceback)) |
95 return newStr | 86 return newStr |
96 | 87 |
OLD | NEW |