1 """Miscellaneous functions to mask Python/Jython differences."""
7 BaseException = Exception
13 def createByteArraySequence(seq):
14 return array.array('B', seq)
15 def createByteArrayZeros(howMany):
16 return array.array('B', [0] * howMany)
17 def concatArrays(a1, a2):
20 def bytesToString(bytes):
21 return bytes.tostring()
24 bytes = createByteArrayZeros(0)
31 return int(math.floor(math.log(n, 2))+1)
33 class CertChainBase: pass
34 class SelfTestBase: pass
35 class ReportFuncBase: pass
37 #Helper functions for working with sets (from Python 2.3)
41 def getListFromSet(set):
44 #Factory function for getting a SHA1 object
51 def formatExceptionTrace(e):
52 newStr = "".join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
56 #Jython 2.1 is missing lots of python 2.3 stuff,
57 #which we have to emulate here:
61 BaseException = java.lang.Exception
63 def createByteArraySequence(seq):
64 if isinstance(seq, type("")): #If it's a string, convert
65 seq = [ord(c) for c in seq]
66 return jarray.array(seq, 'h') #use short instead of bytes, cause bytes are signed
67 def createByteArrayZeros(howMany):
68 return jarray.zeros(howMany, 'h') #use short instead of bytes, cause bytes are signed
69 def concatArrays(a1, a2):
71 return createByteArraySequence(l)
73 #WAY TOO SLOW - MUST BE REPLACED------------
74 def bytesToString(bytes):
75 return "".join([chr(b) for b in bytes])
78 bytes = createByteArrayZeros(len(s))
79 for count, c in enumerate(s):
82 #WAY TOO SLOW - MUST BE REPLACED------------
87 n= 1L * n; #convert to long, if it isn't already
88 return n.__tojava__(java.math.BigInteger).bitLength()
90 #This properly creates static methods for Jython
92 def __init__(self, anycallable): self.__call__ = anycallable
94 #Properties are not supported for Jython
96 def __init__(self, anycallable): pass
98 #True and False have to be specially defined
102 class StopIteration(Exception): pass
104 def enumerate(collection):
105 return zip(range(len(collection)), collection)
108 def __init__(self, seq=None):
112 self.values[e] = None
115 self.values[e] = None
117 def discard(self, e):
118 if e in self.values.keys():
123 for e in self.values.keys():
125 for e in s.values.keys():
129 def issubset(self, other):
130 for e in self.values.keys():
131 if e not in other.values.keys():
135 def __nonzero__( self):
136 return len(self.values.keys())
138 def __contains__(self, e):
139 return e in self.values.keys()
142 return set.values.keys()
144 def getListFromSet(set):
145 return set.values.keys()
149 def __init__(self, s=None):
150 self.md = java.security.MessageDigest.getInstance("SHA1")
159 sha1.md = self.md.clone()
163 digest = self.md.digest()
164 bytes = jarray.zeros(20, 'h')
165 for count in xrange(20):
172 #Factory function for getting a SHA1 object
173 #The JCE_SHA1 class is way too slow...
174 #the sha.sha object we use instead is broken in the jython 2.1
175 #release, and needs to be patched
181 #Adjust the string to an array of bytes
182 def stringToJavaByteArray(s):
183 bytes = jarray.zeros(len(s), 'b')
184 for count, c in enumerate(s):
186 if x >= 128: x -= 256
193 def formatExceptionTrace(e):
194 newStr = "".join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))