Solving one of Python's biggest problems. Reference types (output variables) are not implemented.
Project description
Python Reference Variables refvars
This repository contains but two simple but extremely useful Python class, new and Reference.
ALSO: C Pointers in Python.
This holy grail of simple python hacks, refvars provides a convenient way to work with references to objects.
It allows you to set and get the value of the reference using simple get and set methods.
Usage
To use the refvars class, simply import it into your Python script:
import refvars
Then use new to create a reference to an object:
# Create a reference.
ref = refvars.new[int](0).get_ref()
Note: that new will run a variety of runtime checks to ensure that the reference is being used correctly.
- The way these checks work is by very basic checking of source code.
- This can lead to some problems when trying to open your source code file with
inspectmodule.
To fix this:
refvars.disable_runtime_usage_checks()
Now, you can use the Reference to get and set the value of the reference:
# To ensure that the initial value does actually change.
print(ref.get()) # 0
# If we want a function to modify the reference, we can pass the reference to the function.
def modify_reference(ref:"refvars.Reference[int]"):
ref.set(1)
modify_reference(ref)
print(ref.get()) # 1
Installation
To install the refvars, use pip:
pip install refvars
C Pointers in Python
Yes, you heard it right. You can use refvars to access memory created by C code in Python.
from refvars import alloc
The alloc is much the same to the new class. But instead we use safe_access(< your cb >) to safely access the memory.
from refvars import alloc, Pointer
def callback(ptr:"Pointer") -> None:
print(f"Address: {ptr.address}")
# We even have basic error handling to stop you from writing more than 64 bytes.
more_than_64 = b"X" * 65
ptr.write(more_than_64) # This will raise an error.
# This allocates 64 bytes of memory.
# Your callback will be called with a `Pointer` object as the only argument.
# On function return, the memory will be freed.
# Meaning that we are (hopefully) safe from memory leaks and cyber attacks.
alloc(64).safe_access(callback)
A working version you can use is:
from refvars import alloc, Pointer
def callback(ptr:"Pointer") -> None:
print(f"Address: {ptr.address}")
data = b"Hello, World!"
ptr.write(data)
print(ptr.read(len(data))) # b"Hello, World!"
alloc(1024).safe_access(callback)
Confirmed versions of Python
- 3.11 - Works perfectly on the old version (before c pointer update).
- 3.12 - Works perfectly.
License
Covered under the GNU General Public License v2.0.
V0.7 released on 29th/5/2024
Redesign and added accessing c pointers from Python.
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 Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file refvars-0.7-py3-none-any.whl.
File metadata
- Download URL: refvars-0.7-py3-none-any.whl
- Upload date:
- Size: 88.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd2161580546d7e2b5e5b22a477b1931b6e4768bd4ba226157cb1132d707d096
|
|
| MD5 |
676d319e673bca3bc4be2d335b214e5b
|
|
| BLAKE2b-256 |
291af0355fd3eb80898db29f5be6aac8c4b0defcad46ab3afded993efcfb3871
|