Pure Python C-style pointers with pluggable memory backend.
Project description
Pointer
A Python package that simulates C-style pointers — because Python said it can't have pointers. I took that personally.
Overview
pointer provides a memory simulation layer on top of Python, implementing raw pointer arithmetic, typed memory allocation, and manual memory management — all inside a single bytearray acting as virtual RAM.
How It Works
- A global
_vram(bytearray) serves as the "physical memory". PtrTypeobjects hold an integer address into_vramand interpret the bytes according to aType(short, int, long, char, bool, etc.).Typeis defined viaMainTypes(size in bytes) andModifiedTypes(signed/unsigned).nullptris a sentinel object representing a null pointer.- Memory allocation is tracked via a global
usedset to prevent overlapping.
Core Components
| Class / Enum | Purpose |
|---|---|
MainTypes |
Defines base C types with byte sizes (short=2, int=4, long=8, etc.) |
ModifiedTypes |
Modifiers like unsigned |
PtrType |
A typed pointer that reads/writes from _vram |
nullptr |
Null pointer sentinel |
_vram |
The actual bytearray acting as raw memory |
Usage
Basic Pointer
import pointer as ptr
# Create an integer pointer (default nullptr)
p = ptr.PtrType(ptr.Type(ptr.mat.int))
# Allocate memory (assign a value -> triggers allocation)
p.value = 42
print(p.value) # 42
print(p.ptr) # address in _vram
# Free memory
p.free()
Pointer Arithmetic
p = ptr.PtrType(ptr.Type(ptr.mat.int))
p.value = 100
# Move pointer by N elements (in bytes = N * type.size)
p += 1 # move forward by 4 bytes
p -= 1 # move back by 4 bytes
Array / Multiple Elements
# Create a pointer pointing to 5 consecutive ints
p = ptr.PtrType(ptr.Type(ptr.mat.int), _length=5)
for i in range(5):
p[i] = i * 10
print(p[2]) # 20
Direct Memory Manipulation via _vram
Since _vram is a public bytearray, you can manipulate it directly using ctypes or raw byte operations:
import ctypes
import pointer
# Create a writable buffer that shares memory with _vram
pointer._vram = ctypes.create_string_buffer(1 * 1024 ** 2)
# Now any PtrType reads/writes to this ctypes buffer
p = pointer.PtrType(pointer.Type(pointer.mat.int))
p.value = 999
print(pointer._vram[p.ptr:p.ptr+4]) # raw bytes
Warning: Modifying
_vramdirectly can corrupt existing pointers. Use with caution.
API Reference
PtrType(type, default=nullptr, _length=1)
type: AType(mat, mot)object.default: Initial value (ornullptr)._length: Number of elements (for array-style access).
Properties:
.value→ get/set the value at the current pointer address..ptr→ current integer address (ornullptr).[index]→ array-style element access.
Methods:
.free()→ release allocated memory.__iadd__/__isub__→ pointer arithmetic.
Type(mat, mot=ModifiedTypes.unsigned)
mat: AMainTypesenum member.mot:ModifiedTypes.unsignedor default.
Limitations
- Single global
_vram— no per-process isolation. - No garbage collection; manual
free()required. - Not thread-safe.
- Pointer arithmetic is element-based, not byte-based (unlike real C).
License
MIT — because even fake pointers deserve freedom.
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
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 pointer_x-0.1.0.tar.gz.
File metadata
- Download URL: pointer_x-0.1.0.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e05380c0b5d95cda213ed0c8a010d1258e027b2e6cb683881fc17df9be6845a3
|
|
| MD5 |
e5c1f6a104b441de69810a187209e8cd
|
|
| BLAKE2b-256 |
be04ceefde1d33d7b55d33aa971a512d5aa435a63d58dcbf8b93d3800ec56a71
|
File details
Details for the file pointer_x-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pointer_x-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ef224a266a12f510ba08e3d9ab0c41a676d51b1e0275b0ce2d64d2762da2c59c
|
|
| MD5 |
3ed194c24c729476e873ab2ef576d7bf
|
|
| BLAKE2b-256 |
60328ba5e26cdb5e0f097981dd3408e59587eaaf984a4695df6e7b9dd239da49
|