commit
90bf482fa1
|
@ -35,6 +35,7 @@ You can get an instance of `PyRef` by `PyRef::new`, which does 3 things:
|
|||
You can use `PyRef` just like `&T`, because it implements `Deref<Target=T>`.
|
||||
```rust
|
||||
# use pyo3::prelude::*;
|
||||
# use pyo3::types::PyDict;
|
||||
#[pyclass]
|
||||
struct MyClass {
|
||||
num: i32,
|
||||
|
@ -76,7 +77,8 @@ struct MyClass {
|
|||
}
|
||||
fn return_myclass() -> Py<MyClass> {
|
||||
let gil = Python::acquire_gil();
|
||||
Py::new(|| MyClass { num: 1 })
|
||||
let py = gil.python();
|
||||
Py::new(py, MyClass { num: 1 }).unwrap()
|
||||
}
|
||||
let gil = Python::acquire_gil();
|
||||
let obj = return_myclass();
|
||||
|
@ -144,8 +146,8 @@ Rules for the `new` method:
|
|||
|
||||
By default `PyObject` is used as default base class. To override default base class
|
||||
`base` parameter for `class` needs to be used. Value is full path to base class.
|
||||
`__new__` method accepts `PyRawObject` object. `obj` instance must be initialized
|
||||
with value of custom class struct. Subclass must call parent's `__new__` method.
|
||||
`new` method accepts `PyRawObject` object. `obj` instance must be initialized
|
||||
with value of custom class struct. Subclass must call parent's `new` method.
|
||||
|
||||
```rust
|
||||
# use pyo3::prelude::*;
|
||||
|
@ -345,6 +347,7 @@ with`#[classmethod]` attribute.
|
|||
|
||||
```rust
|
||||
# use pyo3::prelude::*;
|
||||
# use pyo3::types::PyType;
|
||||
# #[pyclass]
|
||||
# struct MyClass {
|
||||
# num: i32,
|
||||
|
@ -543,6 +546,8 @@ as every cycle must contain at least one mutable reference.
|
|||
Example:
|
||||
```rust
|
||||
use pyo3::prelude::*;
|
||||
use pyo3::PyTraverseError;
|
||||
use pyo3::gc::{PyGCProtocol, PyVisit};
|
||||
|
||||
#[pyclass]
|
||||
struct ClassWithGCSupport {
|
||||
|
@ -561,7 +566,9 @@ impl PyGCProtocol for ClassWithGCSupport {
|
|||
fn __clear__(&mut self) {
|
||||
if let Some(obj) = self.obj.take() {
|
||||
// Release reference, this decrements ref counter.
|
||||
self.py().release(obj);
|
||||
let gil = GILGuard::acquire();
|
||||
let py = gil.python();
|
||||
py.release(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -596,11 +603,11 @@ struct MyIterator {
|
|||
|
||||
#[pyproto]
|
||||
impl PyIterProtocol for MyIterator {
|
||||
fn __iter__(&mut self) -> PyResult<PyObject> {
|
||||
Ok(self.into())
|
||||
fn __iter__(slf: PyRefMut<Self>) -> PyResult<Py<MyIterator>> {
|
||||
Ok(slf.into())
|
||||
}
|
||||
fn __next__(&mut self) -> PyResult<Option<PyObject>> {
|
||||
Ok(self.iter.next())
|
||||
fn __next__(mut slf: PyRefMut<Self>) -> PyResult<Option<PyObject>> {
|
||||
Ok(slf.iter.next())
|
||||
}
|
||||
}
|
||||
```
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
You can use the `create_exception!` macro to define a new exception type:
|
||||
|
||||
```rust
|
||||
use pyo3::import_exception;
|
||||
use pyo3::create_exception;
|
||||
|
||||
create_exception!(module, MyError, pyo3::exceptions::Exception);
|
||||
```
|
||||
|
@ -40,7 +40,8 @@ fn main() {
|
|||
To raise an exception, first you need to obtain an exception type and construct a new [`PyErr`](https://docs.rs/pyo3/0.2.7/struct.PyErr.html), then call [`PyErr::restore()`](https://docs.rs/pyo3/0.2.7/struct.PyErr.html#method.restore) method to write the exception back to the Python interpreter's global state.
|
||||
|
||||
```rust
|
||||
use pyo3::{Python, PyErr, exc};
|
||||
use pyo3::{Python, PyErr};
|
||||
use pyo3::exceptions;
|
||||
|
||||
fn main() {
|
||||
let gil = Python::acquire_gil();
|
||||
|
@ -63,6 +64,7 @@ has corresponding rust type, exceptions defined by `create_exception!` and `impo
|
|||
have rust type as well.
|
||||
|
||||
```rust
|
||||
# use pyo3::exceptions;
|
||||
# use pyo3::prelude::*;
|
||||
# fn check_for_error() -> bool {false}
|
||||
fn my_func(arg: PyObject) -> PyResult<()> {
|
||||
|
@ -80,7 +82,8 @@ Python has an [`isinstance`](https://docs.python.org/3/library/functions.html#is
|
|||
in `PyO3` there is a [`Python::is_instance()`](https://docs.rs/pyo3/0.2.7/struct.Python.html#method.is_instance) method which does the same thing.
|
||||
|
||||
```rust
|
||||
use pyo3::{Python, PyBool, PyList};
|
||||
use pyo3::Python;
|
||||
use pyo3::types::{PyBool, PyList};
|
||||
|
||||
fn main() {
|
||||
let gil = Python::acquire_gil();
|
||||
|
@ -97,6 +100,7 @@ fn main() {
|
|||
To check the type of an exception, you can simply do:
|
||||
|
||||
```rust
|
||||
# use pyo3::exceptions;
|
||||
# use pyo3::prelude::*;
|
||||
# fn main() {
|
||||
# let gil = Python::acquire_gil();
|
||||
|
@ -176,7 +180,7 @@ fn tell(file: PyObject) -> PyResult<u64> {
|
|||
let py = gil.python();
|
||||
|
||||
match file.call_method0(py, "tell") {
|
||||
Err(_) => Err(UnsupportedOperation::new("not supported: tell")),
|
||||
Err(_) => Err(UnsupportedOperation::py_err("not supported: tell")),
|
||||
Ok(x) => x.extract::<u64>(py),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -60,6 +60,8 @@ built-ins are new in Python 3 — in Python 2, it is simply considered to be par
|
|||
of the doc-string.
|
||||
|
||||
```rust
|
||||
use pyo3::prelude::*;
|
||||
|
||||
/// add(a, b, /)
|
||||
/// --
|
||||
///
|
||||
|
|
|
@ -52,27 +52,36 @@ Which means that the above Python code will print `This module is implemented in
|
|||
In python, modules are first class objects. This means can store them as values or add them to dicts or other modules:
|
||||
|
||||
```rust
|
||||
use pyo3::prelude::*;
|
||||
use pyo3::{wrap_pyfunction, wrap_pymodule};
|
||||
use pyo3::types::PyDict;
|
||||
|
||||
#[pyfunction]
|
||||
#[cfg(Py_3)]
|
||||
fn subfunction() -> String {
|
||||
"Subfunction".to_string()
|
||||
}
|
||||
|
||||
#[pymodule]
|
||||
#[cfg(Py_3)]
|
||||
fn submodule(_py: Python, module: &PyModule) -> PyResult<()> {
|
||||
module.add_wrapped(wrap_pyfunction!(subfunction))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[pymodule]
|
||||
#[cfg(Py_3)]
|
||||
fn supermodule(_py: Python, module: &PyModule) -> PyResult<()> {
|
||||
module.add_wrapped(wrap_pymodule!(submodule))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(Py_3)]
|
||||
fn nested_call() {
|
||||
let gil = GILGuard::acquire();
|
||||
let py = gil.python();
|
||||
let supermodule = wrap_pymodule!(supermodule)(py);
|
||||
let ctx = PyDict::new(py);
|
||||
ctx.set_item("supermodule", supermodule);
|
||||
|
||||
py.run("assert supermodule.submodule.subfuntion() == 'Subfunction'", None, Some(&ctx)).unwrap();
|
||||
|
|
Loading…
Reference in a new issue