Skip to content

Commit 4dd0304

Browse files
committed
Add check_but_allow_none macro for optional argument checking.
1 parent c5789a0 commit 4dd0304

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

vm/src/macros.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,34 @@ macro_rules! type_check {
3535
};
3636
}
3737

38+
#[macro_export]
39+
macro_rules! check_but_allow_none {
40+
( $vm: ident, $arg: ident, $expected_type: expr ) => {
41+
let $arg = match $arg {
42+
Some(arg) => {
43+
if arg.is(&$vm.get_none()) {
44+
None
45+
} else {
46+
if $crate::obj::objtype::real_isinstance($vm, arg, &$expected_type)? {
47+
Some(arg)
48+
} else {
49+
let arg_typ = arg.typ();
50+
let actual_type = $vm.to_pystr(&arg_typ)?;
51+
let expected_type_name = $vm.to_pystr(&$expected_type)?;
52+
return Err($vm.new_type_error(format!(
53+
"{} must be a {}, not {}",
54+
stringify!($arg),
55+
expected_type_name,
56+
actual_type
57+
)));
58+
}
59+
}
60+
}
61+
None => None,
62+
};
63+
};
64+
}
65+
3866
#[macro_export]
3967
macro_rules! arg_check {
4068
( $vm: ident, $args:ident ) => {

0 commit comments

Comments
 (0)