Repository navigation
bpo-46906: Add PyFloat_Pack8() to the C API #31657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Add new functions to pack and unpack C double (serialize and deserialize): * PyFloat_Pack2() * PyFloat_Pack4() * PyFloat_Pack8() * PyFloat_Unpack2() * PyFloat_Unpack4() * PyFloat_Unpack8() Document these functions and add unit tests. Rename functions and move them from the internal C API to the public C API: * _PyFloat_Pack2() => PyFloat_Pack2() * _PyFloat_Pack4() => PyFloat_Pack4() * _PyFloat_Pack8() => PyFloat_Pack8() * _PyFloat_Unpack2() => PyFloat_Unpack2() * _PyFloat_Unpack4() => PyFloat_Unpack4() * _PyFloat_Unpack8() => PyFloat_Unpack8() Replace the "unsigned char*" type with "char*" which is more common and easy to use.
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,3 +76,83 @@ Floating Point Objects | |
| .. c:function:: double PyFloat_GetMin() | ||
|
|
||
| Return the minimum normalized positive float *DBL_MIN* as C :c:type:`double`. | ||
|
|
||
|
|
||
| Pack and Unpack functions | ||
| ========================= | ||
|
|
||
| The :mod:`struct` and :mod:`pickle` (at least) modules need an efficient | ||
| platform- independent way to store floating-point values as byte strings. The | ||
|
vstinner marked this conversation as resolved.
Outdated
|
||
| Pack routines produce a bytes string from a C :c:type:`double`, and the Unpack | ||
| routines produce a C :c:type:`double` from such a bytes string. The suffix | ||
| (2, 4 or 8) specifies the number of bytes in the bytes string. | ||
|
|
||
| On platforms that appear to use IEEE-754 formats these functions work by | ||
| copying bits. On other platforms, the formats the 4- byte format (32-bit) is | ||
|
vstinner marked this conversation as resolved.
Outdated
|
||
| identical to the IEEE-754 single precision format, and the 8-byte format | ||
|
vstinner marked this conversation as resolved.
Outdated
|
||
| (64-bit) to the IEEE-754 double precision format, although the packing of INFs | ||
|
vstinner marked this conversation as resolved.
Outdated
|
||
| and NaNs (if such things exist on the platform) isn't handled correctly, and | ||
| attempting to unpack a bytes string containing an IEEE INF or NaN will raise an | ||
| exception. | ||
|
|
||
| On non-IEEE platforms with more precision, or larger dynamic range, than IEEE | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Presumably once we're assuming IEEE 754 for CPython, all this text can be dropped?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My plan is to first fix the Python 3.11 "regression" by exposing these functions as public C API functions. And then deprecate or remove support for non-IEEE 754 platforms.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Require IEEE 754 support is tracked by https://bugs.python.org/issue46917 |
||
| 754 supports, not all values can be packed; on non-IEEE platforms with less | ||
| precision, or smaller dynamic range, not all values can be unpacked. What | ||
| happens in such cases is partly accidental (alas). | ||
|
|
||
| .. versionadded:: 3.11 | ||
|
|
||
| Pack functions | ||
| -------------- | ||
|
|
||
| The pack routines write 2, 4 or 8 bytes, starting at *p*. *le* is a bool | ||
|
vstinner marked this conversation as resolved.
Outdated
|
||
| argument, true if you want the bytes string in little-endian format (exponent | ||
| last, at ``p+1``, ``p+3`` or ``p+7``), false if you want big-endian format | ||
|
vstinner marked this conversation as resolved.
Outdated
|
||
| (exponent first, at *p*). | ||
|
|
||
| Return value: ``0`` if all is OK, ``-1`` if error (and an exception is set, | ||
| most likely :exc:`OverflowError`). | ||
|
|
||
| There are two problems on non-IEEE platforms: | ||
|
|
||
| * What this does is undefined if *x* is a NaN or infinity. | ||
| * -0.0 and +0.0 produce the same bytes string. | ||
|
|
||
| .. c:function:: int PyFloat_Pack2(double x, unsigned char *p, int le) | ||
|
|
||
| Pack a C double as 16-bit. | ||
|
vstinner marked this conversation as resolved.
Outdated
|
||
|
|
||
| .. c:function:: int PyFloat_Pack4(double x, unsigned char *p, int le) | ||
|
|
||
| Pack a C double as 32-bit. | ||
|
|
||
| .. c:function:: int PyFloat_Pack8(double x, unsigned char *p, int le) | ||
|
|
||
| Pack a C double as 64-bit. | ||
|
|
||
|
|
||
| Unpack functions | ||
| ---------------- | ||
|
|
||
| The unpack routines read 2, 4 or 8 bytes, starting at *p*. *le* is a bool | ||
|
vstinner marked this conversation as resolved.
Outdated
|
||
| argument, true if the bytes string is in little-endian format (exponent last, | ||
| at ``p+1``, ``p+3`` or ``p+7``), false if big-endian (exponent first, at *p*). | ||
|
|
||
| Return value: The unpacked double. On error, this is ``-1.0`` and | ||
| :c:func:`PyErr_Occurred` is true (and an exception is set, most likely | ||
| :exc:`OverflowError`). | ||
|
|
||
| Note that on a non-IEEE platform this will refuse to unpack a bytes string that | ||
| represents a NaN or infinity. | ||
|
|
||
| .. c:function:: double PyFloat_Unpack2(const unsigned char *p, int le) | ||
|
|
||
| Unpack 16-bit as a C double. | ||
|
|
||
| .. c:function:: double PyFloat_Unpack4(const unsigned char *p, int le) | ||
|
|
||
| Unpack 32-bit as a C double. | ||
|
|
||
| .. c:function:: double PyFloat_Unpack8(const unsigned char *p, int le) | ||
|
|
||
| Unpack 64-bit as a C double. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Add new functions to pack and unpack C double (serialize and deserialize): | ||
| :c:func:`PyFloat_Pack2`, :c:func:`PyFloat_Pack4`, :c:func:`PyFloat_Pack8`, | ||
| :c:func:`PyFloat_Unpack2`, :c:func:`PyFloat_Unpack4` and | ||
| :c:func:`PyFloat_Unpack8`. Patch by Victor Stinner. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,6 @@ | |
|
|
||
| #include "pycore_bitutils.h" // _Py_bswap32() | ||
| #include "pycore_call.h" // _PyObject_CallNoArgs() | ||
| #include "pycore_floatobject.h" // _PyFloat_Pack8() | ||
|
|
||
| #include <ffi.h> | ||
| #include "ctypes.h" | ||
|
|
@@ -1009,10 +1008,10 @@ d_set_sw(void *ptr, PyObject *value, Py_ssize_t size) | |
| if (x == -1 && PyErr_Occurred()) | ||
| return NULL; | ||
| #ifdef WORDS_BIGENDIAN | ||
| if (_PyFloat_Pack8(x, (unsigned char *)ptr, 1)) | ||
| if (PyFloat_Pack8(x, ptr, 1)) | ||
| return NULL; | ||
| #else | ||
| if (_PyFloat_Pack8(x, (unsigned char *)ptr, 0)) | ||
| if (PyFloat_Pack8(x, ptr, 0)) | ||
| return NULL; | ||
| #endif | ||
| _RET(value); | ||
|
|
@@ -1022,9 +1021,9 @@ static PyObject * | |
| d_get_sw(void *ptr, Py_ssize_t size) | ||
| { | ||
| #ifdef WORDS_BIGENDIAN | ||
| return PyFloat_FromDouble(_PyFloat_Unpack8(ptr, 1)); | ||
| return PyFloat_FromDouble(PyFloat_Unpack8(ptr, 1)); | ||
| #else | ||
| return PyFloat_FromDouble(_PyFloat_Unpack8(ptr, 0)); | ||
| return PyFloat_FromDouble(PyFloat_Unpack8(ptr, 0)); | ||
| #endif | ||
| } | ||
|
|
||
|
|
@@ -1057,10 +1056,10 @@ f_set_sw(void *ptr, PyObject *value, Py_ssize_t size) | |
| if (x == -1 && PyErr_Occurred()) | ||
| return NULL; | ||
| #ifdef WORDS_BIGENDIAN | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this parameter is changed, it will be way harder to provide this function in pythoncapi_compat to older Python versions: python/pythoncapi-compat#26 And it maybe become more complicated to write a single code base supporting old and new Python versions. It's easy to implement the "native" behavior by checking if your system uses little endian or not with your preferred way.
So far, I didn't see a module using the "native endian". For a serialization function, it's not convenient.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wrote #31832 to suggest using PY_BIG_ENDIAN macro to use the native endian. |
||
| if (_PyFloat_Pack4(x, (unsigned char *)ptr, 1)) | ||
| if (PyFloat_Pack4(x, ptr, 1)) | ||
| return NULL; | ||
| #else | ||
| if (_PyFloat_Pack4(x, (unsigned char *)ptr, 0)) | ||
| if (PyFloat_Pack4(x, ptr, 0)) | ||
| return NULL; | ||
| #endif | ||
| _RET(value); | ||
|
|
@@ -1070,9 +1069,9 @@ static PyObject * | |
| f_get_sw(void *ptr, Py_ssize_t size) | ||
| { | ||
| #ifdef WORDS_BIGENDIAN | ||
| return PyFloat_FromDouble(_PyFloat_Unpack4(ptr, 1)); | ||
| return PyFloat_FromDouble(PyFloat_Unpack4(ptr, 1)); | ||
| #else | ||
| return PyFloat_FromDouble(_PyFloat_Unpack4(ptr, 0)); | ||
| return PyFloat_FromDouble(PyFloat_Unpack4(ptr, 0)); | ||
| #endif | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.