Skip to content

Conversation

@chenx97
Copy link

@chenx97 chenx97 commented Jan 5, 2026

Description

This PR uses C macros to detect NaN encoding schemes for Lib/test/test_struct.py, instead of detecting the host machine.

math.nan depends on toolchain settings for a MIPS build, as setting -mnan=legacy and -mnan=2008 result in different __builtin_nan behavior for GCC.

Tests

  • Ran the official test suite: ./python -m test test_struct (Passed).

@python-cla-bot

This comment was marked as resolved.

@bedevere-app

This comment was marked as resolved.

@chenx97 chenx97 force-pushed the mips-skip-nan-checks branch from 8ff4af9 to 0bbf71e Compare January 5, 2026 09:49
skirpichev
skirpichev previously approved these changes Jan 5, 2026
Copy link
Contributor

@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In principle, this looks good for me. But I would prefer a different solution, see issue thread.

CC @vstinner

@skirpichev skirpichev added the needs backport to 3.14 bugs and security fixes label Jan 5, 2026
@chenx97 chenx97 changed the title gh-143429: Skip NaN encoding assertions for MIPS gh-143429: Use compile-time NaN encoding detection for test_struct Jan 7, 2026
@chenx97 chenx97 requested a review from skirpichev January 7, 2026 12:34
PyModule_Add(m, "nan_encoding", PyUnicode_FromString("parisc"));
#else
PyModule_Add(m, "nan_encoding", PyUnicode_FromString("regular"));
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to move this change to Modules/_testcapi/float.c?

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I just suggest moving the C change to Modules/_testcapi/float.c.

INF = float('inf')
NAN = float('nan')

_testcapi = import_helper.import_module('_testcapi')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only use this import in the test otherwise the entire module will be skiped if _testcapi doesn't exist.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you can use:

try:
    import _testcapi
except ImportError:
    _testcapi = None

And later check if _testcapi is None.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just move test_half_float() test to the test_capi/test_float.py?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree, these tests are not testing the C API.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. But this code essentially just tests PyFloat_Pack2/UnPack2() via the struct module API.

Then, maybe add a configure test and use the sysconfig module to query this stuff? Though, I think it's fine to skip just this one test if _testcapi is missing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or just move _testcapi = import_helper.import_module('_testcapi') in test_half_float().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since _testcapi is only used in that specific test, it should only be imported inside it and possibly skipped. import_helper.import_module already raises SkipTest if it can't import the module so there is no real need to do try-except with _testcapi is None + decorator (unless at some point we need lots of _testcapi accesses...)

Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to explicitly request changes.

@bedevere-app
Copy link

bedevere-app bot commented Jan 7, 2026

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Comment on lines 3362 to 3366
#if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
PyModule_Add(m, "nan_encoding", PyUnicode_FromString("parisc"));
#else
PyModule_Add(m, "nan_encoding", PyUnicode_FromString("regular"));
#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"parisc" looks rather cryptic for me. Maybe "msb_is_signaling" as a value? Or what about some boolean flag like nan_msb_is_signaling?

Suggested change
#if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
PyModule_Add(m, "nan_encoding", PyUnicode_FromString("parisc"));
#else
PyModule_Add(m, "nan_encoding", PyUnicode_FromString("regular"));
#endif
#if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
PyModule_Add(m, "nan_msb_is_signaling", PyBool_FromLong(1));
#else
PyModule_Add(m, "nan_msb_is_signaling", PyBool_FromLong(0));
#endif

@skirpichev
Copy link
Contributor

@chenx97, could you check also test_pack_unpack_roundtrip_for_nans() in Lib/test/test_capi/test_float.py?

Is it enabled on your system? If so, it should fail too. If not, I suspect you could increase number of samples (10 for now) in this test.

chenx97 added a commit to AOSC-Tracking/cpython that referenced this pull request Jan 8, 2026
@chenx97
Copy link
Author

chenx97 commented Jan 8, 2026

@chenx97, could you check also test_pack_unpack_roundtrip_for_nans() in Lib/test/test_capi/test_float.py?

Is it enabled on your system? If so, it should fail too. If not, I suspect you could increase number of samples (10 for now) in this test.

I think the tests are expected to pass on mips64, as the parisc handling logic only runs for the 32-bit environment. Legacy mips32 fails here, as you guessed.

@skirpichev
Copy link
Contributor

the parisc handling logic only runs for the 32-bit environment

Ah, you are right. (I just did a quick grepping and didn't look on the function code.)

Still, I think we could use new module variable also here, instead of the platform check.

... and migrate another parisc detection to the new nan flag
Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. It's better with _testcapi.nan_msb_is_signaling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting merge needs backport to 3.14 bugs and security fixes skip news tests Tests in the Lib/test dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants