PyCFuncPtr is the internal base class behind every foreign function and function pointer type in ctypes. You never create it directly. Instead, ctypes.CFUNCTYPE(...), ctypes.PYFUNCTYPE(...) and attribute access on a loaded library such as libc.strlen give you subclasses of it, and calling those objects calls the C function. The class name PyCFuncPtr belongs to the C source of CPython; it is not exported as ctypes.PyCFuncPtr, and the Python-visible base is ctypes._CFuncPtr.
This article shows what that base class gives you (argtypes, restype, errcheck), how the pieces fit together, and how to create function pointers safely.
Importing ctypes and looking for PyCFuncPtr raises AttributeError. The class exists in the extension module and is documented only indirectly, through the objects it produces.
The exact internal class names vary between Python versions (older versions show PyCFuncPtr, Python 3.14 shows CFuncPtr), which is one more reason to rely on isinstance(obj, ctypes._CFuncPtr) at most for debugging and not in application code. Treat the underscore name as private.
There are three common sources, and each returns a callable that is a _CFuncPtr subclass instance.
| Source | Example | Use case |
|---|---|---|
| Library attribute | libc.strlen | Call a function exported by a shared library |
| Prototype and Python callable | CFUNCTYPE(c_int, c_int)(py_func) | Pass a Python callback to C |
| Prototype and address | CFUNCTYPE(c_size_t, c_char_p)(address) | Call a function pointer you received as an integer |
Set argtypes and restype before the first call. Without them ctypes assumes every argument is an int and the return value is a C int.
On Linux this prints 5 and then the lists of types. The argtypes and restype attributes live on the function pointer object, which is why they are set per function and not per library.
CFUNCTYPE builds a prototype, which is a subclass of the function pointer base. Calling the prototype with a Python callable creates a C-callable function pointer.
The prototype type object is also a subclass of the base, so issubclass(CompareFunc, ctypes._CFuncPtr) is true. Always store the callback in a variable that outlives every call that C may make through it; if it is garbage collected while C still holds the pointer, the process crashes.
When C code hands you an address, such as a pointer inside a struct, you wrap it with the matching prototype.
You can also declare a structure field with a prototype type, so a C struct of callbacks maps directly to a ctypes.Structure.
Every function pointer object accepts an errcheck attribute: a Python callable that receives (result, func, args) after each call. Return the value to hand back to the caller, or raise an exception.
Opening the library with use_errno=True makes ctypes.get_errno() return the value C saw. Centralising checks in errcheck keeps every call site free of repeated if result == -1 code.
Which prototype factory you use decides how the call is made.
| Factory | Calling convention | GIL during the call |
|---|---|---|
CFUNCTYPE | cdecl | Released |
WINFUNCTYPE | stdcall (Windows only) | Released |
PYFUNCTYPE | cdecl, Python calling convention | Held |
Releasing the GIL lets other Python threads run while a long C call is in progress. Keep it held only when the function itself uses the Python C API, as the functions in ctypes.pythonapi do.
No. Use ctypes.CFUNCTYPE or ctypes.PYFUNCTYPE to create function pointer types, and load libraries with ctypes.CDLL. The base class is private (ctypes._CFuncPtr) and its internal name has changed between releases.
CFUNCTYPE(...) returns a type that describes a signature. Calling that type with a Python function or an address returns an instance, which is the actual callable pointer.
Usually the callback object was garbage collected, or its declared signature does not match the C declaration. Keep a reference to the callback and double-check every argtypes and restype entry.
ctypes cannot propagate an exception through C frames. It prints the traceback to standard error and returns a default value, so catch exceptions in the callback and signal failure with the return value.
PyCFuncPtr is the hidden base class for ctypes function objects. Work with its subclasses through CFUNCTYPE, PYFUNCTYPE and library attributes, set argtypes, restype and errcheck, keep callbacks referenced while C can call them, and choose the factory that matches the calling convention and GIL needs.