Verified Production Fix
[python/cpython] Remove "unknown_format" code path in PyFloat_Pack/Unpack*()
GH-python/cpython#145633 • Mar 08, 2026
### ROOT CAUSE
The "unknown_format" code path in PyFloat_Pack/Unpack*() functions is currently used to handle floating-point formats that are not IEEE 754. However, this code path is only necessary for certain ARM platforms and is not required for other platforms. Additionally, the "unknown_format" code path is used to detect endianness at runtime, which can be simplified by using configure macros in tests.
### CODE FIX
1. **Remove the "unknown_format" code path for unsupported platforms**: Since the "unknown_format" code path is only necessary for certain ARM platforms and is not required for other platforms, we can remove it for unsupported platforms.
2. **Simplify the endianness detection**: Instead of using the "unknown_format" code path to detect endianness at runtime, we can use configure macros in tests to detect the endianness of the platform.
Here is a possible implementation of the fix:
c
#include
#include
#define ENDIANNESS_LITTLE 1
#define ENDIANNESS_BIG 2
static int detect_endianness(void) {
uint32_t num = 1;
uint8_t *ptr = (uint8_t *)#
if (*ptr == 1) {
return ENDIANNESS_LITTLE;
} else {
return ENDIANNESS_BIG;
}
}
static void PyFloat_Pack8(double value, uint8_t *buffer) {
if (detect_endianness() == ENDIANNESS_LITTLE) {
// Pack little-endian format
// ...
} else {
// Pack big-endian format
// ...
}
}
static double PyFloat_Unpack8(const uint8_t *buffer) {
if (detect_endianness() == ENDIANNESS_LITTLE) {
// Unpack little-endian format
// ...
} else {
// Unpack big-endian format
// ...
}
}
In this implementation, the `detect_endianness` function is used to detect the endianness of the platform at runtime. The `PyFloat_Pack8` and `PyFloat_Unpack8` functions use the endianness detected by `detect_endianness` to pack and unpack floating-point numbers.
By removing the "unknown_format" code path for unsupported platforms and simplifying the endianness detection, we can improve the performance and reliability of the PyFloat_Pack/Unpack*() functions.
Deploy with Vultr
Use this fix in production instantly. Claim your high-performance developer credit.
Get Started with Vultr →
digital