Skip to content

Commit 65f6b97

Browse files
committed
adding number of args check and avoiding compiler warnings
1 parent 3709986 commit 65f6b97

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/py_lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,28 @@ pub fn register_function_str(fn_name: String, number_of_args: Option<u8>) -> cra
6363
.globals
6464
.get_item(&fn_name, vm)
6565
.expect(&format!("Function {fn_name} not found"));
66+
67+
if let Some(num_args) = number_of_args {
68+
let py_analyze_sig = format!(
69+
r#"
70+
from inspect import signature
71+
if len(signature({}).parameters) != {}:
72+
raise Exception("Function parameters don't match in 'registerFunction'")
73+
"#,
74+
fn_name, num_args
75+
);
76+
77+
let code_obj = vm
78+
.compile(
79+
&py_analyze_sig,
80+
rustpython_vm::compiler::Mode::Exec,
81+
"<embedded>".to_owned(),
82+
)
83+
.map_err(|err| vm.new_syntax_error(&err, Some(&py_analyze_sig)))?;
84+
vm.run_code_obj(code_obj, GLOBALS.clone()).expect(&format!(
85+
"Number of args doesn't match signature of {fn_name}."
86+
));
87+
}
6688
FUNCTION_MAP.lock().unwrap().insert(fn_name);
6789
Ok(())
6890
})

src/py_lib_pyo3.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,10 @@ pub fn register_function_str(fn_name: String, number_of_args: Option<u8>) -> cra
7676
if let Some(num_args) = number_of_args {
7777
let py_analyze_sig = format!(
7878
r#"
79-
if True:
80-
from inspect import signature
81-
if len(signature({}).parameters) != {}:
82-
raise Exception("Function parameters don't match in 'registerFunction'")
83-
"#,
79+
from inspect import signature
80+
if len(signature({}).parameters) != {}:
81+
raise Exception("Function parameters don't match in 'registerFunction'")
82+
"#,
8483
fn_name, num_args
8584
);
8685
let code_c = CString::new(py_analyze_sig).expect("CString::new failed");

0 commit comments

Comments
 (0)