1
1
/// This is the module that has all the executor functions
2
2
/// i.e. the functions that have the responsibility of parsing and executing functions.
3
- use crate :: types:: { FunctionInfo , Request , Response } ;
4
-
5
3
use std:: sync:: Arc ;
6
4
7
- use anyhow:: { anyhow , Context , Result } ;
5
+ use anyhow:: { Context , Result } ;
8
6
use log:: debug;
7
+ use pyo3:: prelude:: * ;
9
8
use pyo3_asyncio:: TaskLocals ;
10
- // pyO3 module
11
- use pyo3 :: { prelude :: * , PyClass } ;
9
+
10
+ use crate :: types :: { function_info :: FunctionInfo , request :: Request , response :: Response } ;
12
11
13
12
fn get_function_output < ' a , T > (
14
13
function : & ' a FunctionInfo ,
15
14
py : Python < ' a > ,
16
15
input : & T ,
17
16
) -> Result < & ' a PyAny , PyErr >
18
17
where
19
- T : Clone + IntoPy < Py < PyAny > > ,
18
+ T : ToPyObject ,
20
19
{
21
20
let handler = function. handler . as_ref ( py) ;
22
21
23
22
// this makes the request object accessible across every route
24
23
match function. number_of_params {
25
24
0 => handler. call0 ( ) ,
26
- 1 => handler. call1 ( ( input. clone ( ) , ) ) ,
25
+ 1 => handler. call1 ( ( input. to_object ( py ) , ) ) ,
27
26
// this is done to accommodate any future params
28
- 2_u8 ..=u8:: MAX => handler. call1 ( ( input. clone ( ) , ) ) ,
27
+ 2_u8 ..=u8:: MAX => handler. call1 ( ( input. to_object ( py ) , ) ) ,
29
28
}
30
29
}
31
30
32
- pub async fn execute_middleware_function < ' a , T > ( input : & T , function : FunctionInfo ) -> Result < T >
31
+ pub async fn execute_middleware_function < T > ( input : & T , function : FunctionInfo ) -> Result < T >
33
32
where
34
- T : Clone + PyClass + IntoPy < Py < PyAny > > ,
33
+ T : for < ' a > FromPyObject < ' a > + ToPyObject ,
35
34
{
36
35
if function. is_async {
37
- let output = Python :: with_gil ( |py| {
36
+ let output: Py < PyAny > = Python :: with_gil ( |py| {
38
37
pyo3_asyncio:: tokio:: into_future ( get_function_output ( & function, py, input) ?)
39
38
} ) ?
40
39
. await ?;
41
40
42
- Python :: with_gil ( |py| -> PyResult < T > {
43
- let output: ( T , ) = output. extract ( py) ?;
41
+ Python :: with_gil ( |py| -> Result < T > {
42
+ let output: ( T , ) = output
43
+ . extract ( py)
44
+ . context ( "Failed to get middleware response" ) ?;
44
45
Ok ( output. 0 )
45
46
} )
46
- . map_err ( |e| anyhow ! ( e) )
47
47
} else {
48
- Python :: with_gil ( |py| -> PyResult < T > {
49
- let output: ( T , ) = get_function_output ( & function, py, input) ?. extract ( ) ?;
48
+ Python :: with_gil ( |py| -> Result < T > {
49
+ let output: ( T , ) = get_function_output ( & function, py, input) ?
50
+ . extract ( )
51
+ . context ( "Failed to get middleware response" ) ?;
50
52
Ok ( output. 0 )
51
53
} )
52
- . map_err ( |e| anyhow ! ( e) )
53
54
}
54
55
}
55
56
@@ -67,8 +68,9 @@ pub async fn execute_http_function(request: &Request, function: FunctionInfo) ->
67
68
} )
68
69
} else {
69
70
Python :: with_gil ( |py| -> Result < Response > {
70
- let output = get_function_output ( & function, py, request) ?;
71
- output. extract ( ) . context ( "Failed to get route response" )
71
+ get_function_output ( & function, py, request) ?
72
+ . extract ( )
73
+ . context ( "Failed to get route response" )
72
74
} )
73
75
}
74
76
}
0 commit comments