docs/library/struct: Embed format tables.

Also add note about long support.

Signed-off-by: Laurens Valk <laurens@pybricks.com>
This commit is contained in:
Laurens Valk 2022-12-01 16:30:03 +01:00
parent 6503cd47af
commit a6fd13a026
1 changed files with 51 additions and 4 deletions

View File

@ -6,11 +6,58 @@
|see_cpython_module| :mod:`python:struct`.
Supported size/byte order prefixes: ``@``, ``<``, ``>``, ``!``.
The following byte orders are supported:
Supported format codes: ``b``, ``B``, ``h``, ``H``, ``i``, ``I``, ``l``,
``L``, ``q``, ``Q``, ``s``, ``P``, ``f``, ``d`` (the latter 2 depending
on the floating-point support).
+-----------+------------------------+----------+-----------+
| Character | Byte order | Size | Alignment |
+===========+========================+==========+===========+
| @ | native | native | native |
+-----------+------------------------+----------+-----------+
| < | little-endian | standard | none |
+-----------+------------------------+----------+-----------+
| > | big-endian | standard | none |
+-----------+------------------------+----------+-----------+
| ! | network (= big-endian) | standard | none |
+-----------+------------------------+----------+-----------+
The following data types are supported:
+--------+--------------------+-------------------+---------------+
| Format | C Type | Python type | Standard size |
+========+====================+===================+===============+
| b | signed char | integer | 1 |
+--------+--------------------+-------------------+---------------+
| B | unsigned char | integer | 1 |
+--------+--------------------+-------------------+---------------+
| h | short | integer | 2 |
+--------+--------------------+-------------------+---------------+
| H | unsigned short | integer | 2 |
+--------+--------------------+-------------------+---------------+
| i | int | integer (`1<fn>`) | 4 |
+--------+--------------------+-------------------+---------------+
| I | unsigned int | integer (`1<fn>`) | 4 |
+--------+--------------------+-------------------+---------------+
| l | long | integer (`1<fn>`) | 4 |
+--------+--------------------+-------------------+---------------+
| L | unsigned long | integer (`1<fn>`) | 4 |
+--------+--------------------+-------------------+---------------+
| q | long long | integer (`1<fn>`) | 8 |
+--------+--------------------+-------------------+---------------+
| Q | unsigned long long | integer (`1<fn>`) | 8 |
+--------+--------------------+-------------------+---------------+
| f | float | float (`2<fn>`) | 4 |
+--------+--------------------+-------------------+---------------+
| d | double | float (`2<fn>`) | 8 |
+--------+--------------------+-------------------+---------------+
| s | char[] | bytes | |
+--------+--------------------+-------------------+---------------+
| P | void * | integer | |
+--------+--------------------+-------------------+---------------+
.. _fn:
(1) Requires long support when used with values larger than 30 bits.
(2) Requires floating point support.
.. admonition:: Difference to CPython
:class: attention