Skip to main content

senri_ffi / SenRi FFI / 千里 FFI / せんり FFI

统一 FFI(外部函数接口)Python 库 — 一套 API 调用原生 C 库,支持 ctypes(内置)和 cffi(可选)双后端。

特性

  • 双后端自动检测 — 优先使用 cffi,不可用时自动降级为 ctypes
  • 零强制依赖ctypes 为标准库内置;cffi 为可选安装
  • 完整类型系统 — 统一的 C 类型名称(int32float64cstring 等),自动映射到原生类型
  • 结构体支持 — 定义 C 结构体,自动处理布局、对齐和紧凑排列
  • 指针 APIPointer 类封装原生内存,支持读写基本类型和 C 字符串
  • 回调函数 — 将 Python 函数封装为 C 函数指针
  • 内存管理allocfreeaddress_oferrnostrerror
  • 类型注解 — 完整的 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 文档

许可证

Apache-2.0

Release files for senri-ffi 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for senri-ffi 0.2.1
File Size Uploaded
senri_ffi-0.2.1.tar.gz 49.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for senri-ffi 0.2.1
File Interpreter ABI Platform
senri_ffi-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 75.7 kB

Release files / senri_ffi-0.2.1.tar.gz

Download URL senri_ffi-0.2.1.tar.gz
Size 49.9 kB
Tags Source
SHA-256 checksum
How to use checksums
fb9a0e98aa488b63a4c2dd04332c75db6ee5e123ea621bcbb32495a1a306de99
BLAKE2b-256 checksum
How to use checksums
59a13d01373c0d2d39a428a0a44c7f728ae0ac7e46ed5ddeb19875f40cc21943
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release files / senri_ffi-0.2.1-py3-none-any.whl

Download URL senri_ffi-0.2.1-py3-none-any.whl
Size 25.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c47043c03f4d0c60ae6111b6eb22677be3baa46c87dcf30f137971e46cd53d6b
BLAKE2b-256 checksum
How to use checksums
3505ca5b3f6a08f933a1bfa99ee8d3ba6f34bfc012991263bb5b6e518fde566e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page