-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-143429: Use compile-time NaN encoding detection for test_struct #143432
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
base: main
Are you sure you want to change the base?
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
8ff4af9 to
0bbf71e
Compare
skirpichev
left a comment
There was a problem hiding this 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
Modules/_testcapimodule.c
Outdated
| PyModule_Add(m, "nan_encoding", PyUnicode_FromString("parisc")); | ||
| #else | ||
| PyModule_Add(m, "nan_encoding", PyUnicode_FromString("regular")); | ||
| #endif |
There was a problem hiding this comment.
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?
vstinner
left a comment
There was a problem hiding this 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.
Lib/test/test_struct.py
Outdated
| INF = float('inf') | ||
| NAN = float('nan') | ||
|
|
||
| _testcapi = import_helper.import_module('_testcapi') |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 = NoneAnd later check if _testcapi is None.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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().
There was a problem hiding this comment.
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...)
picnixz
left a comment
There was a problem hiding this 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.
|
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 |
Modules/_testcapimodule.c
Outdated
| #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 |
There was a problem hiding this comment.
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?
| #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 |
|
@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. |
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
vstinner
left a comment
There was a problem hiding this 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.
Description
This PR uses C macros to detect NaN encoding schemes for Lib/test/test_struct.py, instead of detecting the host machine.
math.nandepends on toolchain settings for a MIPS build, as setting-mnan=legacyand-mnan=2008result in different__builtin_nanbehavior for GCC.Tests