Skip to content

Commit d764420

Browse files
committed
Fix silent NameError when CFFI bindings are missing, adds _FFIPlaceholder that raises ImportError with instructions
1 parent 05433e9 commit d764420

3 files changed

Lines changed: 19 additions & 5 deletions

File tree

wolfssl/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
from wolfssl._ffi import ffi as _ffi
4040
from wolfssl._ffi import lib as _lib
4141
except ImportError:
42-
pass
42+
from wolfssl.utils import _FFIPlaceholder
43+
_ffi = _FFIPlaceholder()
44+
_lib = _FFIPlaceholder()
4345

4446
from wolfssl.utils import t2b
4547

@@ -169,7 +171,7 @@ def __init__(self, protocol, server_side=None):
169171
self.verify_mode = CERT_NONE
170172

171173
def __del__(self):
172-
if getattr(self, 'native_object', _ffi.NULL) != _ffi.NULL:
174+
if getattr(self, 'native_object', None) is not None and self.native_object != _ffi.NULL:
173175
_lib.wolfSSL_CTX_free(self.native_object)
174176

175177
@property
@@ -474,7 +476,7 @@ def __del__(self):
474476
self._release_native_object()
475477

476478
def _release_native_object(self):
477-
if getattr(self, 'native_object', _ffi.NULL) != _ffi.NULL:
479+
if getattr(self, 'native_object', None) is not None and self.native_object != _ffi.NULL:
478480
_lib.wolfSSL_free(self.native_object)
479481
self.native_object = _ffi.NULL
480482

wolfssl/_methods.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
from wolfssl._ffi import lib as _lib
2727
from wolfssl._ffi import ffi as _ffi
2828
except ImportError:
29-
pass
29+
from wolfssl.utils import _FFIPlaceholder
30+
_ffi = _FFIPlaceholder()
31+
_lib = _FFIPlaceholder()
3032

3133

3234
PROTOCOL_SSLv23 = 1
@@ -111,5 +113,5 @@ def __init__(self, protocol, server_side):
111113
raise MemoryError("Cannot allocate method object")
112114

113115
def __del__(self):
114-
if getattr(self, 'native_object', _ffi.NULL) != _ffi.NULL:
116+
if getattr(self, 'native_object', None) is not None and self.native_object != _ffi.NULL:
115117
_native_free(self.native_object, _DYNAMIC_TYPE_METHOD)

wolfssl/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@
3030
_BINARY_TYPE = bytes if _PY3 else str
3131

3232

33+
class _FFIPlaceholder:
34+
def __getattr__(self, name):
35+
raise ImportError(
36+
"wolfssl._ffi is not available. The CFFI bindings have not been "
37+
"compiled. If you installed wolfssl via pip, the build may have "
38+
"failed silently. Try reinstalling with: "
39+
"pip install --no-binary wolfssl wolfssl"
40+
)
41+
42+
3343
def t2b(string):
3444
"""
3545
Converts text to binary.

0 commit comments

Comments
 (0)