Skip to main content

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".
  • PtrType objects hold an integer address into _vram and interpret the bytes according to a Type (short, int, long, char, bool, etc.).
  • Type is defined via MainTypes (size in bytes) and ModifiedTypes (signed/unsigned).
  • nullptr is a sentinel object representing a null pointer.
  • Memory allocation is tracked via a global used set 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 _vram directly can corrupt existing pointers. Use with caution.

API Reference

PtrType(type, default=nullptr, _length=1)

  • type: A Type(mat, mot) object.
  • default: Initial value (or nullptr).
  • _length: Number of elements (for array-style access).

Properties:

  • .value → get/set the value at the current pointer address.
  • .ptr → current integer address (or nullptr).
  • [index] → array-style element access.

Methods:

  • .free() → release allocated memory.
  • __iadd__ / __isub__ → pointer arithmetic.

Type(mat, mot=ModifiedTypes.unsigned)

  • mat: A MainTypes enum member.
  • mot: ModifiedTypes.unsigned or 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pointer_x-0.1.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pointer_x-0.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

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

Hashes for pointer_x-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e05380c0b5d95cda213ed0c8a010d1258e027b2e6cb683881fc17df9be6845a3
MD5 e5c1f6a104b441de69810a187209e8cd
BLAKE2b-256 be04ceefde1d33d7b55d33aa971a512d5aa435a63d58dcbf8b93d3800ec56a71

See more details on using hashes here.

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

Hashes for pointer_x-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ef224a266a12f510ba08e3d9ab0c41a676d51b1e0275b0ce2d64d2762da2c59c
MD5 3ed194c24c729476e873ab2ef576d7bf
BLAKE2b-256 60328ba5e26cdb5e0f097981dd3408e59587eaaf984a4695df6e7b9dd239da49

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page