From 6f95fa8db378bbb913a01f9daf10e71fcd8451b4 Mon Sep 17 00:00:00 2001 From: Donald Stufft <donald@stufft.io> Date: Mon, 7 Oct 2013 08:32:25 -0400 Subject: [PATCH] Ensure there is no implicit compilation again --- src/nacl/_lib/__init__.py | 36 ++++++++++++++++++++++++--------- src/nacl/c/crypto_box.py | 2 +- src/nacl/c/crypto_hash.py | 2 +- src/nacl/c/crypto_scalarmult.py | 2 +- src/nacl/c/crypto_secretbox.py | 2 +- src/nacl/c/crypto_sign.py | 2 +- src/nacl/c/randombytes.py | 2 +- 7 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/nacl/_lib/__init__.py b/src/nacl/_lib/__init__.py index e0032a33..4bc8a214 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 57ba5ad2..069ff5c0 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 e69cc3e5..de6c3be5 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 910552ba..b94841e4 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 bc867e59..94e6a66a 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 505d2658..7286de06 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 cee7a5e3..d6f01f51 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): -- GitLab