Commits

Anonymous committed 7950c99

Jython 2.2 support added/fixed (at least as far as demo_pk_rsa_sign.py goes).

  • Participants
  • Parent commits 0306db7

Comments (0)

Files changed (1)

tlslite/utils/compat.py

 
 import sys
 import os
+import math
 
 if sys.version_info < (2,2):
     raise AssertionError("Python 2.2 or later required")
         def __iter__(self):
             return iter(set.values.keys())
 
+def numBits(n):
+    if n==0:
+        return 0
+    s = "%x" % n
+    return ((len(s)-1)*4) + \
+    {'0':0, '1':1, '2':2, '3':2,
+     '4':3, '5':3, '6':3, '7':3,
+     '8':4, '9':4, 'a':4, 'b':4,
+     'c':4, 'd':4, 'e':4, 'f':4,
+     }[s[0]]
+    return int(math.floor(math.log(n, 2))+1)
+
 
 if os.name == "java":
     #Jython 2.1 is missing lots of python 2.3 stuff,
         return bytes
     #WAY TOO SLOW - MUST BE REPLACED------------
 
-    def numBits(n):
-        if n==0:
-            return 0
-        n= 1L * n; #convert to long, if it isn't already
-        return n.__tojava__(java.math.BigInteger).bitLength()
+    if sys.version_info < (2, 2):
+        def numBits(n):
+            if n==0:
+                return 0
+            n= 1L * n; #convert to long, if it isn't already
+            return n.__tojava__(java.math.BigInteger).bitLength()
 
     #Adjust the string to an array of bytes
     def stringToJavaByteArray(s):
         bytes.fromstring(s)
         return bytes
 
-    import math
-    def numBits(n):
-        if n==0:
-            return 0
-        s = "%x" % n
-        return ((len(s)-1)*4) + \
-        {'0':0, '1':1, '2':2, '3':2,
-         '4':3, '5':3, '6':3, '7':3,
-         '8':4, '9':4, 'a':4, 'b':4,
-         'c':4, 'd':4, 'e':4, 'f':4,
-         }[s[0]]
-        return int(math.floor(math.log(n, 2))+1)
-
     BaseException = Exception
     import sys
     import traceback