Register Boost.Python converters for NumPy 2.0 scalar types#1131
Open
dwpaley wants to merge 4 commits into
Open
Register Boost.Python converters for NumPy 2.0 scalar types#1131dwpaley wants to merge 4 commits into
dwpaley wants to merge 4 commits into
Conversation
Member
|
/azp run FULL |
|
Azure Pipelines successfully started running 1 pipeline(s). |
Workaround for boostorg/python#511 NumPy 2.0 scalar types (np.float32, np.int32, np.int64, etc.) no longer subclass Python's built-in float/int, so Boost.Python's rvalue converters fail to convert them. Register custom from-Python converters that use the PyNumber_Float/PyNumber_Long protocols instead. Fixes #1084
6d0f610 to
2cdaf5b
Compare
bkpoon
reviewed
Apr 14, 2026
bkpoon
left a comment
Member
There was a problem hiding this comment.
Integer overflow detection
The current PyLong_AsLong + static_cast<CppType> pattern silently truncates values that don't fit the target type (e.g. np.int64(2147483648) assigned to flex.int silently stores a wrong value). PyLong_AsLong does not set an error on overflow — it truncates to C long range.
Fix: Use PyLong_AsLongLong to get the full value, then explicitly range-check against std::numeric_limits<CppType> before casting. This raises OverflowError for out-of-range values instead of silently corrupting data.
Three suggested changes:
- Add
#include <limits>forstd::numeric_limits - Replace the integer converter body with overflow-safe version
- Add overflow boundary tests
Co-authored-by: Billy K. Poon <bkpoon@lbl.gov>
Co-authored-by: Billy K. Poon <bkpoon@lbl.gov>
Co-authored-by: Billy K. Poon <bkpoon@lbl.gov>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Workaround for boostorg/python#511
NumPy 2.0 scalar types (np.float32, np.int32, np.int64, etc.) no longer subclass Python's built-in float/int, so Boost.Python's rvalue converters fail to convert them. Register custom from-Python converters that use the PyNumber_Float/PyNumber_Long protocols instead.
Fixes #1084