negative-to-unsigned conversions are inconsistent between `ValueError` and `OverflowError`
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 16.2k
- Forks
- 1k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 66
Description
The following test fails inconsistently on various Python versions. See also https://github.com/PyO3/pyo3/pull/6016#discussion_r3230711385
diff --git a/src/conversions/std/num.rs b/src/conversions/std/num.rs
index 2fca7aa9f..207249246 100644
--- a/src/conversions/std/num.rs
+++ b/src/conversions/std/num.rs
@@ -920,6 +920,17 @@ mod tests {
let obj = val.into_pyobject(py).unwrap();
assert_eq!(obj.extract::<$t>().unwrap(), val as $t);});
}
+
+ #[test]
+ fn test_negative() {
+ Python::attach(|py| {
+ let obj = py.eval(c"-1", None, None).unwrap();
+ match obj.extract::<$t>() {
+ Ok(val) => assert_eq!(val, <$t>::try_from(-1i8).unwrap()),
+ Err(err) => assert!(err.is_instance_of::<exceptions::PyValueError>(py)),
+ };
+ });
+ }
}
)
);
Note that older CPython APIs such as PyLong_AsUnsignedLong use OverflowError, and newer APIs such as PyLong_AsUInt64 use ValueError.
I think we should match the convention of the newer CPython APIs and use ValueError as the error message. However there is possibly an argument that we should instead accept this divergence and have a clean split between (say) 3.14+ using ValueError and older versions using OverflowError.
See also #5179 which is related here.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/conversions/std/num.rs and review the negative-value extraction test shown in the issue, then read the linked pull request discussion and related issue #5179. Compare the relevant older and newer CPython conversion APIs; done means the project’s chosen ValueError or OverflowError convention is applied consistently and the test passes across supported Python versions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, rust
- Domain
- api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100