Metadata-Version: 2.4
Name: py-like-c
Version: 1.0.2
Summary: Low-level C concepts brought to Python
Home-page: https://github.com/shahin1717/py-like-c
Author: Shahin Alakparov
Author-email: sahinalekperov5@gmail.com
License: MIT
Project-URL: Homepage, https://github.com/shahin1717/py-like-c
Requires-Python: >=3.8
Description-Content-Type: text/markdown
Dynamic: author
Dynamic: author-email
Dynamic: home-page
Dynamic: requires-python

# 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

```bash
pip install py-like-c
```

## Modules

### Pointer
```python
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
```python
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
```python
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
```python
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
```python
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)
