Skip to content

Commit 310479a

Browse files
committed
last minute changes
1 parent 1189bac commit 310479a

File tree

8 files changed

+18
-52
lines changed

8 files changed

+18
-52
lines changed

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
if __name__ == "__main__":
77
setup(
8+
name="pointers.py",
9+
version="2.2.0",
810
packages=["pointers"],
911
license="MIT",
1012
project_urls={

src/pointers/base_pointers.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,9 @@ def address(self) -> Optional[int]:
4545
def __repr__(self) -> str:
4646
...
4747

48-
@abstractmethod
49-
def __rich__(self):
50-
...
51-
5248
@final
5349
def __str__(self) -> str:
54-
return hex(self.address or 0)
50+
return f"{type(self).__name__}({hex(self.address or 0)})"
5551

5652
@abstractmethod
5753
def _cleanup(self) -> None:
@@ -152,10 +148,6 @@ def address(self) -> Optional[int]:
152148
def __repr__(self) -> str:
153149
...
154150

155-
@abstractmethod
156-
def __rich__(self):
157-
...
158-
159151
@abstractmethod
160152
def _cleanup(self) -> None:
161153
...
@@ -217,7 +209,6 @@ class BaseObjectPointer(
217209
def __init__(
218210
self,
219211
address: Optional[int],
220-
typ: Type[T],
221212
increment_ref: bool = False,
222213
) -> None:
223214
"""
@@ -227,7 +218,6 @@ def __init__(
227218
increment_ref: Should the reference count on the target object get incremented.
228219
""" # noqa
229220
self._address: Optional[int] = address
230-
self._type: Type[T] = typ
231221

232222
if increment_ref and address:
233223
add_ref(~self)
@@ -242,7 +232,7 @@ def type(self) -> Type[T]:
242232
DeprecationWarning,
243233
)
244234

245-
return self._type
235+
return type(~self)
246236

247237
@handle
248238
def set_attr(self, key: str, value: Any) -> None:

src/pointers/bindings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def _decode_type(
205205
alt=True,
206206
)
207207
if not issubclass(type(res.contents), ctypes.Structure)
208-
else StructPointer(id(struct), type(_not_null(struct)), struct)
208+
else StructPointer(id(struct), struct)
209209
)
210210
# type safety gets mad if i dont use elif here
211211
elif current is ctypes.c_void_p:

src/pointers/c_pointer.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ def dereference(self) -> Optional[int]:
4343
return deref.value
4444

4545
def __repr__(self) -> str:
46-
return f"<void pointer to {self}>"
47-
48-
def __rich__(self):
49-
return f"<[bold green]void[/] pointer to [cyan]{self}[/cyan]>"
46+
return f"VoidPointer(address={self.address}, size={self.size})"
5047

5148
def _cleanup(self) -> None:
5249
pass
@@ -105,16 +102,6 @@ def decref(self) -> bool:
105102
def type(self):
106103
return self._type
107104

108-
def __repr__(self) -> str:
109-
tp = self._as_parameter_
110-
obj = tp.__name__.replace("c_", "")
111-
return f"<pointer to {obj} at {self}>"
112-
113-
def __rich__(self):
114-
tp = self._as_parameter_
115-
obj = tp.__name__.replace("c_", "")
116-
return f"<pointer to [bold green]{obj}[/] at [cyan]{self}[/]>"
117-
118105

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

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

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

198188
def __repr__(self) -> str:
199-
return f"<pointer to array at {str(self)}>"
200-
201-
def __rich__(self):
202-
return f"<pointer to [bold green]array[/] [cyan]{str(self)}[/]>"
189+
return f"CArrayPointer(address={self.address}, size={self.size})"
203190

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

244-
return StructPointer(id(struct), type(struct))
231+
return StructPointer(id(struct))
245232

246233

247234
@handle

src/pointers/calloc.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,7 @@ def __sub__(self, amount: int) -> "AllocatedArrayPointer[T]":
9393
return self.__add__(-amount)
9494

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

10198
def __iter__(self) -> Iterator["AllocatedArrayPointer[T]"]:
10299
for i in range(self.current_index, self.chunks):

src/pointers/malloc.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ def address(self, value: int) -> None:
4343
self._address = value
4444

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

5148
def __add__(self, amount: int):
5249
return AllocatedPointer(

src/pointers/object_pointer.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,8 @@
1414
class Pointer(BaseObjectPointer[T]):
1515
"""Pointer to a `PyObject`"""
1616

17-
def _get_tp_name(self) -> str:
18-
return type(~self).__name__ if self.address else "NULL"
19-
2017
def __repr__(self) -> str:
21-
22-
return f"<pointer to {self._get_tp_name()} object at {self}>" # noqa
23-
24-
def __rich__(self):
25-
return f"<pointer to [bold green]{self._get_tp_name()}[/] object at [cyan]{self}[/cyan]>" # noqa
18+
return f"Pointer(address={self.address})"
2619

2720
@handle
2821
def move(
@@ -69,7 +62,6 @@ def make_from(cls, obj: Nullable[T]) -> "Pointer[T]":
6962
is_null = obj is NULL
7063
return Pointer(
7164
id(obj) if not is_null else None,
72-
type(obj), # type: ignore
7365
not is_null,
7466
)
7567

src/pointers/structure.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,10 @@ class StructPointer(Pointer[T]):
171171
def __init__(
172172
self,
173173
address: int,
174-
data_type: Type[T],
175174
existing: Optional["Struct"] = None,
176175
):
177176
self._existing = existing
178-
super().__init__(address, data_type, True)
177+
super().__init__(address, True)
179178

180179
@property # type: ignore
181180
@handle
@@ -190,7 +189,9 @@ def _as_parameter_(
190189
return self.ensure()
191190

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

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

0 commit comments

Comments
 (0)