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))
A segmentation fault will occur if the address does not exist, so make sure the pointer is valid.
If you would like to automatically decay values to a pointer, use the pointers.decay
decorator, like this:
@decay
def some_function(ptr: Pointer[str], b: str): # converts "ptr" to a pointer since its hinted as Pointer[str]
print(ptr.dereference(), b) # a b
some_function("a", "b") # converts "a" to a pointer, and leaves b as it is
Make sure you annotate the argument with Pointer
or else decay won't convert it.
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.2.tar.gz
(3.7 kB
view hashes)
Built Distribution
Close
Hashes for pointers.py-1.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fcb439b76d20bf594d189218f55031fe9259a9668cbc5480d63cfa5e1fc6fc7f |
|
MD5 | bab897a6bec8c36b3f74728a5dc48848 |
|
BLAKE2b-256 | 6712beaee0109198c2f88e13e9295ccd3277989f671cf01c2be051dd8430e57d |