Skip to content

Commit

Permalink
Merge pull request RustPython#1953 from limeburst/remove-arg-check
Browse files Browse the repository at this point in the history
Remove remaining usages of arg_check!
  • Loading branch information
coolreader18 authored Jun 30, 2020
2 parents 57289ff + 646feeb commit e11285a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 120 deletions.
2 changes: 0 additions & 2 deletions Lib/test/test_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ def test_convert(self):
self.assertIs(bool(""), False)
self.assertIs(bool(), False)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_keyword_args(self):
with self.assertRaisesRegex(TypeError, 'keyword argument'):
bool(x=10)
Expand Down
8 changes: 5 additions & 3 deletions vm/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,11 +716,13 @@ fn builtin_setattr(
// builtin_slice

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

args.shift();
vm.call_method(&lst, "sort", args)?;
Ok(lst)
}
Expand Down
103 changes: 0 additions & 103 deletions vm/src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,106 +1,3 @@
// count number of tokens given as arguments.
// see: https://danielkeep.github.io/tlborm/book/blk-counting.html
#[macro_export]
macro_rules! replace_expr {
($_t:tt $sub:expr) => {
$sub
};
}

#[macro_export]
macro_rules! count_tts {
($($tts:tt)*) => {0usize $(+ $crate::replace_expr!($tts 1usize))*};
}

#[macro_export]
macro_rules! type_check {
($vm:ident, $args:ident, $arg_count:ident, $arg_name:ident, $arg_type:expr) => {
// None indicates that we have no type requirement (i.e. we accept any type)
if let Some(expected_type) = $arg_type {
let arg = &$args.args[$arg_count];

if !$crate::obj::objtype::isinstance(arg, &expected_type) {
use $crate::pyobject::TypeProtocol;

let arg_typ = arg.class();
let expected_type_name = $vm.to_pystr(&expected_type)?;
let actual_type = $vm.to_pystr(&arg_typ)?;
return Err($vm.new_type_error(format!(
"argument of type {} is required for parameter {} ({}) (got: {})",
expected_type_name,
$arg_count + 1,
stringify!($arg_name),
actual_type
)));
}
}
};
}

#[macro_export]
macro_rules! arg_check {
( $vm: ident, $args:ident ) => {
// Zero-arg case
if $args.args.len() != 0 {
return Err($vm.new_type_error(format!(
"Expected no arguments (got: {})", $args.args.len())));
}
};
( $vm: ident, $args:ident, required=[$( ($arg_name:ident, $arg_type:expr) ),*] ) => {
$crate::arg_check!($vm, $args, required=[$( ($arg_name, $arg_type) ),*], optional=[]);
};
( $vm: ident, $args:ident, required=[$( ($arg_name:ident, $arg_type:expr) ),*], optional=[$( ($optional_arg_name:ident, $optional_arg_type:expr) ),*] ) => {
let mut arg_count = 0;

// use macro magic to compile-time count number of required and optional arguments
let minimum_arg_count = $crate::count_tts!($($arg_name)*);
let maximum_arg_count = minimum_arg_count + $crate::count_tts!($($optional_arg_name)*);

// verify that the number of given arguments is right
if $args.args.len() < minimum_arg_count || $args.args.len() > maximum_arg_count {
let expected_str = if minimum_arg_count == maximum_arg_count {
format!("{}", minimum_arg_count)
} else {
format!("{}-{}", minimum_arg_count, maximum_arg_count)
};
return Err($vm.new_type_error(format!(
"Expected {} arguments (got: {})",
expected_str,
$args.args.len()
)));
};

// for each required parameter:
// check if the type matches. If not, return with error
// assign the arg to a variable
$(
$crate::type_check!($vm, $args, arg_count, $arg_name, $arg_type);
let $arg_name = &$args.args[arg_count];
#[allow(unused_assignments)]
{
arg_count += 1;
}
)*

// for each optional parameter, if there are enough positional arguments:
// check if the type matches. If not, return with error
// assign the arg to a variable
$(
let $optional_arg_name = if arg_count < $args.args.len() {
$crate::type_check!($vm, $args, arg_count, $optional_arg_name, $optional_arg_type);
let ret = Some(&$args.args[arg_count]);
#[allow(unused_assignments)]
{
arg_count += 1;
}
ret
} else {
None
};
)*
};
}

#[macro_export]
macro_rules! no_kwargs {
( $vm: ident, $args:ident ) => {
Expand Down
26 changes: 14 additions & 12 deletions vm/src/obj/objbool.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use num_bigint::Sign;
use num_traits::Zero;

use crate::function::PyFuncArgs;
use crate::function::{OptionalArg, PyFuncArgs};
use crate::pyobject::{
IdProtocol, IntoPyObject, PyClassImpl, PyContext, PyObjectRef, PyResult, TryFromObject,
TypeProtocol,
Expand Down Expand Up @@ -150,18 +150,20 @@ impl PyBool {
}

#[pyslot]
fn tp_new(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(_zelf, Some(vm.ctx.type_type()))],
optional = [(val, None)]
);
let value = match val {
Some(val) => boolval(vm, val.clone())?,
None => false,
fn tp_new(zelf: PyObjectRef, x: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult {
if !objtype::isinstance(&zelf, &vm.ctx.type_type()) {
let zelf_typ = zelf.class();
let actual_type = vm.to_pystr(&zelf_typ)?;
return Err(vm.new_type_error(format!(
"requires a 'type' object but received a '{}'",
actual_type
)));
}
let val = match x {
OptionalArg::Present(val) => boolval(vm, val.clone())?,
OptionalArg::Missing => false,
};
Ok(vm.new_bool(value))
Ok(vm.new_bool(val))
}
}

Expand Down

0 comments on commit e11285a

Please sign in to comment.