Bringing the hell of pointers to Python.
Project description
pointers.py
Bringing the hell of pointers to Python
Why would you ever need this
Example
from pointers import Pointer, to_ptr
class test_class:
pass
def some_function(ptr: Pointer[test_class]):
print(repr(ptr)) # <pointer to test_class object at [address]>"
some_function(to_ptr(test_class()))
Installation
Linux/macOS
python3 -m pip install -U pointers.py
Windows
py -3 -m pip install -U pointers.py
Usage
To convert something to a pointer, use the pointers.to_ptr()
function. Then, when annotating function types use the pointers.Pointer
class, like so:
from pointers import Pointer, to_ptr
class test_class:
pass
def some_function(ptr: Pointer[test_class]): # can be any data type (str, tuple, etc)
...
some_function(to_ptr(test_class())) # converts the instance to a pointer object
To dereference a pointer, use the Pointer.dereference()
function:
def some_function(ptr: Pointer[test_class]):
my_instance: test_class = ptr.dereference() # dereferences the pointer
instance = test_class()
some_function(to_ptr(instance))
If the address no longer exists, pointers.DereferenceError
is raised.
You can add safe = False
to the Pointer.dereference()
function to use C when dereferencing. A segmentation fault will occur if the address does not exist, so only use if you are sure the pointer is valid.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pointers.py-1.0.0.tar.gz
(3.4 kB
view hashes)
Built Distributions
pointers.py-1.0.0-py3.8.egg
(4.0 kB
view hashes)
Close
Hashes for pointers.py-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b971da496858f9d94f7aa72799b8e1316eb006831c1de9aefe98957047086a18 |
|
MD5 | 053a768ba87fb54a69c929e5e82e289e |
|
BLAKE2b-256 | 63abcf2c6f65bae199ed1dfd4b3c477755ac5d232330b36f61a346e0cfb38230 |