From b477cf287fcd4082594cb2d38623a9beaa822c7b Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Mon, 4 Nov 2013 10:17:42 -0800 Subject: Moved the cffi attributes on the OpenSSL backend to be class attributes, so they're shared between all isntances --- cryptography/hazmat/bindings/openssl/backend.py | 36 ++++++++++++++++--------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/cryptography/hazmat/bindings/openssl/backend.py b/cryptography/hazmat/bindings/openssl/backend.py index 32adfed9..7367818a 100644 --- a/cryptography/hazmat/bindings/openssl/backend.py +++ b/cryptography/hazmat/bindings/openssl/backend.py @@ -55,17 +55,31 @@ class Backend(object): "x509v3", ] + ffi = None + lib = None + def __init__(self): - self.ffi = cffi.FFI() + self._ensure_ffi_initialized() + + self.ciphers = Ciphers(self) + self.hashes = Hashes(self) + self.hmacs = HMACs(self) + + @classmethod + def _ensure_ffi_initialized(cls): + if cls.ffi is not None and cls.lib is not None: + return + + ffi = cffi.FFI() includes = [] functions = [] macros = [] - for name in self._modules: + for name in cls._modules: module_name = "cryptography.hazmat.bindings.openssl." + name __import__(module_name) module = sys.modules[module_name] - self.ffi.cdef(module.TYPES) + ffi.cdef(module.TYPES) macros.append(module.MACROS) functions.append(module.FUNCTIONS) @@ -75,9 +89,9 @@ class Backend(object): # so we can set interdependent types in different files and still # have them all defined before we parse the funcs & macros for func in functions: - self.ffi.cdef(func) + ffi.cdef(func) for macro in macros: - self.ffi.cdef(macro) + ffi.cdef(macro) # We include functions here so that if we got any of their definitions # wrong, the underlying C compiler will explode. In C you are allowed @@ -87,17 +101,15 @@ class Backend(object): # is legal, but the following will fail to compile: # int foo(int); # int foo(short); - self.lib = self.ffi.verify( + lib = ffi.verify( source="\n".join(includes + functions), libraries=["crypto", "ssl"], ) - self.lib.OpenSSL_add_all_algorithms() - self.lib.SSL_load_error_strings() - - self.ciphers = Ciphers(self) - self.hashes = Hashes(self) - self.hmacs = HMACs(self) + cls.ffi = ffi + cls.lib = lib + cls.lib.OpenSSL_add_all_algorithms() + cls.lib.SSL_load_error_strings() def openssl_version_text(self): """ -- cgit v1.2.3 From 3e252c601592245590e015558a4c082f11ea40cd Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Mon, 4 Nov 2013 10:32:21 -0800 Subject: coverage --- tests/hazmat/bindings/test_openssl.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/hazmat/bindings/test_openssl.py b/tests/hazmat/bindings/test_openssl.py index 9ce882e4..d47ae919 100644 --- a/tests/hazmat/bindings/test_openssl.py +++ b/tests/hazmat/bindings/test_openssl.py @@ -13,7 +13,7 @@ import pytest -from cryptography.hazmat.bindings.openssl.backend import backend +from cryptography.hazmat.bindings.openssl.backend import backend, Backend from cryptography.hazmat.primitives.block.ciphers import AES from cryptography.hazmat.primitives.block.modes import CBC @@ -39,3 +39,6 @@ class TestOpenSSL(object): def test_register_duplicate_cipher_adapter(self): with pytest.raises(ValueError): backend.ciphers.register_cipher_adapter(AES, CBC, None) + + def test_instantiates(self): + Backend() -- cgit v1.2.3 From b2fc8ac5a367dbedc8d3bbd57dd16deb67b08d01 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Mon, 4 Nov 2013 14:02:04 -0800 Subject: Rewrite this test to be good --- tests/hazmat/bindings/test_openssl.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/hazmat/bindings/test_openssl.py b/tests/hazmat/bindings/test_openssl.py index d47ae919..fb01c10a 100644 --- a/tests/hazmat/bindings/test_openssl.py +++ b/tests/hazmat/bindings/test_openssl.py @@ -40,5 +40,7 @@ class TestOpenSSL(object): with pytest.raises(ValueError): backend.ciphers.register_cipher_adapter(AES, CBC, None) - def test_instantiates(self): - Backend() + def test_instances_share_ffi(self): + b = Backend() + assert b.ffi is backend.ffi + assert b.lib is backend.lib -- cgit v1.2.3