Skip to content

Commit 6c163c2

Browse files
committed
fix(ctypes): validate argument types before binding shared library functions
- Add explicit validation for ctypes function argument declarations before assigning them to the loaded shared library function. - This provides clearer error messages when invalid Python types are passed to `argtypes`, instead of exposing the internal ctypes error about missing `from_param()` methods. Signed-off-by: JamePeng <jame_peng@sina.com>
1 parent 083c4b5 commit 6c163c2

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

llama_cpp/_ctypes_extensions.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,18 @@ def decorator(f: F) -> F:
244244
func = getattr(lib, symbol_name)
245245
except AttributeError:
246246
continue
247+
# Validate ctypes argument declarations before assigning them.
248+
# ctypes requires every argtype to provide from_param().
249+
for index, argtype in enumerate(argtypes):
250+
if not hasattr(argtype, "from_param"):
251+
raise TypeError(
252+
"Invalid ctypes argument type:\n"
253+
f" function: {f.__name__}\n"
254+
f" symbol: {symbol_name}\n"
255+
f" arg index: {index}\n"
256+
f" arg type: {argtype!r}\n"
257+
f" expected: a ctypes type with from_param()"
258+
)
247259

248260
func.argtypes = argtypes
249261
func.restype = restype

0 commit comments

Comments
 (0)