Skip to content

Commit 8b3adf3

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

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

wolfssl/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@
3838
try:
3939
from wolfssl._ffi import ffi as _ffi
4040
from wolfssl._ffi import lib as _lib
41-
except ImportError:
42-
pass
41+
except ImportError as e:
42+
from wolfssl.utils import _FFIPlaceholder
43+
_ffi = _FFIPlaceholder(e)
44+
_lib = _FFIPlaceholder(e)
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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
try:
2626
from wolfssl._ffi import lib as _lib
2727
from wolfssl._ffi import ffi as _ffi
28-
except ImportError:
29-
pass
28+
except ImportError as e:
29+
from wolfssl.utils import _FFIPlaceholder
30+
_ffi = _FFIPlaceholder(e)
31+
_lib = _FFIPlaceholder(e)
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@
3030
_BINARY_TYPE = bytes if _PY3 else str
3131

3232

33+
class _FFIPlaceholder:
34+
def __init__(self, cause=None):
35+
object.__setattr__(self, '_cause', cause)
36+
37+
def __getattr__(self, name):
38+
raise ImportError(
39+
"wolfssl._ffi is not available. The CFFI bindings have not been "
40+
"compiled. If you installed wolfssl via pip, the build may have "
41+
"failed silently. Try reinstalling with: "
42+
"pip install --no-binary wolfssl wolfssl"
43+
) from self._cause
44+
45+
3346
def t2b(string):
3447
"""
3548
Converts text to binary.

0 commit comments

Comments
 (0)