Fix documents following the review comment
This commit is contained in:
parent
44f26f4bb9
commit
35851b7ff9
|
@ -121,7 +121,7 @@ fn main_(py: Python) -> PyResult<()> {
|
|||
}
|
||||
```
|
||||
|
||||
If you want to excute a code with multiple lines, you can use
|
||||
If you want to excute one or more statements, you can use
|
||||
[`Python::run`](https://pyo3.rs/master/doc/pyo3/struct.Python.html#method.run)
|
||||
or [`py_run!](https://pyo3.rs/master/doc/pyo3/macro.py_run.htm) macro.
|
||||
|
||||
|
|
|
@ -94,9 +94,17 @@ Example program displaying the value of `sys.version` and the current user name:
|
|||
use pyo3::prelude::*;
|
||||
use pyo3::types::IntoPyDict;
|
||||
|
||||
fn main() -> PyResult<()> {
|
||||
fn main() -> Result<(), ()> {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
main_(py).map_err(|e| {
|
||||
// We can't display python error type via ::std::fmt::Display,
|
||||
// so print error here manually.
|
||||
e.print_and_set_sys_last_vars(py);
|
||||
})
|
||||
}
|
||||
|
||||
fn main_(py: Python) -> PyResult<()> {
|
||||
let sys = py.import("sys")?;
|
||||
let version: String = sys.get("version")?.extract()?;
|
||||
let locals = [("os", py.import("os")?)].into_py_dict(py);
|
||||
|
@ -107,6 +115,10 @@ fn main() -> PyResult<()> {
|
|||
}
|
||||
```
|
||||
|
||||
If you want to excute one or more statements, you can use
|
||||
[`Python::run`](https://pyo3.rs/master/doc/pyo3/struct.Python.html#method.run)
|
||||
or [`py_run!](https://pyo3.rs/master/doc/pyo3/macro.py_run.htm) macro.
|
||||
|
||||
## Examples and tooling
|
||||
|
||||
* [examples/word-count](examples/word-count) _Counting the occurrences of a word in a text file_
|
||||
|
|
|
@ -106,10 +106,10 @@ impl<'p> Python<'p> {
|
|||
///
|
||||
/// # Example:
|
||||
/// ```
|
||||
/// use pyo3::{types::{IntoPyDict, PyBytes, PyDict}, prelude::*};
|
||||
/// use pyo3::{types::{PyBytes, PyDict}, prelude::*};
|
||||
/// let gil = pyo3::Python::acquire_gil();
|
||||
/// let py = gil.python();
|
||||
/// let locals = [("ret", py.None())].into_py_dict(py);
|
||||
/// let locals = PyDict::new(py);
|
||||
/// py.run(
|
||||
/// r#"
|
||||
/// import base64
|
||||
|
@ -118,7 +118,7 @@ impl<'p> Python<'p> {
|
|||
/// "#,
|
||||
/// None,
|
||||
/// Some(locals),
|
||||
/// );
|
||||
/// ).unwrap();
|
||||
/// let ret = locals.get_item("ret").unwrap();
|
||||
/// let b64: &PyBytes = ret.downcast_ref().unwrap();
|
||||
/// assert_eq!(b64.as_bytes(), b"SGVsbG8gUnVzdCE=");
|
||||
|
|
|
@ -77,23 +77,3 @@ fn new_with_two_args() {
|
|||
assert_eq!(obj._data1, 10);
|
||||
assert_eq!(obj._data2, 20);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn py_run_example() {
|
||||
use pyo3::types::{IntoPyDict, PyDict, PyList};
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
let ret_dict = [("ret", py.None())].into_py_dict(py);
|
||||
py.run(
|
||||
r#"
|
||||
l = [8, 7, 3, 4, 5]
|
||||
l.sort()
|
||||
ret = l
|
||||
"#,
|
||||
None,
|
||||
Some(ret_dict),
|
||||
);
|
||||
let list: &PyList = ret_dict.get_item("ret").unwrap().downcast_ref().unwrap();
|
||||
let ret: Vec<i32> = list.extract().unwrap();
|
||||
assert_eq!(&ret, &[3, 4, 5, 7, 8]);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue