Unified FFI library for Python
Project description
senri_ffi / SenRi FFI / 千里 FFI / せんり FFI
统一 FFI(外部函数接口)Python 库 — 一套 API 调用原生 C 库,支持 ctypes(内置)和 cffi(可选)双后端。
特性
- 双后端自动检测 — 优先使用
cffi,不可用时自动降级为ctypes - 零强制依赖 —
ctypes为标准库内置;cffi为可选安装 - 完整类型系统 — 统一的 C 类型名称(
int32、float64、cstring等),自动映射到原生类型 - 结构体支持 — 定义 C 结构体,自动处理布局、对齐和紧凑排列
- 指针 API —
Pointer类封装原生内存,支持读写基本类型和 C 字符串 - 回调函数 — 将 Python 函数封装为 C 函数指针
- 内存管理 —
alloc、free、address_of、errno、strerror - 类型注解 — 完整的
py.typed支持,开箱即用
安装
pip install senri-ffi
如需使用 cffi 后端:
pip install "senri-ffi[cffi]"
快速开始
from senri_ffi import Library, types
# 加载原生库
lib = Library.load("user32.dll") # Windows
# lib = Library.load("libm.so.6") # Linux
# lib = Library.load("libm.dylib") # macOS
# 调用 C 函数
abs_fn = lib.func("abs", types.int32, [types.int32])
print(abs_fn(-42)) # 42
# 别忘了关闭
lib.close()
结构体示例
from senri_ffi import Library, types, struct
Point = struct({
"x": types.int32,
"y": types.int32,
})
lib = Library.load("mylib.so")
create_point = lib.func("create_point", types.pointer, [])
ptr = create_point()
p = Point.from_pointer(ptr)
print(p.x, p.y)
运行要求
| Python 版本 | 说明 |
|---|---|
| >= 3.13 | 必需 |
| 后端 | 安装方式 | 说明 |
|---|---|---|
ctypes |
内置(标准库) | 默认后端,无需额外安装 |
cffi |
pip install "senri-ffi[cffi]" |
优先使用的后端,性能更优 |
文档
文档请参阅 SenRi FFI 文档
许可证
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
senri_ffi-0.1.0.tar.gz
(34.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
senri_ffi-0.1.0-py3-none-any.whl
(25.1 kB
view details)
File details
Details for the file senri_ffi-0.1.0.tar.gz.
File metadata
- Download URL: senri_ffi-0.1.0.tar.gz
- Upload date:
- Size: 34.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f3bb6ea68f9d556e2887ee3cfd3ca84d22441a40da9b96a8a11b47992e0658d6
|
|
| MD5 |
7da7630377133a25d7e655e6b3a4ee87
|
|
| BLAKE2b-256 |
6e9785c56e96dcfffb6ca35a705a44dbe77d7e65dc0d8d3b809d0c914d75455d
|
File details
Details for the file senri_ffi-0.1.0-py3-none-any.whl.
File metadata
- Download URL: senri_ffi-0.1.0-py3-none-any.whl
- Upload date:
- Size: 25.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89f05c3edca6a3aad83ed7ce3bed1974bb4c54c424d36632ae9a7cafb9a7ae07
|
|
| MD5 |
3fd8907a47cbeb4f75ee4b2e2bf51735
|
|
| BLAKE2b-256 |
817a0643ed690879d5b54abd3b7ccf598e803b44485d3708cf32e74b5c4686a3
|