| OLD | NEW |
| (Empty) |
| 1 """Miscellaneous functions to mask Python/Jython differences.""" | |
| 2 | |
| 3 import os | |
| 4 import sha | |
| 5 | |
| 6 if os.name != "java": | |
| 7 BaseException = Exception | |
| 8 | |
| 9 from sets import Set | |
| 10 import array | |
| 11 import math | |
| 12 | |
| 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): | |
| 18 return a1+a2 | |
| 19 | |
| 20 def bytesToString(bytes): | |
| 21 return bytes.tostring() | |
| 22 | |
| 23 def stringToBytes(s): | |
| 24 bytes = createByteArrayZeros(0) | |
| 25 bytes.fromstring(s) | |
| 26 return bytes | |
| 27 | |
| 28 def numBits(n): | |
| 29 if n==0: | |
| 30 return 0 | |
| 31 return int(math.floor(math.log(n, 2))+1) | |
| 32 | |
| 33 class CertChainBase: pass | |
| 34 class SelfTestBase: pass | |
| 35 class ReportFuncBase: pass | |
| 36 | |
| 37 #Helper functions for working with sets (from Python 2.3) | |
| 38 def iterSet(set): | |
| 39 return iter(set) | |
| 40 | |
| 41 def getListFromSet(set): | |
| 42 return list(set) | |
| 43 | |
| 44 #Factory function for getting a SHA1 object | |
| 45 def getSHA1(s): | |
| 46 return sha.sha(s) | |
| 47 | |
| 48 import sys | |
| 49 import traceback | |
| 50 | |
| 51 def formatExceptionTrace(e): | |
| 52 newStr = "".join(traceback.format_exception(sys.exc_type, sys.exc_value,
sys.exc_traceback)) | |
| 53 return newStr | |
| 54 | |
| 55 else: | |
| 56 #Jython 2.1 is missing lots of python 2.3 stuff, | |
| 57 #which we have to emulate here: | |
| 58 import java | |
| 59 import jarray | |
| 60 | |
| 61 BaseException = java.lang.Exception | |
| 62 | |
| 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 a
re signed | |
| 67 def createByteArrayZeros(howMany): | |
| 68 return jarray.zeros(howMany, 'h') #use short instead of bytes, cause byt
es are signed | |
| 69 def concatArrays(a1, a2): | |
| 70 l = list(a1)+list(a2) | |
| 71 return createByteArraySequence(l) | |
| 72 | |
| 73 #WAY TOO SLOW - MUST BE REPLACED------------ | |
| 74 def bytesToString(bytes): | |
| 75 return "".join([chr(b) for b in bytes]) | |
| 76 | |
| 77 def stringToBytes(s): | |
| 78 bytes = createByteArrayZeros(len(s)) | |
| 79 for count, c in enumerate(s): | |
| 80 bytes[count] = ord(c) | |
| 81 return bytes | |
| 82 #WAY TOO SLOW - MUST BE REPLACED------------ | |
| 83 | |
| 84 def numBits(n): | |
| 85 if n==0: | |
| 86 return 0 | |
| 87 n= 1L * n; #convert to long, if it isn't already | |
| 88 return n.__tojava__(java.math.BigInteger).bitLength() | |
| 89 | |
| 90 #This properly creates static methods for Jython | |
| 91 class staticmethod: | |
| 92 def __init__(self, anycallable): self.__call__ = anycallable | |
| 93 | |
| 94 #Properties are not supported for Jython | |
| 95 class property: | |
| 96 def __init__(self, anycallable): pass | |
| 97 | |
| 98 #True and False have to be specially defined | |
| 99 False = 0 | |
| 100 True = 1 | |
| 101 | |
| 102 class StopIteration(Exception): pass | |
| 103 | |
| 104 def enumerate(collection): | |
| 105 return zip(range(len(collection)), collection) | |
| 106 | |
| 107 class Set: | |
| 108 def __init__(self, seq=None): | |
| 109 self.values = {} | |
| 110 if seq: | |
| 111 for e in seq: | |
| 112 self.values[e] = None | |
| 113 | |
| 114 def add(self, e): | |
| 115 self.values[e] = None | |
| 116 | |
| 117 def discard(self, e): | |
| 118 if e in self.values.keys(): | |
| 119 del(self.values[e]) | |
| 120 | |
| 121 def union(self, s): | |
| 122 ret = Set() | |
| 123 for e in self.values.keys(): | |
| 124 ret.values[e] = None | |
| 125 for e in s.values.keys(): | |
| 126 ret.values[e] = None | |
| 127 return ret | |
| 128 | |
| 129 def issubset(self, other): | |
| 130 for e in self.values.keys(): | |
| 131 if e not in other.values.keys(): | |
| 132 return False | |
| 133 return True | |
| 134 | |
| 135 def __nonzero__( self): | |
| 136 return len(self.values.keys()) | |
| 137 | |
| 138 def __contains__(self, e): | |
| 139 return e in self.values.keys() | |
| 140 | |
| 141 def iterSet(set): | |
| 142 return set.values.keys() | |
| 143 | |
| 144 def getListFromSet(set): | |
| 145 return set.values.keys() | |
| 146 | |
| 147 """ | |
| 148 class JCE_SHA1: | |
| 149 def __init__(self, s=None): | |
| 150 self.md = java.security.MessageDigest.getInstance("SHA1") | |
| 151 if s: | |
| 152 self.update(s) | |
| 153 | |
| 154 def update(self, s): | |
| 155 self.md.update(s) | |
| 156 | |
| 157 def copy(self): | |
| 158 sha1 = JCE_SHA1() | |
| 159 sha1.md = self.md.clone() | |
| 160 return sha1 | |
| 161 | |
| 162 def digest(self): | |
| 163 digest = self.md.digest() | |
| 164 bytes = jarray.zeros(20, 'h') | |
| 165 for count in xrange(20): | |
| 166 x = digest[count] | |
| 167 if x < 0: x += 256 | |
| 168 bytes[count] = x | |
| 169 return bytes | |
| 170 """ | |
| 171 | |
| 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 | |
| 176 def getSHA1(s): | |
| 177 #return JCE_SHA1(s) | |
| 178 return sha.sha(s) | |
| 179 | |
| 180 | |
| 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): | |
| 185 x = ord(c) | |
| 186 if x >= 128: x -= 256 | |
| 187 bytes[count] = x | |
| 188 return bytes | |
| 189 | |
| 190 import sys | |
| 191 import traceback | |
| 192 | |
| 193 def formatExceptionTrace(e): | |
| 194 newStr = "".join(traceback.format_exception(sys.exc_type, sys.exc_value,
sys.exc_traceback)) | |
| 195 return newStr | |
| OLD | NEW |