14.14.2.7 Fundamental data types

Python 2.5

14.14.2.7 Fundamental data types

This non-public class is the base class of all fundamental ctypes data types. It is mentioned here because it contains the common attributes of the fundamental ctypes data types. _SimpleCData is a subclass of _CData, so it inherits their methods and attributes.

Instances have a single attribute:

This attribute contains the actual value of the instance. For integer and pointer types, it is an integer, for character types, it is a single character string, for character pointer types it is a Python string or unicode string.

When the value attribute is retrieved from a ctypes instance, usually a new object is returned each time. ctypes does not implement original object return, always a new object is constructed. The same is true for all other ctypes object instances.

Fundamental data types, when returned as foreign function call results, or, for example, by retrieving structure field members or array items, are transparently converted to native Python types. In other words, if a foreign function has a restype of c_char_p, you will always receive a Python string, not a c_char_p instance.

Subclasses of fundamental data types do not inherit this behaviour. So, if a foreign functions restype is a subclass of c_void_p, you will receive an instance of this subclass from the function call. Of course, you can get the value of the pointer by accessing the value attribute.

These are the fundamental ctypes data types:

Represents the C signed char datatype, and interprets the value as small integer. The constructor accepts an optional integer initializer; no overflow checking is done.

Represents the C char datatype, and interprets the value as a single character. The constructor accepts an optional string initializer, the length of the string must be exactly one character.

Represents the C char * datatype, which must be a pointer to a zero-terminated string. The constructor accepts an integer address, or a string.

Represents the C double datatype. The constructor accepts an optional float initializer.

Represents the C double datatype. The constructor accepts an optional float initializer.

Represents the C signed int datatype. The constructor accepts an optional integer initializer; no overflow checking is done. On platforms where sizeof(int) == sizeof(long) it is an alias to c_long.

Represents the C 8-bit signed int datatype. Usually an alias for c_byte.

Represents the C 16-bit signed int datatype. Usually an alias for c_short.

Represents the C 32-bit signed int datatype. Usually an alias for c_int.

Represents the C 64-bit signed int datatype. Usually an alias for c_longlong.

Represents the C signed long datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

Represents the C signed long long datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

Represents the C signed short datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

Represents the C size_t datatype.

Represents the C unsigned char datatype, it interprets the value as small integer. The constructor accepts an optional integer initializer; no overflow checking is done.

Represents the C unsigned int datatype. The constructor accepts an optional integer initializer; no overflow checking is done. On platforms where sizeof(int) == sizeof(long) it is an alias for c_ulong.

Represents the C 8-bit unsigned int datatype. Usually an alias for c_ubyte.

Represents the C 16-bit unsigned int datatype. Usually an alias for c_ushort.

Represents the C 32-bit unsigned int datatype. Usually an alias for c_uint.

Represents the C 64-bit unsigned int datatype. Usually an alias for c_ulonglong.

Represents the C unsigned long datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

Represents the C unsigned long long datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

Represents the C unsigned short datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

Represents the C void * type. The value is represented as integer. The constructor accepts an optional integer initializer.

Represents the C wchar_t datatype, and interprets the value as a single character unicode string. The constructor accepts an optional string initializer, the length of the string must be exactly one character.

Represents the C wchar_t * datatype, which must be a pointer to a zero-terminated wide character string. The constructor accepts an integer address, or a string.

Windows only: Represents a HRESULT value, which contains success or error information for a function or method call.

py_object : classdesc*

Represents the C PyObject * datatype. Calling this with an without an argument creates a NULL PyObject * pointer.

The ctypes.wintypes module provides quite some other Windows specific data types, for example HWND, WPARAM, or DWORD. Some useful structures like MSG or RECT are also defined.

See About this document... for information on suggesting changes.