/gdata/tlslite/utils/compat.py

http://radioappz.googlecode.com/ · Python · 140 lines · 105 code · 26 blank · 9 comment · 19 complexity · 4cbc28ec369009f768bd602d3e01f5b5 MD5 · raw file

  1. """Miscellaneous functions to mask Python version differences."""
  2. import sys
  3. import os
  4. if sys.version_info < (2,2):
  5. raise AssertionError("Python 2.2 or later required")
  6. if sys.version_info < (2,3):
  7. def enumerate(collection):
  8. return zip(range(len(collection)), collection)
  9. class Set:
  10. def __init__(self, seq=None):
  11. self.values = {}
  12. if seq:
  13. for e in seq:
  14. self.values[e] = None
  15. def add(self, e):
  16. self.values[e] = None
  17. def discard(self, e):
  18. if e in self.values.keys():
  19. del(self.values[e])
  20. def union(self, s):
  21. ret = Set()
  22. for e in self.values.keys():
  23. ret.values[e] = None
  24. for e in s.values.keys():
  25. ret.values[e] = None
  26. return ret
  27. def issubset(self, other):
  28. for e in self.values.keys():
  29. if e not in other.values.keys():
  30. return False
  31. return True
  32. def __nonzero__( self):
  33. return len(self.values.keys())
  34. def __contains__(self, e):
  35. return e in self.values.keys()
  36. def __iter__(self):
  37. return iter(set.values.keys())
  38. if os.name != "java":
  39. import array
  40. def createByteArraySequence(seq):
  41. return array.array('B', seq)
  42. def createByteArrayZeros(howMany):
  43. return array.array('B', [0] * howMany)
  44. def concatArrays(a1, a2):
  45. return a1+a2
  46. def bytesToString(bytes):
  47. return bytes.tostring()
  48. def stringToBytes(s):
  49. bytes = createByteArrayZeros(0)
  50. bytes.fromstring(s)
  51. return bytes
  52. import math
  53. def numBits(n):
  54. if n==0:
  55. return 0
  56. s = "%x" % n
  57. return ((len(s)-1)*4) + \
  58. {'0':0, '1':1, '2':2, '3':2,
  59. '4':3, '5':3, '6':3, '7':3,
  60. '8':4, '9':4, 'a':4, 'b':4,
  61. 'c':4, 'd':4, 'e':4, 'f':4,
  62. }[s[0]]
  63. return int(math.floor(math.log(n, 2))+1)
  64. BaseException = Exception
  65. import sys
  66. import traceback
  67. def formatExceptionTrace(e):
  68. newStr = "".join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
  69. return newStr
  70. else:
  71. #Jython 2.1 is missing lots of python 2.3 stuff,
  72. #which we have to emulate here:
  73. #NOTE: JYTHON SUPPORT NO LONGER WORKS, DUE TO USE OF GENERATORS.
  74. #THIS CODE IS LEFT IN SO THAT ONE JYTHON UPDATES TO 2.2, IT HAS A
  75. #CHANCE OF WORKING AGAIN.
  76. import java
  77. import jarray
  78. def createByteArraySequence(seq):
  79. if isinstance(seq, type("")): #If it's a string, convert
  80. seq = [ord(c) for c in seq]
  81. return jarray.array(seq, 'h') #use short instead of bytes, cause bytes are signed
  82. def createByteArrayZeros(howMany):
  83. return jarray.zeros(howMany, 'h') #use short instead of bytes, cause bytes are signed
  84. def concatArrays(a1, a2):
  85. l = list(a1)+list(a2)
  86. return createByteArraySequence(l)
  87. #WAY TOO SLOW - MUST BE REPLACED------------
  88. def bytesToString(bytes):
  89. return "".join([chr(b) for b in bytes])
  90. def stringToBytes(s):
  91. bytes = createByteArrayZeros(len(s))
  92. for count, c in enumerate(s):
  93. bytes[count] = ord(c)
  94. return bytes
  95. #WAY TOO SLOW - MUST BE REPLACED------------
  96. def numBits(n):
  97. if n==0:
  98. return 0
  99. n= 1L * n; #convert to long, if it isn't already
  100. return n.__tojava__(java.math.BigInteger).bitLength()
  101. #Adjust the string to an array of bytes
  102. def stringToJavaByteArray(s):
  103. bytes = jarray.zeros(len(s), 'b')
  104. for count, c in enumerate(s):
  105. x = ord(c)
  106. if x >= 128: x -= 256
  107. bytes[count] = x
  108. return bytes
  109. BaseException = java.lang.Exception
  110. import sys
  111. import traceback
  112. def formatExceptionTrace(e):
  113. newStr = "".join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
  114. return newStr