diff --git a/cwrap/basecclass.py b/cwrap/basecclass.py index 306a837..d72a704 100644 --- a/cwrap/basecclass.py +++ b/cwrap/basecclass.py @@ -45,6 +45,9 @@ def __new__(cls, *more, **kwargs): return obj + def is_initialized(self): + return self._address() is not None + def _address(self): return self.__c_pointer diff --git a/cwrap/prototype.py b/cwrap/prototype.py index 2637faa..f989048 100644 --- a/cwrap/prototype.py +++ b/cwrap/prototype.py @@ -214,6 +214,9 @@ def __call__(self, *args): raise NotImplementedError("Function:%s has not been properly resolved" % self.__name__) else: raise PrototypeError("Prototype has not been properly resolved") + if self._bind and not args[0].is_initialized(): + raise ValueError("Called bound function with uninitialized object of type " + f"{type(args[0]).__name__}") try: return self._func(*args) except ctypes.ArgumentError as err: diff --git a/requirements.txt b/requirements.txt index ffe2fce..fa56aed 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ six +pytest diff --git a/tests/test_prototype.py b/tests/test_prototype.py new file mode 100644 index 0000000..2bb2d1c --- /dev/null +++ b/tests/test_prototype.py @@ -0,0 +1,29 @@ +from cwrap import BaseCClass, Prototype, load +import os +import pytest + + +class LibCPrototype(Prototype): + lib = load("msvcrt" if os.name == "nt" else None) + + def __init__(self, prototype, bind): + super(LibCPrototype, self).__init__( + LibCPrototype.lib, + prototype, + bind=bind) + + +class BadInitialization(BaseCClass): + TYPE_NAME = "bad_initialization" + _abs = LibCPrototype("int abs(int)", bind=True) + + def __init__(self): + self._abs(0) # called before initialization, should raise + + def free(self): + pass + + +def test_that_unitialized_is_raised(): + with pytest.raises(ValueError, match="uninitialized"): + _ = BadInitialization()