Deprecate float.__getformat__()?

With Remove "unknown_format" code path in PyFloat_Pack/Unpack*() · Issue #145633 · python/cpython · GitHub, support for non-IEEE floating-point formats was removed. The remaining part is the semi-public float.__getformat__() class method, that now is redundant, as endianness could be tested by the sys.byteorder.

But despite this method is not useful for the CPython itself, it could be used for alternative implementations, using the CPython’s test suite. Here are some findings from top 15,000 PyPI’s projects, I think they confirm such usage pattern.

I propose deprecation of this method: gh-145633: deprecate float.__getformat__() class method by skirpichev · Pull Request #146400 · python/cpython · GitHub. New helper in the test.support module will replace it for tests. For alternative Python implementations this will make just same checks as before, but with the struct module.

Alternative would be some sys/sys.float_info option. But I don’t sure about it’s meaning. The former test in the float.__getformat__() just checked binary format for some chosen finite number to be as IEEE 754 double. But some tests in the CPython test suite, that depend on this, certainly assume more about runtime behavior of floating-point arithmetic, not just about binary format.

I proposed to expose the __STDC_IEC_559__ macro. That will means “full IEEE 754 compatibility”. Unfortunately, it seems it’s adoption by compilers/libc is provisional. On practice it doesn’t indicate conformance to the standard.

1 Like

Replacing float.__getformat__() string with a sys.float_info.ieee_754 boolean may be a good compromise. sys.float_info.ieee_754 would only check the binary format, something like this C code:

        double x = 9006104071832581.0;
        if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
            ieee_754 = true;
        else if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
            ieee_754 = true;
        else if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
            ieee_754 = false;

In this case, @test.support.requires_IEEE_754 decorator should implement additional tests.

That is a flag with a clear meaning. But why do you think it will be useful?

test.support.requires_IEEE_754 actually assume, that implementation “enough” follows to the IEEE 754, including runtime behavior.

And this is a heuristics, right?