Seven llama_sampler_init_* bindings admit keyword arguments that the ctypes function object silently drops
还没有人认领这个 Issue。
评估
调研方向
从 llama_cpp/llama_cpp.py 中 4553-4606 行附近的七个 llama_sampler_init_* 定义开始,然后阅读 llama_cpp/_ctypes_extensions.py:146-152 中的 ctypes_function,以确认导出函数的创建方式。使这七个绑定参数成为 positional-only,与周围的定义保持一致,并验证它们的签名不再引导使用关键字调用。
由索引模型根据 Issue 内容生成。
描述
Summary
In llama_cpp/llama_cpp.py, seven bindings are declared as defs whose parameters are NOT
positional-only, while every other decorated binding in the file uses /:
line (main d736646) |
def |
|---|---|
| 4553 | def llama_sampler_init_dist(seed: int) -> llama_sampler_p |
| 4560 | def llama_sampler_init_top_k(k: int) -> llama_sampler_p |
| 4570 | def llama_sampler_init_top_p(p: float, min_keep: int) -> llama_sampler_p |
| 4580 | def llama_sampler_init_min_p(p: float, min_keep: int) -> llama_sampler_p |
| 4590 | def llama_sampler_init_typical(p: float, min_keep: int) -> llama_sampler_p |
| 4596 | def llama_sampler_init_temp(t: float) -> llama_sampler_p |
| 4606 | def llama_sampler_init_temp_ext(t: float, delta: float, exponent: float) -> llama_sampler_p |
ctypes_function (llama_cpp/_ctypes_extensions.py:146-152) replaces the def with the
ctypes function object:
def decorator(f):
if enabled:
func = getattr(lib, name)
func.argtypes = argtypes
func.restype = restype
functools.wraps(f)(func)
return func
so the name the module exports IS getattr(lib, "llama_sampler_init_top_k"). A ctypes
function object without paramflags binds no argument by name, but functools.wraps and
the (f: F) -> F typing make inspect.signature, pyright and IDEs show the def's
signature — which invites keyword calls the object cannot honour.
What happens (CPython 3.12.10, ctypes)
>>> import ctypes; lib = ctypes.CDLL(None); f = lib.abs
>>> f.argtypes = [ctypes.c_int]; f.restype = ctypes.c_int
>>> f(-3, x=1) # an extra keyword is DROPPED silently
3
>>> f(x=-3) # all-keyword: refused
TypeError: this function takes at least 1 argument (0 given)
Applied to these bindings:
llama_sampler_init_top_k(k=40)— type-checks clean, raisesTypeErrorat run time.llama_sampler_init_top_p(0.9, min_keep=2)— type-checks clean, runs, and passes ONE
positional to a two-parameter C function:min_keepis silently dropped and the C side
reads whatever the register/stack held for it.
No caller inside the repository is affected today (_internals.py:705-732 and the server
pass positionals), so this is a latent API hazard rather than a current bug — but the
signature is public and the failure is invisible to every static check.
Fix
Make the seven defs positional-only, as the other bindings already are:
def llama_sampler_init_top_k(k: int, /) -> llama_sampler_p: ...
(Alternatively, build the functions through a CFUNCTYPE prototype with paramflags,
which does bind by name — but the one-character fix matches the file's convention.)
Found with SemLinker, a cross-language contract checker we are developing; happy to send a PR.
- 主要语言
- Python
- 星标
- 10.6k
- 派生
- 1.5k
- 平均合并
- 6 小时 43 分钟
- 30 天内合并 PR
- 2
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
abetlen/llama-cpp-python 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 65/100
abetlen/llama-cpp-python#2352 ·
-
难度 2/5 1-3 小时 新手友好度 65/100
abetlen/llama-cpp-python#2211 · 2 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 65/100
abetlen/llama-cpp-python#2210 ·
-
难度 2/5 1-3 小时 新手友好度 65/100
abetlen/llama-cpp-python#2145 ·
-
难度 1/5 1 小时以内 新手友好度 75/100
abetlen/llama-cpp-python#2135 · 4 个 reaction ·
查看 abetlen/llama-cpp-python 的全部 Issue
相似的 Issue
-
essnmx good first issue
难度 1/5 1 小时以内 新手友好度 95/100
-
难度 2/5 1-3 小时 新手友好度 65/100
syfoud/Simulated_Scepter#174 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
Giskard-AI/giskard-oss#2840 · 1 条评论 ·
-
A claim comment carrying the issue number is silently declined while the workflow reports success 未关闭area: repo bug perceived difficulty: 2
难度 2/5 1-3 小时 新手友好度 70/100
-
难度 2/5 1-3 小时 新手友好度 75/100
yeti-platform/yeti#1380 ·