ctypeslib submodule
Typed ctypes aliases for NumPy scalar-like C types.
This module is named after numpy.ctypeslib, and follows the
dtype-to-ctypes correspondence that
np.ctypeslib.as_ctypes_type implements.
The concrete names are plain re-exports: they are the ctypes types, so they
can be instantiated and passed to argtypes/restype as usual. The abstract
names are typing-only.
Throughout this page, ctypes is imported as ct:
The module assumes a C99-compatible compiler, a 32- or 64-bit system, and an
ILP32, LLP64, or LP64 data model. If that isn't the case
for your platform, please open an issue.
Note
Generic shadows typing.Generic, and Array means something different
here than it does in the parent optype.numpy namespace. Both are
intentional (the names mirror np.generic and np.ndarray) and are safe
as long as the module is imported as a namespace rather than star-imported.
Concrete types
| alias | ctypes |
NumPy analogue | kind |
|---|---|---|---|
Bool |
c_bool |
np.bool |
boolean |
Int8 |
c_int8 |
np.int8 |
fixed-width integer |
UInt8 |
c_uint8 |
np.uint8 |
fixed-width integer |
Int16 |
c_int16 |
np.int16 |
fixed-width integer |
UInt16 |
c_uint16 |
np.uint16 |
fixed-width integer |
Int32 |
c_int32 |
np.int32 |
fixed-width integer |
UInt32 |
c_uint32 |
np.uint32 |
fixed-width integer |
Int64 |
c_int64 |
np.int64 |
fixed-width integer |
UInt64 |
c_uint64 |
np.uint64 |
fixed-width integer |
Byte |
c_byte |
np.byte |
C-native integer |
UByte |
c_ubyte |
np.ubyte |
C-native integer |
Short |
c_short |
np.short |
C-native integer |
UShort |
c_ushort |
np.ushort |
C-native integer |
IntC |
c_int |
np.intc |
C-native integer |
UIntC |
c_uint |
np.uintc |
C-native integer |
IntP |
c_ssize_t |
np.intp |
C-native integer |
UIntP |
c_size_t |
np.uintp |
C-native integer |
Long |
c_long |
np.long |
C-native integer |
ULong |
c_ulong |
np.ulong |
C-native integer |
LongLong |
c_longlong |
np.longlong |
C-native integer |
ULongLong |
c_ulonglong |
np.ulonglong |
C-native integer |
Float32 |
c_float |
np.float32 |
floating-point |
Float64 |
c_double |
np.float64 |
floating-point |
LongDouble |
c_longdouble |
np.longdouble |
floating-point |
Complex64 |
c_float_complex |
np.complex64 |
complex floating-point |
Complex128 |
c_double_complex |
np.complex128 |
complex floating-point |
CLongDouble |
c_longdouble_complex |
np.clongdouble |
complex floating-point |
Bytes |
c_char |
np.bytes_ |
character/byte |
Object |
py_object |
np.object_ |
Python object reference |
Complex types
c_float_complex, c_double_complex, and c_longdouble_complex were added in
Python 3.14 and are not available on Windows.
Where they are unavailable, Complex64, Complex128, and CLongDouble are
still importable but are aliases of Never.
Differences between NumPy and ctypes
np.float16, np.str_, np.datetime64, and np.timedelta64 have no ctypes
counterpart and are deliberately absent. c_wchar is likewise not exposed,
since NumPy maps no dtype onto it.
Abstract type aliases
CType and CScalar correspond to the private ctypes base classes that every
C type derives from. They exist at runtime, but neither is importable from
ctypes by name, and neither is meant to be instantiated; use them in
annotations.
| alias | definition |
|---|---|
CType |
ct._CData |
CScalar[T] |
ct._SimpleCData[T] |
Array[CT: CType] |
ct.Array[CT] | ct.Array[Array[CT]] |
Array is recursive, so it matches arbitrarily nested ctypes arrays:
c_int * 3, c_int * 3 * 4, and deeper.
The remaining aliases mirror the np.generic hierarchy:
| alias | C types |
|---|---|
SignedInteger |
Byte | Short | IntC | Long | LongLong
|
UnsignedInteger |
UByte | UShort | UIntC | ULong | ULongLong
|
Integer |
SignedInteger | UnsignedInteger |
Floating |
Float32 | Float64 | LongDouble |
ComplexFloating |
Complex64 | Complex128 | CLongDouble |
Inexact |
Floating | ComplexFloating |
Number |
Integer | Inexact |
Void |
ct.Structure | ct.Union |
Flexible |
Bytes | Void |
Generic |
Bool | Number | Flexible | Object |
How the numeric aliases are defined
Integer, Floating, and ComplexFloating are defined as CScalar[int],
CScalar[float], and CScalar[complex] rather than as the literal unions
above, so they also admit third-party _SimpleCData subclasses. Inexact and
Number are built from those. Because CScalar is invariant, Bool
(_SimpleCData[bool]) is not covered by Number and is listed separately in
Generic.