{"record":{"id":"2760d05053a55ecb","repo":"numpy/numpy","slug":"unknown-ctypes-type-t-name","errorCode":null,"errorMessage":"Unknown ctypes type {t.__name__}","messagePattern":"Unknown ctypes type (.+?)","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"numpy/_core/_dtype_ctypes.py","lineNumber":119,"sourceCode":"\n\ndef dtype_from_ctypes_type(t):\n    \"\"\"\n    Construct a dtype object from a ctypes type\n    \"\"\"\n    import _ctypes\n    if issubclass(t, _ctypes.Array):\n        return _from_ctypes_array(t)\n    elif issubclass(t, _ctypes._Pointer):\n        raise TypeError(\"ctypes pointers have no dtype equivalent\")\n    elif issubclass(t, _ctypes.Structure):\n        return _from_ctypes_structure(t)\n    elif issubclass(t, _ctypes.Union):\n        return _from_ctypes_union(t)\n    elif isinstance(getattr(t, '_type_', None), str):\n        return _from_ctypes_scalar(t)\n    else:\n        raise NotImplementedError(\n            f\"Unknown ctypes type {t.__name__}\")\n","sourceCodeStart":101,"sourceCodeEnd":121,"githubUrl":"https://github.com/numpy/numpy/blob/e117b3ca4edacf581f440dccfd7f3242f0312afa/numpy/_core/_dtype_ctypes.py#L101-L121","documentation":"`dtype_from_ctypes_type` handles Arrays, Pointers, Structures, Unions, and scalar types with a `_type_` string. Any ctypes type that is none of these (e.g. a ctypes function pointer `CFUNCTYPE`, a `_CData` subclass, or a custom ctypes object without `_type_`) falls through to NotImplementedError with the type's name.","triggerScenarios":"Calling np.dtype on a ctypes function pointer type (`CFUNCTYPE(...)`), a `ctypes.c_void_p` in some edge cases, or a non-standard ctypes subclass without `_type_`. Also wrapping ctypes callback types.","commonSituations":"FFI code that passes arbitrary ctypes objects to numpy; auto-conversion layers that try `np.dtype(x)` on every ctypes type.","solutions":["Map the unsupported ctypes type to an explicit numpy dtype yourself (e.g. function pointer -> np.dtype('P') / np.uintp).","Avoid passing function-pointer/callback ctypes types through np.dtype; handle them separately in your FFI layer.","Pre-check the ctypes type kind before calling np.dtype and branch to the right conversion."],"exampleFix":"// before\nfptype = ctypes.CFUNCTYPE(None, ctypes.c_int)\nnp.dtype(fptype)   # NotImplementedError: Unknown ctypes type\n\n// after\nnp.dtype(np.uintp)   # represent pointer-sized handle explicitly","handlingStrategy":"type-guard","validationCode":"import _ctypes\ndef is_supported_ctypes(t) -> bool:\n    return (issubclass(t, _ctypes.Array) or issubclass(t, _ctypes.Structure)\n            or issubclass(t, _ctypes.Union)\n            or isinstance(getattr(t,'_type_',None), str))\nif not is_supported_ctypes(MyType):\n    raise NotImplementedError(f'no numpy dtype for {MyType.__name__}')","typeGuard":"import _ctypes\ndef is_convertible_ctypes(t) -> bool:\n    return issubclass(t, (_ctypes.Array, _ctypes.Structure, _ctypes.Union)) or isinstance(getattr(t,'_type_',None), str)","tryCatchPattern":"try:\n    dt = np.dtype(t)\nexcept NotImplementedError:\n    dt = np.dtype(np.uintp)   # generic pointer-sized fallback","preventionTips":["Do not pass ctypes function-pointer/callback types to np.dtype.","Maintain an explicit mapping for unusual ctypes types.","Pre-check the ctypes type category before conversion."],"tags":["ctypes","dtype","not-implemented","interop"],"analyzedSha":"e117b3ca4edacf581f440dccfd7f3242f0312afa","analyzedAt":"2026-08-07T01:25:31.049Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}