@@ -28,15 +28,15 @@ pub fn py_init(fnname: &Ident, name: &Ident, doc: syn::LitStr) -> TokenStream {
28
28
}
29
29
30
30
/// Finds and takes care of the #[pyfn(...)] in `#[pymodule]`
31
- pub fn process_functions_in_module ( func : & mut syn:: ItemFn ) {
31
+ pub fn process_functions_in_module ( func : & mut syn:: ItemFn ) -> syn :: Result < ( ) > {
32
32
let mut stmts: Vec < syn:: Stmt > = Vec :: new ( ) ;
33
33
34
34
for stmt in func. block . stmts . iter_mut ( ) {
35
35
if let syn:: Stmt :: Item ( syn:: Item :: Fn ( ref mut func) ) = stmt {
36
36
if let Some ( ( module_name, python_name, pyfn_attrs) ) =
37
37
extract_pyfn_attrs ( & mut func. attrs )
38
38
{
39
- let function_to_python = add_fn_to_module ( func, python_name, pyfn_attrs) ;
39
+ let function_to_python = add_fn_to_module ( func, python_name, pyfn_attrs) ? ;
40
40
let function_wrapper_ident = function_wrapper_ident ( & func. sig . ident ) ;
41
41
let item: syn:: ItemFn = syn:: parse_quote! {
42
42
fn block_wrapper( ) {
@@ -51,26 +51,27 @@ pub fn process_functions_in_module(func: &mut syn::ItemFn) {
51
51
}
52
52
53
53
func. block . stmts = stmts;
54
+ Ok ( ( ) )
54
55
}
55
56
56
57
/// Transforms a rust fn arg parsed with syn into a method::FnArg
57
- fn wrap_fn_argument < ' a > ( cap : & ' a syn:: PatType , name : & ' a Ident ) -> method:: FnArg < ' a > {
58
+ fn wrap_fn_argument < ' a > ( cap : & ' a syn:: PatType , name : & ' a Ident ) -> syn :: Result < method:: FnArg < ' a > > {
58
59
let ( mutability, by_ref, ident) = match * cap. pat {
59
60
syn:: Pat :: Ident ( ref patid) => ( & patid. mutability , & patid. by_ref , & patid. ident ) ,
60
- _ => panic ! ( "unsupported argument: {:?}" , cap. pat) ,
61
+ _ => return Err ( syn :: Error :: new_spanned ( & cap. pat , "Unsupported argument" ) ) ,
61
62
} ;
62
63
63
64
let py = crate :: utils:: if_type_is_python ( & cap. ty ) ;
64
65
let opt = method:: check_arg_ty_and_optional ( & name, & cap. ty ) ;
65
- method:: FnArg {
66
+ Ok ( method:: FnArg {
66
67
name : ident,
67
68
mutability,
68
69
by_ref,
69
70
ty : & cap. ty ,
70
71
optional : opt,
71
72
py,
72
73
reference : method:: is_ref ( & name, & cap. ty ) ,
73
- }
74
+ } )
74
75
}
75
76
76
77
/// Extracts the data from the #[pyfn(...)] attribute of a function
@@ -131,7 +132,7 @@ pub fn add_fn_to_module(
131
132
func : & mut syn:: ItemFn ,
132
133
python_name : Ident ,
133
134
pyfn_attrs : Vec < pyfunction:: Argument > ,
134
- ) -> TokenStream {
135
+ ) -> syn :: Result < TokenStream > {
135
136
let mut arguments = Vec :: new ( ) ;
136
137
let mut self_ = None ;
137
138
@@ -141,21 +142,15 @@ pub fn add_fn_to_module(
141
142
self_ = Some ( recv. mutability . is_some ( ) ) ;
142
143
}
143
144
syn:: FnArg :: Typed ( ref cap) => {
144
- arguments. push ( wrap_fn_argument ( cap, & func. sig . ident ) ) ;
145
+ arguments. push ( wrap_fn_argument ( cap, & func. sig . ident ) ? ) ;
145
146
}
146
147
}
147
148
}
148
149
149
150
let ty = method:: get_return_info ( & func. sig . output ) ;
150
151
151
- let text_signature = match utils:: parse_text_signature_attrs ( & mut func. attrs , & python_name) {
152
- Ok ( text_signature) => text_signature,
153
- Err ( err) => return err. to_compile_error ( ) ,
154
- } ;
155
- let doc = match utils:: get_doc ( & func. attrs , text_signature, true ) {
156
- Ok ( doc) => doc,
157
- Err ( err) => return err. to_compile_error ( ) ,
158
- } ;
152
+ let text_signature = utils:: parse_text_signature_attrs ( & mut func. attrs , & python_name) ?;
153
+ let doc = utils:: get_doc ( & func. attrs , text_signature, true ) ?;
159
154
160
155
let function_wrapper_ident = function_wrapper_ident ( & func. sig . ident ) ;
161
156
@@ -176,7 +171,7 @@ pub fn add_fn_to_module(
176
171
177
172
let wrapper = function_c_wrapper ( & func. sig . ident , & spec) ;
178
173
179
- let tokens = quote ! {
174
+ Ok ( quote ! {
180
175
fn #function_wrapper_ident( py: pyo3:: Python ) -> pyo3:: PyObject {
181
176
#wrapper
182
177
@@ -199,9 +194,7 @@ pub fn add_fn_to_module(
199
194
200
195
function
201
196
}
202
- } ;
203
-
204
- tokens
197
+ } )
205
198
}
206
199
207
200
/// Generate static function wrapper (PyCFunction, PyCFunctionWithKeywords)
0 commit comments