From af0f03cdc6c02ce400dbc34a47697632285b7d58 Mon Sep 17 00:00:00 2001 From: lntuition Date: Mon, 30 Sep 2019 20:22:18 +0900 Subject: [PATCH 1/6] Add testcase for int.__new__ Add testcase for int.__new__ --- tests/snippets/ints.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/snippets/ints.py b/tests/snippets/ints.py index 39923aba8..ef6995903 100644 --- a/tests/snippets/ints.py +++ b/tests/snippets/ints.py @@ -144,6 +144,10 @@ assert int('1 ') == 1 assert int(' 1 ') == 1 assert int('10', base=0) == 10 +assert int(x=10) == 10 +assert int(x='10') == 10 +assert int(x='10', base=0) == 10 + # type byte, signed, implied base assert int(b' -0XFF ', base=0) == -255 From 17ef308785ca553f702c817585d9e963326d5784 Mon Sep 17 00:00:00 2001 From: lntuition Date: Mon, 30 Sep 2019 20:23:33 +0900 Subject: [PATCH 2/6] Fix argument keyword error in int.__new__ Change property name and attributes of IntOptions Fixed: #1440 --- vm/src/obj/objint.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs index 0fd01b078..a907afe67 100644 --- a/vm/src/obj/objint.rs +++ b/vm/src/obj/objint.rs @@ -689,15 +689,15 @@ impl PyInt { #[derive(FromArgs)] struct IntOptions { - #[pyarg(positional_only, optional = true)] - val_options: OptionalArg, + #[pyarg(positional_or_keyword, optional = true)] + x: OptionalArg, #[pyarg(positional_or_keyword, optional = true)] base: OptionalArg, } impl IntOptions { fn get_int_value(self, vm: &VirtualMachine) -> PyResult { - if let OptionalArg::Present(val) = self.val_options { + if let OptionalArg::Present(val) = self.x { let base = if let OptionalArg::Present(base) = self.base { if !(objtype::isinstance(&val, &vm.ctx.str_type()) || objtype::isinstance(&val, &vm.ctx.bytes_type())) From 9cee128c6ff9f0efabb57c87f8a057b6faa96f52 Mon Sep 17 00:00:00 2001 From: lntuition Date: Mon, 30 Sep 2019 21:30:34 +0900 Subject: [PATCH 3/6] Add testcase for float.__new__ Add testcase for float.__new__ --- tests/snippets/floats.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/snippets/floats.py b/tests/snippets/floats.py index 73bc2bbc4..a247f9004 100644 --- a/tests/snippets/floats.py +++ b/tests/snippets/floats.py @@ -150,6 +150,8 @@ assert 1.2.__float__() == 1.2 assert 1.2.__trunc__() == 1 assert int(1.2) == 1 assert float(1.2) == 1.2 +assert float(x=1.2) == 1.2 +assert float(x='1.2') == 1.2 assert math.trunc(1.2) == 1 assert_raises(OverflowError, float('inf').__trunc__) assert_raises(ValueError, float('nan').__trunc__) From a209169d9cf0b629acc066f91bf3a51444c355a8 Mon Sep 17 00:00:00 2001 From: lntuition Date: Mon, 30 Sep 2019 21:31:18 +0900 Subject: [PATCH 4/6] Fix argument keyword error in float.__new__ Add FloatArgs struct to fix argument keyword error in float.__new --- vm/src/obj/objfloat.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/vm/src/obj/objfloat.rs b/vm/src/obj/objfloat.rs index 8fdd7af89..8e2946fd7 100644 --- a/vm/src/obj/objfloat.rs +++ b/vm/src/obj/objfloat.rs @@ -154,16 +154,18 @@ fn inner_gt_int(value: f64, other_int: &BigInt) -> bool { } } +#[derive(FromArgs)] +struct FloatArgs { + #[pyarg(positional_or_keyword, optional = true)] + x: OptionalArg, +} + #[pyimpl] #[allow(clippy::trivially_copy_pass_by_ref)] impl PyFloat { #[pymethod(name = "__new__")] - fn float_new( - cls: PyClassRef, - arg: OptionalArg, - vm: &VirtualMachine, - ) -> PyResult { - let float_val = match arg { + fn float_new(cls: PyClassRef, arg: FloatArgs, vm: &VirtualMachine) -> PyResult { + let float_val = match arg.x { OptionalArg::Present(val) => to_float(vm, &val), OptionalArg::Missing => Ok(0f64), }; From 02135be992e545ccc7a88976a3255fcdf4b9925e Mon Sep 17 00:00:00 2001 From: lntuition Date: Wed, 2 Oct 2019 15:01:10 +0900 Subject: [PATCH 5/6] Add testcase for bool.__new__ Add testcase for bool.__new__ --- tests/snippets/bools.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/snippets/bools.py b/tests/snippets/bools.py index e3d824b5c..f51e8b205 100644 --- a/tests/snippets/bools.py +++ b/tests/snippets/bools.py @@ -16,6 +16,8 @@ assert not None assert bool() == False assert bool(1) == True +assert bool(x=True) == True +assert bool(x=False) == False assert bool({}) == False assert bool(NotImplemented) == True From a07daf322f75f06dee57ef8e92849e018ff178b0 Mon Sep 17 00:00:00 2001 From: lntuition Date: Wed, 2 Oct 2019 15:03:04 +0900 Subject: [PATCH 6/6] Fix argument keyword error in bool.__new__ Add BoolArgs struct to fix argument keyword error in bool.__new --- vm/src/obj/objbool.rs | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/vm/src/obj/objbool.rs b/vm/src/obj/objbool.rs index 039b4688d..28b6a701a 100644 --- a/vm/src/obj/objbool.rs +++ b/vm/src/obj/objbool.rs @@ -1,7 +1,7 @@ use num_bigint::Sign; use num_traits::Zero; -use crate::function::PyFuncArgs; +use crate::function::{OptionalArg, PyFuncArgs}; use crate::pyobject::{ IntoPyObject, PyContext, PyObjectRef, PyResult, TryFromObject, TypeProtocol, }; @@ -188,19 +188,16 @@ fn bool_rxor(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResul do_bool_xor(vm, &lhs, &rhs) } -fn bool_new(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - arg_check!( - vm, - args, - required = [(_zelf, Some(vm.ctx.type_type()))], - optional = [(val, None)] - ); - Ok(match val { - Some(val) => { - let bv = boolval(vm, val.clone())?; - vm.new_bool(bv) - } - None => vm.context().new_bool(false), +#[derive(FromArgs)] +struct BoolArgs { + #[pyarg(positional_or_keyword, optional = true)] + x: OptionalArg, +} + +fn bool_new(_cls: objtype::PyClassRef, args: BoolArgs, vm: &VirtualMachine) -> PyResult { + Ok(match args.x { + OptionalArg::Present(x) => vm.new_bool(boolval(vm, x.clone())?), + OptionalArg::Missing => vm.context().new_bool(false), }) }