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.1.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.1-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: py_like_c-1.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 2d28e8874dc0e4f04a6dfe01337611fac58a66ae4984cab650fd901a1c053f16
MD5 611e57c97fc85e11c4537acbb61ccc4f
BLAKE2b-256 184b2187a8193b3a27051d22013257de40256332ec0466c0a30aa16c7951d2ac

See more details on using hashes here.

File details

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

File metadata

  • Download URL: py_like_c-1.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 86af55d63ab3f61734a1e3e1daa618e9834c0aa2913cf49dc1636f162f9dc21e
MD5 41a4a4c4111d2328c54700c544148e26
BLAKE2b-256 d8acf8528b36cd6527806937824a96dd007f064458cccde30599c6dc75c1b3f9

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