1
1
use rustpython_vm:: {
2
- builtins:: PyStr ,
3
- function:: { FuncArgs , KwArgs , PosArgs } ,
4
- pyclass, pymodule, PyObject , PyObjectRef , PyPayload , PyResult , TryFromBorrowedObject ,
2
+ pyclass, pymodule, PyObject , PyPayload , PyResult , TryFromBorrowedObject ,
5
3
VirtualMachine ,
6
4
} ;
7
5
@@ -19,19 +17,10 @@ pub fn main() {
19
17
20
18
let module = vm. import ( "call_between_rust_and_python" , None , 0 ) . unwrap ( ) ;
21
19
let init_fn = module. get_attr ( "python_callback" , vm) . unwrap ( ) ;
22
-
23
20
vm. invoke ( & init_fn, ( ) ) . unwrap ( ) ;
24
21
25
- let pystr = PyObjectRef :: from ( PyStr :: new_ref (
26
- unsafe {
27
- PyStr :: new_ascii_unchecked ( String :: from ( "Rust string sent to python" ) . into_bytes ( ) )
28
- } ,
29
- vm. as_ref ( ) ,
30
- ) ) ;
31
- let take_string_args = FuncArgs :: new ( PosArgs :: new ( vec ! [ pystr] ) , KwArgs :: default ( ) ) ;
32
22
let take_string_fn = module. get_attr ( "take_string" , vm) . unwrap ( ) ;
33
-
34
- vm. invoke ( & take_string_fn, take_string_args) . unwrap ( ) ;
23
+ vm. invoke ( & take_string_fn, ( String :: from ( "Rust string sent to python" ) , ) ) . unwrap ( ) ;
35
24
} )
36
25
}
37
26
@@ -43,8 +32,8 @@ mod rust_py_module {
43
32
fn rust_function (
44
33
num : i32 ,
45
34
s : String ,
46
- python_person : PyObjectRef ,
47
- vm : & VirtualMachine ,
35
+ python_person : PythonPerson ,
36
+ _vm : & VirtualMachine ,
48
37
) -> PyResult < RustStruct > {
49
38
println ! (
50
39
"Calling standalone rust function from python passing args:
@@ -53,7 +42,7 @@ string: {},
53
42
python_person.name: {}" ,
54
43
num,
55
44
s,
56
- python_person. try_into_value :: < PythonPerson > ( vm ) . unwrap ( ) . name
45
+ python_person. name
57
46
) ;
58
47
Ok ( RustStruct )
59
48
}
@@ -78,10 +67,8 @@ python_person.name: {}",
78
67
impl TryFromBorrowedObject for PythonPerson {
79
68
fn try_from_borrowed_object ( vm : & VirtualMachine , obj : & PyObject ) -> PyResult < Self > {
80
69
let name = obj
81
- . get_attr ( "name" , vm)
82
- . unwrap ( )
83
- . try_into_value :: < String > ( vm)
84
- . unwrap ( ) ;
70
+ . get_attr ( "name" , vm) ?
71
+ . try_into_value :: < String > ( vm) ?;
85
72
Ok ( PythonPerson { name } )
86
73
}
87
74
}
0 commit comments