Skip to main content

Low-level C concepts brought to Python

Project description

py-like-c

Low-level C concepts brought to Python. py-like-c gives you pointers, references, pipes, forks, and signals — all with a clean Pythonic API inspired by C and Unix.

Installation

pip install pylikec

Modules

Pointer

from pylikec import Pointer, NullPointer, DoublePointer

p = Pointer(42)
p.deref()       # → 42  (*p in C)
p.set(100)      # (*p = 100)
p.address       # → '0x...' (&p in C)

# Pointer arithmetic
arr = [10, 20, 30]
p = Pointer(arr)
(p + 1).deref() # → 20

# Null pointer
p = NullPointer()
p.deref()       # raises NullPointerException

# Double pointer
pp = DoublePointer(Pointer(42))
pp.deref_all()  # → 42  (**pp in C)

Ref

from pylikec import Ref, WeakRef

x = [10]
r = Ref(x, 0)
r.value = 99
print(x[0])     # → 99, original changed!

# Weak reference (dangling pointer simulation)
wr = WeakRef(obj)
wr.is_alive()   # → True / False
wr.value        # raises InvalidReferenceError if dead

Pipe

from pylikec import Pipe, IPCPipe
from pylikec.pipe import FuncPipe, CmdPipe

# In-process pipe
p = Pipe()
p[1].write("hello")   # fd[1] write end
p[0].read()           # fd[0] read end → "hello"

# Cross-process pipe (use with Fork)
p = IPCPipe()         # must create BEFORE fork!

# Functional pipe
result = FuncPipe("hello world") | str.upper | str.split
result.value    # → ['HELLO', 'WORLD']

# Command pipe
cmd = CmdPipe("ls -la")
cmd.read()      # → terminal output

Fork

from pylikec import Fork

f = Fork()
pid = f.fork()

if f.is_child():
    print("child!")
    f.exit(0)
else:
    print(f"parent, child pid={pid}")
    f.wait()

# Run function in child
f = Fork()
f.run(my_function, arg1, arg2)

Signal

from pylikec import Signal, SignalTimeout, SignalHandler
from pylikec.signal import SIGINT, SIGUSR1

# Handle a signal
Signal.handle(SIGINT, lambda s, f: print("Caught!"))

# Ignore a signal
Signal.ignore(SIGPIPE)

# Send signal to process
Signal.send(SIGUSR1, pid)

# Decorator style
@SignalHandler(SIGINT)
def on_ctrl_c(signum, frame):
    print("Graceful shutdown!")

# Timeout context manager
with SignalTimeout(5):
    do_something_slow()   # raises TimeoutError after 5s

Exceptions

Exception Cause
NullPointerException Dereferencing a null pointer
SegmentationFault Pointer arithmetic out of bounds
BrokenPipeError Writing to a closed pipe
PipeNotReadableError Reading from a closed read end
PipeNotWritableError Writing to a closed write end
ForkError Fork failed or invalid operation
SignalError Invalid signal or handler
InvalidReferenceError Ref target no longer exists

Requirements

  • Python >= 3.8
  • Linux or macOS (fork/signal not supported on Windows)

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

py_like_c-1.0.0.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

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

py_like_c-1.0.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file py_like_c-1.0.0.tar.gz.

File metadata

  • Download URL: py_like_c-1.0.0.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for py_like_c-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a62a161370a29c21cd038a4d61bc2b7d0a14ded068aa4528b1cfaac8a9784380
MD5 409ea4201897de3096dfec44f0a584f1
BLAKE2b-256 93625418b8b767f6bcaba3ca92ba32e40a93069b7e0537c2948a0f0b24959235

See more details on using hashes here.

File details

Details for the file py_like_c-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: py_like_c-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for py_like_c-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fbf9f88dee26d3159fe2bbc3df263310a64d0a3ac94c4c0969c77907135b7014
MD5 4c95bb9c46f097c152c48a84b04526a7
BLAKE2b-256 76cc3fc3de5d180b09f3334de68e10b1117a14bd2dc96eaaf96b651c246b83fb

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