Installation¶
PyCyBase is a Cython extension package — it cannot be used with
pip install -e . (editable mode breaks .pxd resolution for
downstream Cython projects that cimport from it). Extensions must
be compiled in-place before installation.
Quick Build (POSIX)¶
git clone https://github.com/BolunHan/PyCyBase.git
cd PyCyBase
# Build + install (recommended)
./build.sh -i
# Or via Makefile:
make build && pip install -U . --no-build-isolation
# Or step by step:
python setup.py build_ext --inplace --verbose --force
pip install -U . --no-build-isolation
Build Script Reference¶
build.sh¶
The primary build script for POSIX (Linux/macOS). Supports venv activation, clean/rebuild/all-clean modes, optional pip install, and compile-time macro introspection.
./build.sh [options]
Options:
|
Path to virtual environment to activate before building |
|
|
|
Force-reinstall (uninstall + |
|
Clean build artifacts only (no build) |
|
Deep clean — remove |
|
List all compile-time macros and their default values |
|
Show help |
Makefile¶
Convenience targets wrapping build.sh:
Target |
Effect |
|---|---|
|
Clean + |
|
Alias for |
|
Build + |
|
Build + force-reinstall (uninstall first) |
|
Remove |
|
|
|
List compile-time macros (delegates to |
build.ps1 (Windows)¶
PowerShell build script for Windows NT. Activates a specified venv,
cleans artifacts, and runs build_ext --inplace --verbose --force.
.\build.ps1 -VenvPath "C:\Users\...\venv_313"
nt_build.py (Cross-Compile from Linux)¶
Paramiko-based orchestrator that syncs the local source tree to a
Windows VM via SSH, then triggers build.ps1 and runs tests.
Configured via nt_config.json.
python nt_build.py # sync → build → test
python nt_build.py --host Win11-CN
Compile-Time Macros¶
PyCyBase exposes numerous #define macros that control allocation
behaviour, page sizes, thread safety, and debugging. Override any
macro at compile time by setting an environment variable of the same
name:
DEBUG=1 ./build.sh # Enable debug mode
AP_ALLOC_VIGILANT=0 make build # Disable vigilant/canary checks
Listing available macros:
./build.sh -l # Reads macros.json; auto-generates via probe.py if missing
make list-args # Same, via Makefile
python probe.py # Run the probe manually
probe.py scans Cython .pxd files for cdef extern from
headers, extracts every #define macro from those headers, and
writes a JSON inventory to macros.json.
Key macros (see macros.json for the full list):
Macro |
Default |
Description |
|---|---|---|
|
|
Enable bounds/canary validation |
|
|
Magic sentinel for live allocations |
|
|
Magic sentinel for freed memory |
|
|
Auto-free on refcount reaching zero |
|
|
Enable pthread mutex locking |
|
|
Default to SHM (off = heap default) |
|
|
Enable free-list reuse |
|
|
Default heap page size |
|
|
Max heap page size |
|
|
Heap page alignment |
|
|
Default SHM page size |
|
|
Max SHM page size |
|
|
SHM page alignment |
|
|
SHM name prefix |
|
|
Virtual region size |
|
|
Max SHM name length |
|
|
Max custom prefix length |
Prerequisites¶
Python: 3.12 or later
Build: Cython ≥ 3.0, C11 compiler (GCC/Clang on Linux, MSVC on Windows)
Runtime: No external Python dependencies (stdlib only for the Python layer)
Docs (optional):
sphinx+furo
Linux¶
# Ubuntu/Debian
sudo apt-get install build-essential python3-dev
# Arch/Manjaro
sudo pacman -S base-devel
Windows¶
Visual C++ Build Tools (download)
Select “Desktop development with C++” and Windows SDK during install
Verifying the Build¶
import cbase
print(cbase.__version__)
from cbase.allocator_protocol import AllocatorProtocol, AP_SHARED
from cbase.bytemap import ByteMap
from cbase.intern_string import POOL
Using get_include() in Downstream Projects¶
PyCyBase provides cbase.get_include() to help downstream Cython
extensions find .pxd and .h files:
from setuptools import setup, Extension
from Cython.Build import cythonize
import cbase
ext = Extension(
"my_module",
sources=["my_module.pyx"],
include_dirs=cbase.get_include(),
)
setup(ext_modules=cythonize([ext]))
Important
pip install -e . is not supported for PyCyBase. Editable
installs break .pxd resolution because cimport searches
Cython/Includes/ for the package name, not the editable
egg-link. Always use setup.py build_ext --inplace followed by
pip install -U . --no-build-isolation.
Platform-Specific Notes¶
SHM Allocator¶
POSIX (Linux/macOS): Full
SharedMemoryAllocatorwithshm_open/mmap, cross-process pointer stability.Windows:
NtSharedMemoryAllocatorcompat layer (limited functionality, no cross-process sharing).
NB: the top-level cbase.allocator_protocol package selects the
correct backend automatically based on sys.platform.
NT Cross-Compilation¶
For cross-compiling to Windows from a Linux host, use the NT build orchestrator:
pip install paramiko # (in the host venv)
python nt_build.py # sync → build → test on remote VM
Configuration is in nt_config.json. See the memory files for
detailed VM connection and environment setup.
Troubleshooting¶
Compilation fails with missing Python.h
sudo apt-get install python3-dev
Cython version mismatch
pip install -U cython
Binary incompatibility after rebuild
If you see ValueError: ... size changed, may indicate binary
incompatibility, do a deep clean and rebuild:
./build.sh -a # Remove all .c and .so files
./build.sh -i # Rebuild and install
Multiprocessing forkserver errors (Python 3.14 on Arch)
Set the multiprocessing start method before building:
export N_THREADS=0
python setup.py build_ext --inplace --verbose --force
Building Documentation¶
pip install sphinx furo
cd docs
sphinx-build -M html . _build
Open docs/_build/html/index.html in a browser.