-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix int type casting error with negative base value #1406
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
Conversation
I want to test
|
Try running it from |
I try with above method. but result is same.
|
Hmm, odd. And is just running |
vm/src/obj/objint.rs
Outdated
@@ -704,7 +704,7 @@ struct IntOptions { | |||
#[pyarg(positional_only, optional = true)] | |||
val_options: OptionalArg<PyObjectRef>, | |||
#[pyarg(positional_or_keyword, optional = true)] | |||
base: OptionalArg<u32>, | |||
base: OptionalArg<i32>, |
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.
This doesn't completely fix the problem, it just shifts the range that the problem occurs, because if a PyInt can't fit into a i32
it still overflows. I recommend changing base
to be a PyIntRef and passing a &BigInt
around instead of an i32
.
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.
Thank you for reviewing. I Change base to PyIntRef
. And i also add test for minus base and Large value than max of i32.
Yeah. just running |
Try |
It's tricky because we're not actually running the test snippets with unittest/pytest, we have a custom loader |
|
dd81aea
to
188e485
Compare
Change base type from u32 to PyIntRef Fixed: RustPython#1405
@@ -105,7 +105,7 @@ where | |||
} | |||
|
|||
fn get_int(vm: &VirtualMachine, arg: &PyObjectRef) -> PyResult<BigInt> { | |||
objint::to_int(vm, arg, 10) | |||
objint::to_int(vm, arg, &BigInt::from(10)) |
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.
If you want to prevent using BigInt
here, you can change the signature of to_int
to accept an Into<BigInt>
instead of bigint itself. This will allow you to call the function with 10
and with a BigInt
value as well.
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.
@windelbouwman Thank you for reviewing. I agree that change signature of to_int
will make to_int
function more general.
But I have some questions. When we use base type to u32
, we can easily pass the argument because u32
have copy trait. And when we use base type to pyIntRef
, we can also pass reference of PyInt by as_bigint
method.
But If i changed signature of to_int
function. i cannot pass base to to_int
function. Because PyInt
does not have copy
trait. Just simple modifying make this error.
error[E0507]: cannot move out of dereference of `pyobject::PyRef<obj::objint::PyInt>`
--> vm/src/obj/objint.rs:721:34
|
721 | to_int(vm, &val, base.value)
| ^^^^^^^^^^ move occurs because value has type `num_bigint::bigint::BigInt`, which does not implement the `Copy` trait
error: aborting due to previous error
And also change base type to BigInt
occurs other error.
error[E0277]: the trait bound `num_bigint::bigint::BigInt: pyobject::TryFromObject` is not satisfied
--> vm/src/obj/objint.rs:702:10
|
702 | #[derive(FromArgs)]
| ^^^^^^^^ the trait `pyobject::TryFromObject` is not implemented for `num_bigint::bigint::BigInt`
|
::: vm/src/pyobject.rs:953:5
|
953 | fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self>;
| ---------------------------------------------------------------------------- required by `pyobject::TryFromObject::try_from_object`
Unfortunately, I'm not good at rust. so please give me an opinion. thx
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.
Ah, okay, the copy trait is important here. I'm not a rust wizard either, so I think the current situation is probably ok. Maybe other people could have a look at this?
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 think that the current solution is fine as well, passing an &BigInt
rather than an Into<BigInt>
removes the need for cloning as well.
Change base type from u32 to i32
After result
Fixed: #1405