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 py-like-c

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: py_like_c-1.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 487c065ee3bada757bd15eb7d99543324a096fb9931d636911933b73c962e61d
MD5 c5c339fdfbb613054421a7207c03ba4e
BLAKE2b-256 b2b1b300ba3b0f35950ef47a382332b15a98aeb77733554c9c919d7939fcc913

See more details on using hashes here.

File details

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

File metadata

  • Download URL: py_like_c-1.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3fa61b0d68c00b4ba4a8fe5530afcc7bc144a35ccb9f46fc65510e1c0a2f752b
MD5 89f235ca3632ce9126f0ab229ffdaec1
BLAKE2b-256 14160aa1b45619fb10cb33e8b4881971d8b1eaab93cb426b4396446a334e1aa3

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