Skip to content

Commit

Permalink
last minute changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroIntensity committed Oct 16, 2022
1 parent 1189bac commit 310479a
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 52 deletions.
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

if __name__ == "__main__":
setup(
name="pointers.py",
version="2.2.0",
packages=["pointers"],
license="MIT",
project_urls={
Expand Down
14 changes: 2 additions & 12 deletions src/pointers/base_pointers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,9 @@ def address(self) -> Optional[int]:
def __repr__(self) -> str:
...

@abstractmethod
def __rich__(self):
...

@final
def __str__(self) -> str:
return hex(self.address or 0)
return f"{type(self).__name__}({hex(self.address or 0)})"

@abstractmethod
def _cleanup(self) -> None:
Expand Down Expand Up @@ -152,10 +148,6 @@ def address(self) -> Optional[int]:
def __repr__(self) -> str:
...

@abstractmethod
def __rich__(self):
...

@abstractmethod
def _cleanup(self) -> None:
...
Expand Down Expand Up @@ -217,7 +209,6 @@ class BaseObjectPointer(
def __init__(
self,
address: Optional[int],
typ: Type[T],
increment_ref: bool = False,
) -> None:
"""
Expand All @@ -227,7 +218,6 @@ def __init__(
increment_ref: Should the reference count on the target object get incremented.
""" # noqa
self._address: Optional[int] = address
self._type: Type[T] = typ

if increment_ref and address:
add_ref(~self)
Expand All @@ -242,7 +232,7 @@ def type(self) -> Type[T]:
DeprecationWarning,
)

return self._type
return type(~self)

@handle
def set_attr(self, key: str, value: Any) -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/pointers/bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def _decode_type(
alt=True,
)
if not issubclass(type(res.contents), ctypes.Structure)
else StructPointer(id(struct), type(_not_null(struct)), struct)
else StructPointer(id(struct), struct)
)
# type safety gets mad if i dont use elif here
elif current is ctypes.c_void_p:
Expand Down
25 changes: 6 additions & 19 deletions src/pointers/c_pointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ def dereference(self) -> Optional[int]:
return deref.value

def __repr__(self) -> str:
return f"<void pointer to {self}>"

def __rich__(self):
return f"<[bold green]void[/] pointer to [cyan]{self}[/cyan]>"
return f"VoidPointer(address={self.address}, size={self.size})"

def _cleanup(self) -> None:
pass
Expand Down Expand Up @@ -105,16 +102,6 @@ def decref(self) -> bool:
def type(self):
return self._type

def __repr__(self) -> str:
tp = self._as_parameter_
obj = tp.__name__.replace("c_", "")
return f"<pointer to {obj} at {self}>"

def __rich__(self):
tp = self._as_parameter_
obj = tp.__name__.replace("c_", "")
return f"<pointer to [bold green]{obj}[/] at [cyan]{self}[/]>"


class TypedCPointer(_TypedPointer[T]):
"""Class representing a pointer with a known type."""
Expand Down Expand Up @@ -157,6 +144,9 @@ def __iter__(self) -> Iterator[T]:
"""Dereference the pointer."""
return iter({self.dereference()})

def __repr__(self) -> str:
return f"TypedCPointer(address={self.address}, size={self.size})"


class CArrayPointer(_CDeref[List[T]], Typed[Type[T]], BaseCPointer[List[T]]):
"""Class representing a pointer to a C array."""
Expand Down Expand Up @@ -196,10 +186,7 @@ def dereference(self) -> List[T]:
return [array[i] for i in range(self._length)] # type: ignore

def __repr__(self) -> str:
return f"<pointer to array at {str(self)}>"

def __rich__(self):
return f"<pointer to [bold green]array[/] [cyan]{str(self)}[/]>"
return f"CArrayPointer(address={self.address}, size={self.size})"

def __getitem__(self, index: int) -> T:
array = ~self
Expand Down Expand Up @@ -241,7 +228,7 @@ def to_struct_ptr(struct: A) -> "StructPointer[A]":
"""Convert a struct to a pointer."""
from .structure import StructPointer

return StructPointer(id(struct), type(struct))
return StructPointer(id(struct))


@handle
Expand Down
5 changes: 1 addition & 4 deletions src/pointers/calloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ def __sub__(self, amount: int) -> "AllocatedArrayPointer[T]":
return self.__add__(-amount)

def __repr__(self) -> str:
return f"<pointer to allocated chunk at {self}>"

def __rich__(self) -> str:
return f"<pointer to [bold green]allocated chunk[/] at [cyan]{self}[/]>" # noqa
return f"AllocatedArrayPointer(address={self.address}, current_index={self.current_index})" # noqa

def __iter__(self) -> Iterator["AllocatedArrayPointer[T]"]:
for i in range(self.current_index, self.chunks):
Expand Down
5 changes: 1 addition & 4 deletions src/pointers/malloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ def address(self, value: int) -> None:
self._address = value

def __repr__(self) -> str:
return f"<pointer to {self.size} bytes of memory at {self}>"

def __rich__(self) -> str:
return f"<pointer to [bold green]{self.size} bytes[/] of memory at [cyan]{self}[/]>" # noqa
return f"AllocatedPointer(address={self.address}, size={self.size})"

def __add__(self, amount: int):
return AllocatedPointer(
Expand Down
10 changes: 1 addition & 9 deletions src/pointers/object_pointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,8 @@
class Pointer(BaseObjectPointer[T]):
"""Pointer to a `PyObject`"""

def _get_tp_name(self) -> str:
return type(~self).__name__ if self.address else "NULL"

def __repr__(self) -> str:

return f"<pointer to {self._get_tp_name()} object at {self}>" # noqa

def __rich__(self):
return f"<pointer to [bold green]{self._get_tp_name()}[/] object at [cyan]{self}[/cyan]>" # noqa
return f"Pointer(address={self.address})"

@handle
def move(
Expand Down Expand Up @@ -69,7 +62,6 @@ def make_from(cls, obj: Nullable[T]) -> "Pointer[T]":
is_null = obj is NULL
return Pointer(
id(obj) if not is_null else None,
type(obj), # type: ignore
not is_null,
)

Expand Down
7 changes: 4 additions & 3 deletions src/pointers/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,10 @@ class StructPointer(Pointer[T]):
def __init__(
self,
address: int,
data_type: Type[T],
existing: Optional["Struct"] = None,
):
self._existing = existing
super().__init__(address, data_type, True)
super().__init__(address, True)

@property # type: ignore
@handle
Expand All @@ -190,7 +189,9 @@ def _as_parameter_(
return self.ensure()

def __repr__(self) -> str:
return f"<pointer to struct at {str(self)}>"
return (
f"StructPointer(address={self.address}, existing={self._existing})" # noqa
)

def __rich__(self) -> str:
return f"<[bold blue]pointer[/] to struct at {str(self)}>"
Expand Down

0 comments on commit 310479a

Please sign in to comment.