From 310479a098d2e14157f92c16d972072e319bb4fa Mon Sep 17 00:00:00 2001 From: ZeroIntensity Date: Sun, 16 Oct 2022 16:06:41 -0400 Subject: [PATCH] last minute changes --- setup.py | 2 ++ src/pointers/base_pointers.py | 14 ++------------ src/pointers/bindings.py | 2 +- src/pointers/c_pointer.py | 25 ++++++------------------- src/pointers/calloc.py | 5 +---- src/pointers/malloc.py | 5 +---- src/pointers/object_pointer.py | 10 +--------- src/pointers/structure.py | 7 ++++--- 8 files changed, 18 insertions(+), 52 deletions(-) diff --git a/setup.py b/setup.py index 778bd1f..cf4f3c2 100644 --- a/setup.py +++ b/setup.py @@ -5,6 +5,8 @@ if __name__ == "__main__": setup( + name="pointers.py", + version="2.2.0", packages=["pointers"], license="MIT", project_urls={ diff --git a/src/pointers/base_pointers.py b/src/pointers/base_pointers.py index 83f1263..30a0979 100644 --- a/src/pointers/base_pointers.py +++ b/src/pointers/base_pointers.py @@ -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: @@ -152,10 +148,6 @@ def address(self) -> Optional[int]: def __repr__(self) -> str: ... - @abstractmethod - def __rich__(self): - ... - @abstractmethod def _cleanup(self) -> None: ... @@ -217,7 +209,6 @@ class BaseObjectPointer( def __init__( self, address: Optional[int], - typ: Type[T], increment_ref: bool = False, ) -> None: """ @@ -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) @@ -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: diff --git a/src/pointers/bindings.py b/src/pointers/bindings.py index 5ae915c..9cf326e 100644 --- a/src/pointers/bindings.py +++ b/src/pointers/bindings.py @@ -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: diff --git a/src/pointers/c_pointer.py b/src/pointers/c_pointer.py index 5e3b711..c32c16f 100644 --- a/src/pointers/c_pointer.py +++ b/src/pointers/c_pointer.py @@ -43,10 +43,7 @@ def dereference(self) -> Optional[int]: return deref.value def __repr__(self) -> str: - return f"" - - 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 @@ -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"" - - def __rich__(self): - tp = self._as_parameter_ - obj = tp.__name__.replace("c_", "") - return f"" - class TypedCPointer(_TypedPointer[T]): """Class representing a pointer with a known type.""" @@ -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.""" @@ -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"" - - def __rich__(self): - return f"" + return f"CArrayPointer(address={self.address}, size={self.size})" def __getitem__(self, index: int) -> T: array = ~self @@ -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 diff --git a/src/pointers/calloc.py b/src/pointers/calloc.py index b3aa238..96947d9 100644 --- a/src/pointers/calloc.py +++ b/src/pointers/calloc.py @@ -93,10 +93,7 @@ def __sub__(self, amount: int) -> "AllocatedArrayPointer[T]": return self.__add__(-amount) def __repr__(self) -> str: - return f"" - - def __rich__(self) -> str: - return f"" # 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): diff --git a/src/pointers/malloc.py b/src/pointers/malloc.py index fe539ca..5d164e4 100644 --- a/src/pointers/malloc.py +++ b/src/pointers/malloc.py @@ -43,10 +43,7 @@ def address(self, value: int) -> None: self._address = value def __repr__(self) -> str: - return f"" - - def __rich__(self) -> str: - return f"" # noqa + return f"AllocatedPointer(address={self.address}, size={self.size})" def __add__(self, amount: int): return AllocatedPointer( diff --git a/src/pointers/object_pointer.py b/src/pointers/object_pointer.py index 408d454..c7313a1 100644 --- a/src/pointers/object_pointer.py +++ b/src/pointers/object_pointer.py @@ -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"" # noqa - - def __rich__(self): - return f"" # noqa + return f"Pointer(address={self.address})" @handle def move( @@ -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, ) diff --git a/src/pointers/structure.py b/src/pointers/structure.py index f039246..7a91380 100644 --- a/src/pointers/structure.py +++ b/src/pointers/structure.py @@ -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 @@ -190,7 +189,9 @@ def _as_parameter_( return self.ensure() def __repr__(self) -> str: - return f"" + return ( + f"StructPointer(address={self.address}, existing={self._existing})" # noqa + ) def __rich__(self) -> str: return f"<[bold blue]pointer[/] to struct at {str(self)}>"