aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDonald Stufft <donald@stufft.io>2014-11-07 18:25:22 -0500
committerDonald Stufft <donald@stufft.io>2014-11-07 21:13:18 -0500
commit5fb44c0a96c71f28ae438060a3a1615977c740cb (patch)
tree59428057f829d393480411fc2114fb8a5b4d58d4
parentd1b70f31e7fabf6320d077235b74afd5d61a6144 (diff)
downloadcryptography-5fb44c0a96c71f28ae438060a3a1615977c740cb.tar.gz
cryptography-5fb44c0a96c71f28ae438060a3a1615977c740cb.tar.bz2
cryptography-5fb44c0a96c71f28ae438060a3a1615977c740cb.zip
Lazily load the library on first use instead of on import
-rw-r--r--cryptography/hazmat/bindings/utils.py16
-rw-r--r--cryptography/hazmat/primitives/constant_time.py4
-rw-r--r--cryptography/hazmat/primitives/padding.py4
3 files changed, 20 insertions, 4 deletions
diff --git a/cryptography/hazmat/bindings/utils.py b/cryptography/hazmat/bindings/utils.py
index f741aada..0fceac28 100644
--- a/cryptography/hazmat/bindings/utils.py
+++ b/cryptography/hazmat/bindings/utils.py
@@ -14,6 +14,7 @@
from __future__ import absolute_import, division, print_function
import binascii
+import threading
import sys
@@ -21,6 +22,21 @@ from cffi import FFI
from cffi.verifier import Verifier
+class LazyLibrary(object):
+ def __init__(self, ffi):
+ self._ffi = ffi
+ self._lib = None
+ self._lock = threading.Lock()
+
+ def __getattr__(self, name):
+ if self._lib is None:
+ with self._lock:
+ if self._lib is None:
+ self._lib = self._ffi.verifier.load_library()
+
+ return getattr(self._lib, name)
+
+
def load_library_for_binding(ffi, module_prefix, modules):
lib = ffi.verifier.load_library()
diff --git a/cryptography/hazmat/primitives/constant_time.py b/cryptography/hazmat/primitives/constant_time.py
index 8ba045aa..a14eda85 100644
--- a/cryptography/hazmat/primitives/constant_time.py
+++ b/cryptography/hazmat/primitives/constant_time.py
@@ -16,7 +16,7 @@ from __future__ import absolute_import, division, print_function
import hmac
import os
-from cryptography.hazmat.bindings.utils import build_ffi
+from cryptography.hazmat.bindings.utils import LazyLibrary, build_ffi
with open(os.path.join(os.path.dirname(__file__), "src/constant_time.h")) as f:
@@ -27,7 +27,7 @@ with open(os.path.join(os.path.dirname(__file__), "src/constant_time.c")) as f:
_ffi = build_ffi(cdef_source=TYPES, verify_source=FUNCTIONS)
-_lib = _ffi.verifier.load_library()
+_lib = LazyLibrary(_ffi)
if hasattr(hmac, "compare_digest"):
diff --git a/cryptography/hazmat/primitives/padding.py b/cryptography/hazmat/primitives/padding.py
index 6bb51ab9..7aeeff7c 100644
--- a/cryptography/hazmat/primitives/padding.py
+++ b/cryptography/hazmat/primitives/padding.py
@@ -17,7 +17,7 @@ import six
from cryptography import utils
from cryptography.exceptions import AlreadyFinalized
-from cryptography.hazmat.bindings.utils import build_ffi
+from cryptography.hazmat.bindings.utils import LazyLibrary, build_ffi
from cryptography.hazmat.primitives import interfaces
@@ -65,7 +65,7 @@ uint8_t Cryptography_check_pkcs7_padding(const uint8_t *data,
_ffi = build_ffi(cdef_source=TYPES, verify_source=FUNCTIONS)
-_lib = _ffi.verifier.load_library()
+_lib = LazyLibrary(_ffi)
class PKCS7(object):