You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add `_SimpleCData._type_` docs.
Add type codes to the summary table.
Cross-link `struct`, `array`, and `ctypes`; throw in `numpy` too.
(Anyone wanting to add a code should be aware of those.)
Add `py_object`, and `VARIANT_BOOL` for completeness.
Copy file name to clipboardExpand all lines: Doc/library/array.rst
+7Lines changed: 7 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -63,6 +63,13 @@ Notes:
63
63
(2)
64
64
.. versionadded:: 3.13
65
65
66
+
.. seealso::
67
+
68
+
The :ref:`ctypes <ctypes-fundamental-data-types>` and
69
+
:ref:`struct <format-characters>` modules,
70
+
as well as third-party modules like `numpy <https://numpy.org/doc/stable/reference/arrays.interface.html#object.__array_interface__>`__,
71
+
use similar -- but slightly different -- type codes.
72
+
66
73
67
74
The actual representation of values is determined by the machine architecture
68
75
(strictly speaking, by the C implementation). The actual size can be accessed
All these types can be created by calling them with an optional initializer of
@@ -315,6 +392,16 @@ the correct type and value::
315
392
c_ushort(65533)
316
393
>>>
317
394
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
+
318
405
Since these types are mutable, their value can also be changed afterwards::
319
406
320
407
>>> i = c_int(42)
@@ -2478,6 +2565,29 @@ Fundamental data types
2478
2565
original object return, always a new object is constructed. The same is
2479
2566
true for all other ctypes object instances.
2480
2567
2568
+
Each subclass has a class attribute:
2569
+
2570
+
.. attribute:: _type_
2571
+
2572
+
Class attribute that contains an internal type code, as a
2573
+
single-character string.
2574
+
See :ref:`ctypes-fundamental-data-types` for a summary.
2575
+
2576
+
Types marked \* in the summary may be (or always are) aliases of a
2577
+
different :class:`_SimpleCData` subclass, and will not necessarily
2578
+
use the listed type code.
2579
+
For example, if the platform's :c:expr:`long`, :c:expr:`long long`
2580
+
and :c:expr:`time_t` C types are the same, then :class:`c_long`,
2581
+
:class:`c_longlong` and :class:`c_time_t` all refer to a single class,
2582
+
:class:`c_long`, whose :attr:`_type_` code is ``'l'``.
2583
+
The ``'L'`` code will be unused.
2584
+
2585
+
.. seealso::
2586
+
2587
+
The :mod:`array` and :ref:`struct <format-characters>` modules,
2588
+
as well as third-party modules like `numpy <https://numpy.org/doc/stable/reference/arrays.interface.html#object.__array_interface__>`__,
2589
+
use similar -- but slightly different -- type codes.
2590
+
2481
2591
2482
2592
Fundamental data types, when returned as foreign function call results, or, for
2483
2593
example, by retrieving structure field members or array items, are transparently
@@ -2599,6 +2709,8 @@ These are the fundamental ctypes data types:
2599
2709
2600
2710
Represents the C :c:expr:`signed long long` datatype. The constructor accepts
2601
2711
an optional integer initializer; no overflow checking is done.
2712
+
On platforms where ``sizeof(long long) == sizeof(long)`` it is an alias
2713
+
to :class:`c_long`.
2602
2714
2603
2715
2604
2716
.. class:: c_short
@@ -2610,18 +2722,23 @@ These are the fundamental ctypes data types:
2610
2722
.. class:: c_size_t
2611
2723
2612
2724
Represents the C :c:type:`size_t` datatype.
2725
+
Usually an alias for another unsigned integer type.
2613
2726
2614
2727
2615
2728
.. class:: c_ssize_t
2616
2729
2617
-
Represents the C :c:type:`ssize_t` datatype.
2730
+
Represents the :c:type:`Py_ssize_t` datatype.
2731
+
This is a signed version of :c:type:`size_t`;
2732
+
that is, the POSIX :c:type:`ssize_t` type.
2733
+
Usually an alias for another integer type.
2618
2734
2619
2735
.. versionadded:: 3.2
2620
2736
2621
2737
2622
2738
.. class:: c_time_t
2623
2739
2624
2740
Represents the C :c:type:`time_t` datatype.
2741
+
Usually an alias for another integer type.
2625
2742
2626
2743
.. versionadded:: 3.12
2627
2744
@@ -2674,6 +2791,8 @@ These are the fundamental ctypes data types:
2674
2791
2675
2792
Represents the C :c:expr:`unsigned long long` datatype. The constructor
2676
2793
accepts an optional integer initializer; no overflow checking is done.
2794
+
On platforms where ``sizeof(long long) == sizeof(long)`` it is an alias
2795
+
to :class:`c_long`.
2677
2796
2678
2797
2679
2798
.. class:: c_ushort
@@ -2725,8 +2844,11 @@ These are the fundamental ctypes data types:
2725
2844
.. versionchanged:: 3.14
2726
2845
:class:`!py_object` is now a :term:`generic type`.
2727
2846
2847
+
.. _ctypes-wintypes:
2848
+
2728
2849
The :mod:`!ctypes.wintypes` module provides quite some other Windows specific
2729
-
data types, for example :c:type:`!HWND`, :c:type:`!WPARAM`, or :c:type:`!DWORD`.
2850
+
data types, for example :c:type:`!HWND`, :c:type:`!WPARAM`,
2851
+
:c:type:`!VARIANT_BOOL` or :c:type:`!DWORD`.
2730
2852
Some useful structures like :c:type:`!MSG` or :c:type:`!RECT` are also defined.
Copy file name to clipboardExpand all lines: Doc/library/struct.rst
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -280,6 +280,12 @@ platform-dependent.
280
280
.. versionchanged:: 3.14
281
281
Added support for the ``'F'`` and ``'D'`` formats.
282
282
283
+
.. seealso::
284
+
285
+
The :mod:`array` and :ref:`ctypes <ctypes-fundamental-data-types>` modules,
286
+
as well as third-party modules like `numpy <https://numpy.org/doc/stable/reference/arrays.interface.html#object.__array_interface__>`__,
287
+
use similar -- but slightly different -- type codes.
0 commit comments