Skip to main content

Unified FFI library for Python

Project description

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

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

senri_ffi-0.1.0.tar.gz (34.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

senri_ffi-0.1.0-py3-none-any.whl (25.1 kB view details)

Uploaded Python 3

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

Hashes for senri_ffi-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f3bb6ea68f9d556e2887ee3cfd3ca84d22441a40da9b96a8a11b47992e0658d6
MD5 7da7630377133a25d7e655e6b3a4ee87
BLAKE2b-256 6e9785c56e96dcfffb6ca35a705a44dbe77d7e65dc0d8d3b809d0c914d75455d

See more details on using hashes here.

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

Hashes for senri_ffi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 89f05c3edca6a3aad83ed7ce3bed1974bb4c54c424d36632ae9a7cafb9a7ae07
MD5 3fd8907a47cbeb4f75ee4b2e2bf51735
BLAKE2b-256 817a0643ed690879d5b54abd3b7ccf598e803b44485d3708cf32e74b5c4686a3

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