diff --git a/lib/spack/spack/package.py b/lib/spack/spack/package.py
index 42e18b2a1ec3b01128dc421deeb55d1d0997e8ea..768605294f8e7cb3362e34cbe584994713ad67fb 100644
--- a/lib/spack/spack/package.py
+++ b/lib/spack/spack/package.py
@@ -40,7 +40,6 @@
 import time
 import string
 import contextlib
-from urlparse import urlparse
 from StringIO import StringIO
 
 import llnl.util.lock
@@ -63,6 +62,7 @@
 import spack.util.web
 
 from spack.stage import Stage, ResourceStage, StageComposite
+from spack.util.crypto import bit_length
 from spack.util.environment import dump_environment
 from spack.util.executable import ProcessError, which
 from spack.version import *
@@ -719,7 +719,7 @@ def prefix_lock(self):
             if prefix not in Package.prefix_locks:
                 Package.prefix_locks[prefix] = llnl.util.lock.Lock(
                     spack.installed_db.prefix_lock_path,
-                    self.spec.dag_hash_bit_prefix(sys.maxsize.bit_length()), 1)
+                    self.spec.dag_hash_bit_prefix(bit_length(sys.maxsize)), 1)
 
             self._prefix_lock = Package.prefix_locks[prefix]
 
diff --git a/lib/spack/spack/stage.py b/lib/spack/spack/stage.py
index 230defc67e3cc8d0cef09e1381146d4759b0c755..c0dfbba98716fa3a5f9bea9aece529a33be86a08 100644
--- a/lib/spack/spack/stage.py
+++ b/lib/spack/spack/stage.py
@@ -41,7 +41,7 @@
 import spack.fetch_strategy as fs
 import spack.error
 from spack.version import *
-from spack.util.crypto import prefix_bits
+from spack.util.crypto import prefix_bits, bit_length
 
 STAGE_PREFIX = 'spack-stage-'
 
@@ -161,7 +161,7 @@ def __init__(
         if lock:
             if self.name not in Stage.stage_locks:
                 sha1 = hashlib.sha1(self.name).digest()
-                lock_id = prefix_bits(sha1, sys.maxsize.bit_length())
+                lock_id = prefix_bits(sha1, bit_length(sys.maxsize))
                 stage_lock_path = join_path(spack.stage_path, '.lock')
 
                 Stage.stage_locks[self.name] = llnl.util.lock.Lock(
diff --git a/lib/spack/spack/util/crypto.py b/lib/spack/spack/util/crypto.py
index 6e17b74774935380d1b30b1871b327dd90596c38..d0747160223387a282e3988a2f916b41eb64a890 100644
--- a/lib/spack/spack/util/crypto.py
+++ b/lib/spack/spack/util/crypto.py
@@ -114,3 +114,10 @@ def prefix_bits(byte_array, bits):
 
     result >>= (n - bits)
     return result
+
+
+def bit_length(num):
+    """Number of bits required to represent an integer in binary."""
+    s = bin(num)
+    s = s.lstrip('-0b')
+    return len(s)