Skip to content

Commit

Permalink
Add check that object is done initialized before calling bound functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mortalisk committed Sep 19, 2022
1 parent 2e394c7 commit d972469
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cwrap/basecclass.py
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions cwrap/prototype.py
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
@@ -1 +1,2 @@
six
pytest
29 changes: 29 additions & 0 deletions 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()

0 comments on commit d972469

Please sign in to comment.