Skip to content

Commit

Permalink
patch null pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroIntensity committed Sep 17, 2022
1 parent 3483bb5 commit 3da47e2
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 41 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ classifiers = [
dependencies = [
"typing_extensions",
]
version = "2.1.0"
version = "2.1.1"
2 changes: 1 addition & 1 deletion src/pointers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
from .std_structs import DivT, Lconv, LDivT, Tm
from .structure import Struct

__version__ = "2.1.0"
__version__ = "2.1.1"
64 changes: 32 additions & 32 deletions src/pointers/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,36 @@
"make_py",
)

_C_TYPES: Dict[type, Type["ctypes._CData"]] = {
bytes: ctypes.c_char_p,
str: ctypes.c_wchar_p,
int: ctypes.c_int,
float: ctypes.c_float,
bool: ctypes.c_bool,
}

_PY_TYPES: Dict[Type["ctypes._CData"], type] = {
ctypes.c_bool: bool,
ctypes.c_char: bytes,
ctypes.c_wchar: str,
ctypes.c_ubyte: int,
ctypes.c_short: int,
ctypes.c_int: int,
ctypes.c_uint: int,
ctypes.c_long: int,
ctypes.c_ulong: int,
ctypes.c_longlong: int,
ctypes.c_ulonglong: int,
ctypes.c_size_t: int,
ctypes.c_ssize_t: int,
ctypes.c_float: float,
ctypes.c_double: float,
ctypes.c_longdouble: float,
ctypes.c_char_p: bytes,
ctypes.c_wchar_p: str,
ctypes.c_void_p: int,
}


def move_to_mem(
ptr: "ctypes._PointerLike",
Expand Down Expand Up @@ -52,15 +82,7 @@ def map_type(data: Any) -> "ctypes._CData":

def get_mapped(typ: Any):
"""Get the C mapped value of the given type."""
types: Dict[type, Type["ctypes._CData"]] = {
bytes: ctypes.c_char_p,
str: ctypes.c_wchar_p,
int: ctypes.c_int,
float: ctypes.c_float,
bool: ctypes.c_bool,
}

res = types.get(typ)
res = _C_TYPES.get(typ)

if not res:
raise ValueError(f'"{typ.__name__}" is not mappable to a c type')
Expand All @@ -86,30 +108,8 @@ def get_py(
if data.__name__.startswith("LP_"):
return BaseCPointer

types: Dict[Type["ctypes._CData"], type] = {
ctypes.c_bool: bool,
ctypes.c_char: bytes,
ctypes.c_wchar: str,
ctypes.c_ubyte: int,
ctypes.c_short: int,
ctypes.c_int: int,
ctypes.c_uint: int,
ctypes.c_long: int,
ctypes.c_ulong: int,
ctypes.c_longlong: int,
ctypes.c_ulonglong: int,
ctypes.c_size_t: int,
ctypes.c_ssize_t: int,
ctypes.c_float: float,
ctypes.c_double: float,
ctypes.c_longdouble: float,
ctypes.c_char_p: bytes,
ctypes.c_wchar_p: str,
ctypes.c_void_p: int,
}

try:
return types[data]
return _PY_TYPES[data]
except KeyError as e:
raise ValueError(
f"{data} is not a valid ctypes type",
Expand Down
14 changes: 9 additions & 5 deletions src/pointers/base_pointers.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,11 @@ def __init__(
""" # noqa
self._address: Optional[int] = address
self._type: Type[T] = typ
obj = ~self

if increment_ref and address:
add_ref(obj)
add_ref(~self)

self._origin_size = sys.getsizeof(obj)
self._origin_size = sys.getsizeof(~self if address else None)

@property
def type(self):
Expand Down Expand Up @@ -285,12 +284,17 @@ def assign(
"can only point to object pointer",
)

if new.type is not self.type:
if (new.type is not self.type) and self.address:
raise TypeError(
f"object at new address must be the same type (pointer looks at {self.type.__name__}, target is {new.type.__name__})", # noqa
)

remove_ref(~self)
if not self.address:
self._type = new.type

with suppress(NullPointerError):
remove_ref(~self)

self._address = new.address
add_ref(~self)

Expand Down
5 changes: 3 additions & 2 deletions src/pointers/object_pointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ def move(

@classmethod
def make_from(cls, obj: Nullable[T]) -> "Pointer[T]":
is_null = obj is NULL
return Pointer(
id(obj) if obj is not NULL else None,
id(obj) if not is_null else None,
type(obj), # type: ignore
True,
not is_null,
)


Expand Down
8 changes: 8 additions & 0 deletions tests/test_pointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ def _():
with raises(NullPointerError):
print(~to_ptr(NULL))

ptr2 = to_ptr(NULL)
ptr2 >>= 1

with raises(TypeError):
ptr2 >>= ""

ptr2 >>= NULL


@test("operator magic")
def _():
Expand Down

0 comments on commit 3da47e2

Please sign in to comment.