Skip to content

Commit e11285a

Browse files
authored
Merge pull request RustPython#1953 from limeburst/remove-arg-check
Remove remaining usages of arg_check!
2 parents 57289ff + 646feeb commit e11285a

File tree

4 files changed

+19
-120
lines changed

4 files changed

+19
-120
lines changed

Lib/test/test_bool.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,6 @@ def test_convert(self):
170170
self.assertIs(bool(""), False)
171171
self.assertIs(bool(), False)
172172

173-
# TODO: RUSTPYTHON
174-
@unittest.expectedFailure
175173
def test_keyword_args(self):
176174
with self.assertRaisesRegex(TypeError, 'keyword argument'):
177175
bool(x=10)

vm/src/builtins.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -716,11 +716,13 @@ fn builtin_setattr(
716716
// builtin_slice
717717

718718
fn builtin_sorted(vm: &VirtualMachine, mut args: PyFuncArgs) -> PyResult {
719-
arg_check!(vm, args, required = [(iterable, None)]);
720-
let items = vm.extract_elements(iterable)?;
719+
let iterable = args.take_positional();
720+
if iterable.is_none() {
721+
return Err(vm.new_type_error("sorted expected 1 arguments, got 0".to_string()));
722+
}
723+
let items = vm.extract_elements(&iterable.unwrap())?;
721724
let lst = vm.ctx.new_list(items);
722725

723-
args.shift();
724726
vm.call_method(&lst, "sort", args)?;
725727
Ok(lst)
726728
}

vm/src/macros.rs

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,3 @@
1-
// count number of tokens given as arguments.
2-
// see: https://danielkeep.github.io/tlborm/book/blk-counting.html
3-
#[macro_export]
4-
macro_rules! replace_expr {
5-
($_t:tt $sub:expr) => {
6-
$sub
7-
};
8-
}
9-
10-
#[macro_export]
11-
macro_rules! count_tts {
12-
($($tts:tt)*) => {0usize $(+ $crate::replace_expr!($tts 1usize))*};
13-
}
14-
15-
#[macro_export]
16-
macro_rules! type_check {
17-
($vm:ident, $args:ident, $arg_count:ident, $arg_name:ident, $arg_type:expr) => {
18-
// None indicates that we have no type requirement (i.e. we accept any type)
19-
if let Some(expected_type) = $arg_type {
20-
let arg = &$args.args[$arg_count];
21-
22-
if !$crate::obj::objtype::isinstance(arg, &expected_type) {
23-
use $crate::pyobject::TypeProtocol;
24-
25-
let arg_typ = arg.class();
26-
let expected_type_name = $vm.to_pystr(&expected_type)?;
27-
let actual_type = $vm.to_pystr(&arg_typ)?;
28-
return Err($vm.new_type_error(format!(
29-
"argument of type {} is required for parameter {} ({}) (got: {})",
30-
expected_type_name,
31-
$arg_count + 1,
32-
stringify!($arg_name),
33-
actual_type
34-
)));
35-
}
36-
}
37-
};
38-
}
39-
40-
#[macro_export]
41-
macro_rules! arg_check {
42-
( $vm: ident, $args:ident ) => {
43-
// Zero-arg case
44-
if $args.args.len() != 0 {
45-
return Err($vm.new_type_error(format!(
46-
"Expected no arguments (got: {})", $args.args.len())));
47-
}
48-
};
49-
( $vm: ident, $args:ident, required=[$( ($arg_name:ident, $arg_type:expr) ),*] ) => {
50-
$crate::arg_check!($vm, $args, required=[$( ($arg_name, $arg_type) ),*], optional=[]);
51-
};
52-
( $vm: ident, $args:ident, required=[$( ($arg_name:ident, $arg_type:expr) ),*], optional=[$( ($optional_arg_name:ident, $optional_arg_type:expr) ),*] ) => {
53-
let mut arg_count = 0;
54-
55-
// use macro magic to compile-time count number of required and optional arguments
56-
let minimum_arg_count = $crate::count_tts!($($arg_name)*);
57-
let maximum_arg_count = minimum_arg_count + $crate::count_tts!($($optional_arg_name)*);
58-
59-
// verify that the number of given arguments is right
60-
if $args.args.len() < minimum_arg_count || $args.args.len() > maximum_arg_count {
61-
let expected_str = if minimum_arg_count == maximum_arg_count {
62-
format!("{}", minimum_arg_count)
63-
} else {
64-
format!("{}-{}", minimum_arg_count, maximum_arg_count)
65-
};
66-
return Err($vm.new_type_error(format!(
67-
"Expected {} arguments (got: {})",
68-
expected_str,
69-
$args.args.len()
70-
)));
71-
};
72-
73-
// for each required parameter:
74-
// check if the type matches. If not, return with error
75-
// assign the arg to a variable
76-
$(
77-
$crate::type_check!($vm, $args, arg_count, $arg_name, $arg_type);
78-
let $arg_name = &$args.args[arg_count];
79-
#[allow(unused_assignments)]
80-
{
81-
arg_count += 1;
82-
}
83-
)*
84-
85-
// for each optional parameter, if there are enough positional arguments:
86-
// check if the type matches. If not, return with error
87-
// assign the arg to a variable
88-
$(
89-
let $optional_arg_name = if arg_count < $args.args.len() {
90-
$crate::type_check!($vm, $args, arg_count, $optional_arg_name, $optional_arg_type);
91-
let ret = Some(&$args.args[arg_count]);
92-
#[allow(unused_assignments)]
93-
{
94-
arg_count += 1;
95-
}
96-
ret
97-
} else {
98-
None
99-
};
100-
)*
101-
};
102-
}
103-
1041
#[macro_export]
1052
macro_rules! no_kwargs {
1063
( $vm: ident, $args:ident ) => {

vm/src/obj/objbool.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use num_bigint::Sign;
22
use num_traits::Zero;
33

4-
use crate::function::PyFuncArgs;
4+
use crate::function::{OptionalArg, PyFuncArgs};
55
use crate::pyobject::{
66
IdProtocol, IntoPyObject, PyClassImpl, PyContext, PyObjectRef, PyResult, TryFromObject,
77
TypeProtocol,
@@ -150,18 +150,20 @@ impl PyBool {
150150
}
151151

152152
#[pyslot]
153-
fn tp_new(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
154-
arg_check!(
155-
vm,
156-
args,
157-
required = [(_zelf, Some(vm.ctx.type_type()))],
158-
optional = [(val, None)]
159-
);
160-
let value = match val {
161-
Some(val) => boolval(vm, val.clone())?,
162-
None => false,
153+
fn tp_new(zelf: PyObjectRef, x: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult {
154+
if !objtype::isinstance(&zelf, &vm.ctx.type_type()) {
155+
let zelf_typ = zelf.class();
156+
let actual_type = vm.to_pystr(&zelf_typ)?;
157+
return Err(vm.new_type_error(format!(
158+
"requires a 'type' object but received a '{}'",
159+
actual_type
160+
)));
161+
}
162+
let val = match x {
163+
OptionalArg::Present(val) => boolval(vm, val.clone())?,
164+
OptionalArg::Missing => false,
163165
};
164-
Ok(vm.new_bool(value))
166+
Ok(vm.new_bool(val))
165167
}
166168
}
167169

0 commit comments

Comments
 (0)