@@ -62,32 +62,24 @@ numpy = "0.11.0"
6262```
6363
6464``` rust
65- use numpy :: { PyArray1 , get_array_module} ;
65+ use numpy :: PyArray1 ;
6666use pyo3 :: prelude :: {PyResult , Python };
67- use pyo3 :: types :: PyDict ;
68-
69- fn main () -> Result <(), ()> {
70- let gil = Python :: acquire_gil ();
71- main_ (gil . python ()). map_err (| e | {
72- eprintln! (" error! :{:?}" , e );
73- // we can't display python error type via ::std::fmt::Display
74- // so print error here manually
75- e . print_and_set_sys_last_vars (gil . python ());
67+ use pyo3 :: types :: IntoPyDict ;
68+
69+ fn main () -> PyResult <()> {
70+ Python :: with_gil (| py | {
71+ let np = py . import (" numpy" )? ;
72+ let locals = [(" np" , np )]. into_py_dict (py );
73+ let pyarray : & PyArray1 <i32 > = py
74+ . eval (" np.absolute(np.array([-1, -2, -3], dtype='int32'))" , Some (locals ), None )?
75+ . extract ()? ;
76+ let readonly = pyarray . readonly ();
77+ let slice = readonly . as_slice ()? ;
78+ assert_eq! (slice , & [1 , 2 , 3 ]);
79+ Ok (())
7680 })
7781}
7882
79- fn main_ <'py >(py : Python <'py >) -> PyResult <()> {
80- let np = py . import (" numpy" )? ;
81- let dict = PyDict :: new (py );
82- dict . set_item (" np" , np )? ;
83- let pyarray : & PyArray1 <i32 > = py
84- . eval (" np.absolute(np.array([-1, -2, -3], dtype='int32'))" , Some (& dict ), None )?
85- . extract ()? ;
86- let readonly = pyarray . readonly ();
87- let slice = readonly . as_slice (). unwrap ();
88- assert_eq! (slice , & [1 , 2 , 3 ]);
89- Ok (())
90- }
9183```
9284
9385### Write a Python module in Rust
0 commit comments