Raise ValueError consistently for negative int -> unsigned conversions#6196
Open
teddytennant wants to merge 4 commits into
Open
Raise ValueError consistently for negative int -> unsigned conversions#6196teddytennant wants to merge 4 commits into
teddytennant wants to merge 4 commits into
Conversation
Merging this PR will degrade performance by 12.26%
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing |
The negative-int-to-unsigned conversion now raises ValueError instead of OverflowError, so update the expected traceback in test_conversion_error.
Negative values for unsigned time components now raise ValueError (consistent with newer CPython int-conversion APIs), so rename test_invalid_time_fails_overflow accordingly.
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.
Extracting a negative Python
intinto an unsigned Rust integer previously raised an inconsistent exception type depending on the width and the Python version:OverflowErrorforu8/u16/u32(try_fromafterPyLong_AsLong) andu64(PyLong_AsUnsignedLongLong), butValueErrorforu128on 3.13+.This standardizes on
ValueErrorfor the negative case across every unsigned width (u8–u128,usize, and theNonZeroU*variants), matching the convention of newer CPython APIs such asPyLong_AsUInt64, as suggested in the issue. Genuine magnitude overflow of an in-range-sign value continues to raiseOverflowError.The remap is gated on
<$rust_type>::MIN == 0(and, in the 128-bit path, the existing$is_signed), and only fires on the already-cleared error path after confirming the value is negative, so signed types and too-large positive values are unaffected.Adds the
test_negativecase totest_common!so it runs for all integer widths (signed types still accept-1; unsigned types must raiseValueError).Fixes #6116.