Fix silent NameError when CFFI bindings are missing, adds _FFIPlaceholder that raises ImportError with instructions

pull/64/head
Chris Conlon 2026-03-02 14:19:59 -07:00
parent 05433e92b6
commit 8b3adf3023
3 changed files with 24 additions and 7 deletions

View File

@ -38,8 +38,10 @@ from wolfssl.__about__ import * # noqa: F401, F403
try:
from wolfssl._ffi import ffi as _ffi
from wolfssl._ffi import lib as _lib
except ImportError:
pass
except ImportError as e:
from wolfssl.utils import _FFIPlaceholder
_ffi = _FFIPlaceholder(e)
_lib = _FFIPlaceholder(e)
from wolfssl.utils import t2b
@ -169,7 +171,7 @@ class SSLContext(object):
self.verify_mode = CERT_NONE
def __del__(self):
if getattr(self, 'native_object', _ffi.NULL) != _ffi.NULL:
if getattr(self, 'native_object', None) is not None and self.native_object != _ffi.NULL:
_lib.wolfSSL_CTX_free(self.native_object)
@property
@ -474,7 +476,7 @@ class SSLSocket(object):
self._release_native_object()
def _release_native_object(self):
if getattr(self, 'native_object', _ffi.NULL) != _ffi.NULL:
if getattr(self, 'native_object', None) is not None and self.native_object != _ffi.NULL:
_lib.wolfSSL_free(self.native_object)
self.native_object = _ffi.NULL

View File

@ -25,8 +25,10 @@
try:
from wolfssl._ffi import lib as _lib
from wolfssl._ffi import ffi as _ffi
except ImportError:
pass
except ImportError as e:
from wolfssl.utils import _FFIPlaceholder
_ffi = _FFIPlaceholder(e)
_lib = _FFIPlaceholder(e)
PROTOCOL_SSLv23 = 1
@ -111,5 +113,5 @@ class WolfSSLMethod(object): # pylint: disable=too-few-public-methods
raise MemoryError("Cannot allocate method object")
def __del__(self):
if getattr(self, 'native_object', _ffi.NULL) != _ffi.NULL:
if getattr(self, 'native_object', None) is not None and self.native_object != _ffi.NULL:
_native_free(self.native_object, _DYNAMIC_TYPE_METHOD)

View File

@ -30,6 +30,19 @@ _TEXT_TYPE = str if _PY3 else unicode # noqa: F821
_BINARY_TYPE = bytes if _PY3 else str
class _FFIPlaceholder:
def __init__(self, cause=None):
object.__setattr__(self, '_cause', cause)
def __getattr__(self, name):
raise ImportError(
"wolfssl._ffi is not available. The CFFI bindings have not been "
"compiled. If you installed wolfssl via pip, the build may have "
"failed silently. Try reinstalling with: "
"pip install --no-binary wolfssl wolfssl"
) from self._cause
def t2b(string):
"""
Converts text to binary.