diff --git a/src/nacl/_lib/__init__.py b/src/nacl/_lib/__init__.py index e0032a339eb76b21a21f90d2720d33d2aa684953..4bc8a2141b2b613ab9197fc5ae0ef6bc2263e7d6 100644 --- a/src/nacl/_lib/__init__.py +++ b/src/nacl/_lib/__init__.py @@ -23,6 +23,7 @@ import six from nacl import _cffi_fix from cffi import FFI +from cffi.verifier import Verifier __all__ = ["ffi"] @@ -43,9 +44,8 @@ for header in HEADERS: ffi.cdef(hfile.read()) -# Compile our module # TODO: Can we use the ABI of libsodium for this instead? -lib = ffi.verify( +ffi.verifier = Verifier(ffi, "#include <sodium.h>", # We need to link to the sodium library @@ -56,11 +56,29 @@ lib = ffi.verify( ) -# Put all of the exposed functions onto the module -g = globals() -for name, function in six.iteritems(lib.__dict__): - # Add this function to the __all__ namespace - __all__.append(name) +class Library(object): - # Add this function to the globals - g[name] = function + def __init__(self, ffi): + self.ffi = ffi + self._initalized = False + + # This prevents the compile_module() from being called, the module + # should have been compiled by setup.py + def _compile_module(*args, **kwargs): + raise RuntimeError("Cannot compile module during runtime") + self.ffi.verifier.compile_module = _compile_module + + def __getattr__(self, name): + if not self._initalized: + self._lib = self.ffi.verifier.load_library() + + # redirect attribute access to the underlying lib + attr = getattr(self._lib, name) + + # Go ahead and assign the returned value to this class so we don't + # need to do this lookup again + setattr(self, name, attr) + + return attr + +lib = Library(ffi) diff --git a/src/nacl/c/crypto_box.py b/src/nacl/c/crypto_box.py index 57ba5ad2f97eaab67da5fcc24a23d4704e6a19e4..069ff5c0a3cda089c3bc3ec1041bdce729d39296 100644 --- a/src/nacl/c/crypto_box.py +++ b/src/nacl/c/crypto_box.py @@ -13,7 +13,7 @@ # limitations under the License. from __future__ import absolute_import, division, print_function -from nacl import _lib as lib +from nacl._lib import lib from nacl.exceptions import CryptoError diff --git a/src/nacl/c/crypto_hash.py b/src/nacl/c/crypto_hash.py index e69cc3e5d683bed7b87579b35a355e36dc0a00c5..de6c3be5025c5c71f830d82f735f484edd5d7dfc 100644 --- a/src/nacl/c/crypto_hash.py +++ b/src/nacl/c/crypto_hash.py @@ -13,7 +13,7 @@ # limitations under the License. from __future__ import absolute_import, division, print_function -from nacl import _lib as lib +from nacl._lib import lib from nacl.exceptions import CryptoError diff --git a/src/nacl/c/crypto_scalarmult.py b/src/nacl/c/crypto_scalarmult.py index 910552ba4af784084f2b9a5be4f84a0b2ea00c62..b94841e49879f3ff19e2b9d2eccca20bc48227bc 100644 --- a/src/nacl/c/crypto_scalarmult.py +++ b/src/nacl/c/crypto_scalarmult.py @@ -13,7 +13,7 @@ # limitations under the License. from __future__ import absolute_import, division, print_function -from nacl import _lib as lib +from nacl._lib import lib from nacl.exceptions import CryptoError diff --git a/src/nacl/c/crypto_secretbox.py b/src/nacl/c/crypto_secretbox.py index bc867e595f0fb1375237705ca3b65543da4591d7..94e6a66a35c2c0e78986571f4f070c4417de91af 100644 --- a/src/nacl/c/crypto_secretbox.py +++ b/src/nacl/c/crypto_secretbox.py @@ -13,7 +13,7 @@ # limitations under the License. from __future__ import absolute_import, division, print_function -from nacl import _lib as lib +from nacl._lib import lib from nacl.exceptions import CryptoError diff --git a/src/nacl/c/crypto_sign.py b/src/nacl/c/crypto_sign.py index 505d2658e46d5443bbddb3cd6a83f7cb02899633..7286de062889e0743feecb5e4c730af91df49397 100644 --- a/src/nacl/c/crypto_sign.py +++ b/src/nacl/c/crypto_sign.py @@ -13,7 +13,7 @@ # limitations under the License. from __future__ import absolute_import, division, print_function -from nacl import _lib as lib +from nacl._lib import lib from nacl.exceptions import BadSignatureError, CryptoError diff --git a/src/nacl/c/randombytes.py b/src/nacl/c/randombytes.py index cee7a5e3b4194a65898b28207ddad24da354e6fb..d6f01f5157bf62c1a96db27e4e4d30ca945f2e98 100644 --- a/src/nacl/c/randombytes.py +++ b/src/nacl/c/randombytes.py @@ -13,7 +13,7 @@ # limitations under the License. from __future__ import absolute_import, division, print_function -from nacl import _lib as lib +from nacl._lib import lib def randombytes(size):