11use 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 ,
53 VirtualMachine ,
64} ;
75
@@ -19,19 +17,10 @@ pub fn main() {
1917
2018 let module = vm. import ( "call_between_rust_and_python" , None , 0 ) . unwrap ( ) ;
2119 let init_fn = module. get_attr ( "python_callback" , vm) . unwrap ( ) ;
22-
2320 vm. invoke ( & init_fn, ( ) ) . unwrap ( ) ;
2421
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 ( ) ) ;
3222 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 ( ) ;
3524 } )
3625}
3726
@@ -43,8 +32,8 @@ mod rust_py_module {
4332 fn rust_function (
4433 num : i32 ,
4534 s : String ,
46- python_person : PyObjectRef ,
47- vm : & VirtualMachine ,
35+ python_person : PythonPerson ,
36+ _vm : & VirtualMachine ,
4837 ) -> PyResult < RustStruct > {
4938 println ! (
5039 "Calling standalone rust function from python passing args:
@@ -53,7 +42,7 @@ string: {},
5342python_person.name: {}" ,
5443 num,
5544 s,
56- python_person. try_into_value :: < PythonPerson > ( vm ) . unwrap ( ) . name
45+ python_person. name
5746 ) ;
5847 Ok ( RustStruct )
5948 }
@@ -78,10 +67,8 @@ python_person.name: {}",
7867 impl TryFromBorrowedObject for PythonPerson {
7968 fn try_from_borrowed_object ( vm : & VirtualMachine , obj : & PyObject ) -> PyResult < Self > {
8069 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) ?;
8572 Ok ( PythonPerson { name } )
8673 }
8774 }
0 commit comments