File tree Expand file tree Collapse file tree 2 files changed +25
-5
lines changed Expand file tree Collapse file tree 2 files changed +25
-5
lines changed Original file line number Diff line number Diff line change @@ -209,6 +209,9 @@ def __rich__(self):
209
209
return (
210
210
f"<[green]void[/green] pointer to [cyan]{ hex (self .address )} [/cyan]>" # noqa
211
211
)
212
+
213
+ def __del__ (self ):
214
+ pass
212
215
213
216
214
217
class TypedCPointer (_BaseCPointer [T ], Generic [T ]):
@@ -219,11 +222,13 @@ def __init__(
219
222
address : int ,
220
223
data_type : Type [T ],
221
224
size : int ,
222
- alternate_method : bool = True
225
+ alternate_method : bool = True ,
226
+ decref : bool = True
223
227
):
224
228
self ._alt = alternate_method
225
229
super ().__init__ (address , size )
226
230
self ._type = data_type
231
+ self ._decref = decref
227
232
228
233
@property
229
234
def _as_parameter_ (self ) -> "ctypes.pointer[ctypes._CData]" :
@@ -255,7 +260,7 @@ def __rich__(self):
255
260
return f"<[green]typed c[/green] pointer to [cyan]{ hex (self .address )} [/cyan]>" # noqa
256
261
257
262
def __del__ (self ):
258
- if self .type is not str :
263
+ if ( self .type is not str ) and ( self . _decref ) :
259
264
super ().__del__ ()
260
265
remove_ref (~ self )
261
266
@@ -264,7 +269,12 @@ def __del__(self):
264
269
265
270
def cast (ptr : VoidPointer , data_type : Type [T ]) -> TypedCPointer [T ]:
266
271
"""Cast a void pointer to a typed pointer."""
267
- return TypedCPointer (ptr .address , data_type , ptr .size )
272
+ return TypedCPointer (
273
+ ptr .address ,
274
+ data_type ,
275
+ ptr .size ,
276
+ decref = False
277
+ )
268
278
269
279
270
280
def to_c_ptr (data : T ) -> TypedCPointer [T ]:
Original file line number Diff line number Diff line change 1
1
from pointers import (
2
2
to_c_ptr ,
3
- TypedCPointer ,
4
3
StructPointer ,
5
4
localeconv ,
6
- frexp ,
7
5
div ,
8
6
strlen ,
9
7
c_malloc ,
@@ -24,3 +22,15 @@ def test_bindings():
24
22
r = strlen ("test" )
25
23
assert r == 4
26
24
assert type (r ) is int
25
+
26
+ def test_to_c_ptr ():
27
+ a = to_c_ptr ('test' )
28
+ assert a .type is str
29
+ assert ~ a == 'test'
30
+
31
+ def test_strings ():
32
+ mem = c_malloc (2 )
33
+ sprintf (mem , "%s" , "a" ) # testing format strings
34
+ ptr = cast (mem , bytes )
35
+ assert ~ ptr == b"a"
36
+ c_free (mem )
You can’t perform that action at this time.
0 commit comments