@@ -221,87 +221,164 @@ Fundamental data types
221221
222222:mod: `!ctypes ` defines a number of primitive C compatible data types:
223223
224- +----------------------+------------------------------------------+----------------------------+
225- | ctypes type | C type | Python type |
226- +======================+==========================================+============================+
227- | :class: `c_bool ` | :c:expr: `_Bool ` | bool (1) |
228- +----------------------+------------------------------------------+----------------------------+
229- | :class: `c_char ` | :c:expr: `char ` | 1-character bytes object |
230- +----------------------+------------------------------------------+----------------------------+
231- | :class: `c_wchar ` | :c:type: `wchar_t ` | 1-character string |
232- +----------------------+------------------------------------------+----------------------------+
233- | :class: `c_byte ` | :c:expr: `char ` | int |
234- +----------------------+------------------------------------------+----------------------------+
235- | :class: `c_ubyte ` | :c:expr: `unsigned char ` | int |
236- +----------------------+------------------------------------------+----------------------------+
237- | :class: `c_short ` | :c:expr: `short ` | int |
238- +----------------------+------------------------------------------+----------------------------+
239- | :class: `c_ushort ` | :c:expr: `unsigned short ` | int |
240- +----------------------+------------------------------------------+----------------------------+
241- | :class: `c_int ` | :c:expr: `int ` | int |
242- +----------------------+------------------------------------------+----------------------------+
243- | :class: `c_int8 ` | :c:type: `int8_t ` | int |
244- +----------------------+------------------------------------------+----------------------------+
245- | :class: `c_int16 ` | :c:type: `int16_t ` | int |
246- +----------------------+------------------------------------------+----------------------------+
247- | :class: `c_int32 ` | :c:type: `int32_t ` | int |
248- +----------------------+------------------------------------------+----------------------------+
249- | :class: `c_int64 ` | :c:type: `int64_t ` | int |
250- +----------------------+------------------------------------------+----------------------------+
251- | :class: `c_uint ` | :c:expr: `unsigned int ` | int |
252- +----------------------+------------------------------------------+----------------------------+
253- | :class: `c_uint8 ` | :c:type: `uint8_t ` | int |
254- +----------------------+------------------------------------------+----------------------------+
255- | :class: `c_uint16 ` | :c:type: `uint16_t ` | int |
256- +----------------------+------------------------------------------+----------------------------+
257- | :class: `c_uint32 ` | :c:type: `uint32_t ` | int |
258- +----------------------+------------------------------------------+----------------------------+
259- | :class: `c_uint64 ` | :c:type: `uint64_t ` | int |
260- +----------------------+------------------------------------------+----------------------------+
261- | :class: `c_long ` | :c:expr: `long ` | int |
262- +----------------------+------------------------------------------+----------------------------+
263- | :class: `c_ulong ` | :c:expr: `unsigned long ` | int |
264- +----------------------+------------------------------------------+----------------------------+
265- | :class: `c_longlong ` | :c:expr: `__int64 ` or :c:expr: `long long ` | int |
266- +----------------------+------------------------------------------+----------------------------+
267- | :class: `c_ulonglong ` | :c:expr: `unsigned __int64 ` or | int |
268- | | :c:expr: `unsigned long long ` | |
269- +----------------------+------------------------------------------+----------------------------+
270- | :class: `c_size_t ` | :c:type: `size_t ` | int |
271- +----------------------+------------------------------------------+----------------------------+
272- | :class: `c_ssize_t ` | :c:type: `ssize_t ` or | int |
273- | | :c:expr: `Py_ssize_t ` | |
274- +----------------------+------------------------------------------+----------------------------+
275- | :class: `c_time_t ` | :c:type: `time_t ` | int |
276- +----------------------+------------------------------------------+----------------------------+
277- | :class: `c_float ` | :c:expr: `float ` | float |
278- +----------------------+------------------------------------------+----------------------------+
279- | :class: `c_double ` | :c:expr: `double ` | float |
280- +----------------------+------------------------------------------+----------------------------+
281- | :class: `c_longdouble`| :c:expr:`long double ` | float |
282- +----------------------+------------------------------------------+----------------------------+
283- | :class: `c_char_p ` | :c:expr: `char * ` (NUL terminated) | bytes object or ``None `` |
284- +----------------------+------------------------------------------+----------------------------+
285- | :class: `c_wchar_p ` | :c:expr: `wchar_t * ` (NUL terminated) | string or ``None `` |
286- +----------------------+------------------------------------------+----------------------------+
287- | :class: `c_void_p ` | :c:expr: `void * ` | int or ``None `` |
288- +----------------------+------------------------------------------+----------------------------+
289-
290- (1)
291- The constructor accepts any object with a truth value.
224+ .. list-table ::
225+ :header-rows: 1
226+
227+ * - ctypes type
228+ - C type
229+ - Python type
230+ - :py:attr: `~_SimpleCData._type_ `
231+ * - :class: `c_bool `
232+ - :c:expr: `_Bool `
233+ - :py:class: `bool `
234+ - ``'?' ``
235+ * - :class: `c_char `
236+ - :c:expr: `char `
237+ - 1-character :py:class: `bytes `
238+ - ``'c' ``
239+ * - :class: `c_wchar `
240+ - :c:type: `wchar_t `
241+ - 1-character :py:class: `str `
242+ - ``'u' ``
243+ * - :class: `c_byte `
244+ - :c:expr: `char `
245+ - :py:class: `int `
246+ - ``'b' ``
247+ * - :class: `c_ubyte `
248+ - :c:expr: `unsigned char `
249+ - :py:class: `int `
250+ - ``'B' ``
251+ * - :class: `c_short `
252+ - :c:expr: `short `
253+ - :py:class: `int `
254+ - ``'h' ``
255+ * - :class: `c_ushort `
256+ - :c:expr: `unsigned short `
257+ - :py:class: `int `
258+ - ``'H' ``
259+ * - :class: `c_int `
260+ - :c:expr: `int `
261+ - :py:class: `int `
262+ - ``'i' `` \*
263+ * - :class: `c_int8 `
264+ - :c:type: `int8_t `
265+ - :py:class: `int `
266+ - \*
267+ * - :class: `c_int16 `
268+ - :c:type: `int16_t `
269+ - :py:class: `int `
270+ - \*
271+ * - :class: `c_int32 `
272+ - :c:type: `int32_t `
273+ - :py:class: `int `
274+ - \*
275+ * - :class: `c_int64 `
276+ - :c:type: `int64_t `
277+ - :py:class: `int `
278+ - \*
279+ * - :class: `c_uint `
280+ - :c:expr: `unsigned int `
281+ - :py:class: `int `
282+ - ``'I' `` \*
283+ * - :class: `c_uint8 `
284+ - :c:type: `uint8_t `
285+ - :py:class: `int `
286+ - \*
287+ * - :class: `c_uint16 `
288+ - :c:type: `uint16_t `
289+ - :py:class: `int `
290+ - \*
291+ * - :class: `c_uint32 `
292+ - :c:type: `uint32_t `
293+ - :py:class: `int `
294+ - \*
295+ * - :class: `c_uint64 `
296+ - :c:type: `uint64_t `
297+ - :py:class: `int `
298+ - \*
299+ * - :class: `c_long `
300+ - :c:expr: `long `
301+ - :py:class: `int `
302+ - ``'l' ``
303+ * - :class: `c_ulong `
304+ - :c:expr: `unsigned long `
305+ - :py:class: `int `
306+ - ``'L' ``
307+ * - :class: `c_longlong `
308+ - :c:expr: `long long `
309+ - :py:class: `int `
310+ - ``'q' `` \*
311+ * - :class: `c_ulonglong `
312+ - :c:expr: `unsigned long long `
313+ - :py:class: `int `
314+ - ``'Q' `` \*
315+ * - :class: `c_size_t `
316+ - :c:type: `size_t `
317+ - :py:class: `int `
318+ - \*
319+ * - :class: `c_ssize_t `
320+ - :c:type: `Py_ssize_t `
321+ - :py:class: `int `
322+ - \*
323+ * - :class: `c_time_t `
324+ - :c:type: `time_t `
325+ - :py:class: `int `
326+ - \*
327+ * - :class: `c_float `
328+ - :c:expr: `float `
329+ - :py:class: `float `
330+ - ``'f' ``
331+ * - :class: `c_double `
332+ - :c:expr: `double `
333+ - :py:class: `float `
334+ - ``'d' ``
335+ * - :class: `c_longdouble `
336+ - :c:expr: `long double `
337+ - :py:class: `float `
338+ - ``'g' `` \*
339+ * - :class: `c_char_p `
340+ - :c:expr: `char * ` (NUL terminated)
341+ - :py:class: `bytes ` or ``None ``
342+ - ``'z' ``
343+ * - :class: `c_wchar_p `
344+ - :c:expr: `wchar_t * ` (NUL terminated)
345+ - :py:class: `str ` or ``None ``
346+ - ``'Z' ``
347+ * - :class: `c_void_p `
348+ - :c:expr: `void * `
349+ - :py:class: `int ` or ``None ``
350+ - ``'P' ``
351+ * - :class: `py_object `
352+ - :c:expr: `PyObject * `
353+ - :py:class: `object `
354+ - ``'O' ``
355+ * - :ref: `VARIANT_BOOL <ctypes-wintypes >`
356+ - :c:expr: `short int `
357+ - :py:class: `bool `
358+ - ``'v' ``
292359
293360Additionally, if IEC 60559 compatible complex arithmetic (Annex G) is supported
294361in both C and ``libffi ``, the following complex types are available:
295362
296- +----------------------------------+---------------------------------+-----------------+
297- | ctypes type | C type | Python type |
298- +==================================+=================================+=================+
299- | :class: `c_float_complex ` | :c:expr: `float complex ` | complex |
300- +----------------------------------+---------------------------------+-----------------+
301- | :class: `c_double_complex ` | :c:expr: `double complex ` | complex |
302- +----------------------------------+---------------------------------+-----------------+
303- | :class: `c_longdouble_complex ` | :c:expr: `long double complex ` | complex |
304- +----------------------------------+---------------------------------+-----------------+
363+ .. list-table ::
364+ :header-rows: 1
365+
366+ * - ctypes type
367+ - C type
368+ - Python type
369+ - :py:attr: `~_SimpleCData._type_ `
370+ * - :class: `c_float_complex `
371+ - :c:expr: `float complex `
372+ - :py:class: `complex `
373+ - ``'F' ``
374+ * - :class: `c_double_complex `
375+ - :c:expr: `double complex `
376+ - :py:class: `complex `
377+ - ``'D' ``
378+ * - :class: `c_longdouble_complex `
379+ - :c:expr: `long double complex `
380+ - :py:class: `complex `
381+ - ``'G' ``
305382
306383
307384All these types can be created by calling them with an optional initializer of
@@ -315,6 +392,16 @@ the correct type and value::
315392 c_ushort(65533)
316393 >>>
317394
395+ The constructors for numeric types will convert input using
396+ :py:meth: `~object.__bool__ `,
397+ :py:meth: `~object.__index__ ` (for ``int ``),
398+ :py:meth: `~object.__float__ ` or :py:meth: `~object.__complex__ `.
399+ This means :py:class: `~ctypes.c_bool ` accepts any object with a truth value::
400+
401+ >>> empty_list = []
402+ >>> c_bool(empty_list)
403+ c_bool(False)
404+
318405Since these types are mutable, their value can also be changed afterwards::
319406
320407 >>> i = c_int(42)
@@ -2468,6 +2555,29 @@ Fundamental data types
24682555 original object return, always a new object is constructed. The same is
24692556 true for all other ctypes object instances.
24702557
2558+ Each subclass has a class attribute:
2559+
2560+ .. attribute :: _type_
2561+
2562+ Class attribute that contains an internal type code, as a
2563+ single-character string.
2564+ See :ref: `ctypes-fundamental-data-types ` for a summary.
2565+
2566+ Types marked \* in the summary may be (or always are) aliases of a
2567+ different :class: `_SimpleCData ` subclass, and will not necessarily
2568+ use the listed type code.
2569+ For example, if the platform's :c:expr: `long `, :c:expr: `long long `
2570+ and :c:expr: `time_t ` C types are the same, then :class: `c_long `,
2571+ :class: `c_longlong ` and :class: `c_time_t ` all refer to a single class,
2572+ :class: `c_long `, whose :attr: `_type_ ` code is ``'l' ``.
2573+ The ``'L' `` code will be unused.
2574+
2575+ .. seealso ::
2576+
2577+ The :mod: `array ` and :ref: `struct <format-characters >` modules,
2578+ as well as third-party modules like `numpy <https://numpy.org/doc/stable/reference/arrays.interface.html#object.__array_interface_ _>`__,
2579+ use similar -- but slightly different -- type codes.
2580+
24712581
24722582Fundamental data types, when returned as foreign function call results, or, for
24732583example, by retrieving structure field members or array items, are transparently
@@ -2589,6 +2699,8 @@ These are the fundamental ctypes data types:
25892699
25902700 Represents the C :c:expr: `signed long long ` datatype. The constructor accepts
25912701 an optional integer initializer; no overflow checking is done.
2702+ On platforms where ``sizeof(long long) == sizeof(long) `` it is an alias
2703+ to :class: `c_long `.
25922704
25932705
25942706.. class :: c_short
@@ -2600,18 +2712,23 @@ These are the fundamental ctypes data types:
26002712.. class :: c_size_t
26012713
26022714 Represents the C :c:type: `size_t ` datatype.
2715+ Usually an alias for another integer type.
26032716
26042717
26052718.. class :: c_ssize_t
26062719
2607- Represents the C :c:type: `ssize_t ` datatype.
2720+ Represents the :c:type: `Py_ssize_t ` datatype.
2721+ This is a signed version of :c:type: `size_t `;
2722+ that is, the POSIX :c:type: `ssize_t ` type.
2723+ Usually an alias for another integer type.
26082724
26092725 .. versionadded :: 3.2
26102726
26112727
26122728.. class :: c_time_t
26132729
26142730 Represents the C :c:type: `time_t ` datatype.
2731+ Usually an alias for another integer type.
26152732
26162733 .. versionadded :: 3.12
26172734
@@ -2664,6 +2781,8 @@ These are the fundamental ctypes data types:
26642781
26652782 Represents the C :c:expr: `unsigned long long ` datatype. The constructor
26662783 accepts an optional integer initializer; no overflow checking is done.
2784+ On platforms where ``sizeof(long long) == sizeof(long) `` it is an alias
2785+ to :class: `c_long `.
26672786
26682787
26692788.. class :: c_ushort
@@ -2715,8 +2834,11 @@ These are the fundamental ctypes data types:
27152834 .. versionchanged :: 3.14
27162835 :class: `!py_object ` is now a :term: `generic type `.
27172836
2837+ .. _ctypes-wintypes :
2838+
27182839The :mod: `!ctypes.wintypes ` module provides quite some other Windows specific
2719- data types, for example :c:type: `!HWND `, :c:type: `!WPARAM `, or :c:type: `!DWORD `.
2840+ data types, for example :c:type: `!HWND `, :c:type: `!WPARAM `,
2841+ :c:type: `!VARIANT_BOOL ` or :c:type: `!DWORD `.
27202842Some useful structures like :c:type: `!MSG ` or :c:type: `!RECT ` are also defined.
27212843
27222844
0 commit comments