Skip to content

Remove PyObject, rename PyAny -> PyObject #863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benches/bench_pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn drop_many_objects(b: &mut Bencher) {
let py = gil.python();
b.iter(|| {
for _ in 0..1000 {
std::mem::drop(py.None());
std::mem::drop(Py::from(py.None()));
}
});
}
6 changes: 3 additions & 3 deletions examples/rustapi_module/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn make_time<'p>(
minute,
second,
microsecond,
tzinfo.map(|o| o.to_object(py)).as_ref(),
tzinfo.map(|o| o.as_ref()),
)
}

Expand All @@ -59,7 +59,7 @@ fn time_with_fold<'p>(
minute,
second,
microsecond,
tzinfo.map(|o| o.to_object(py)).as_ref(),
tzinfo.map(|o| o.as_ref()),
fold,
)
}
Expand Down Expand Up @@ -135,7 +135,7 @@ fn make_datetime<'p>(
minute,
second,
microsecond,
tzinfo.map(|o| (o.to_object(py))).as_ref(),
tzinfo.map(|o| o.as_ref()),
)
}

Expand Down
6 changes: 3 additions & 3 deletions examples/rustapi_module/src/objstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use pyo3::prelude::*;
#[pyclass]
#[derive(Default)]
pub struct ObjStore {
obj: Vec<PyObject>,
obj: Vec<Py<PyObject>>,
}

#[pymethods]
Expand All @@ -13,8 +13,8 @@ impl ObjStore {
ObjStore::default()
}

fn push(&mut self, py: Python, obj: &PyAny) {
self.obj.push(obj.to_object(py));
fn push(&mut self, obj: &PyObject) {
self.obj.push(obj.into());
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/word-count/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn matches(word: &str, needle: &str) -> bool {
}
}
}
return needle.next().is_none();
needle.next().is_none()
}

/// Count the occurences of needle in line, case insensitive
Expand Down
2 changes: 1 addition & 1 deletion guide/src/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The C API is naturally unsafe and requires you to manage reference counts, error

## Memory Management

PyO3's "owned references" (`&PyAny` etc.) make PyO3 more ergonomic to use by ensuring that their lifetime can never be longer than the duration the Python GIL is held. This means that most of PyO3's API can assume the GIL is held. (If PyO3 could not assume this, every PyO3 API would need to take a `Python` GIL token to prove that the GIL is held.)
PyO3's "owned references" (`&PyObject` etc.) make PyO3 more ergonomic to use by ensuring that their lifetime can never be longer than the duration the Python GIL is held. This means that most of PyO3's API can assume the GIL is held. (If PyO3 could not assume this, every PyO3 API would need to take a `Python` GIL token to prove that the GIL is held.)

The caveat to these "owned references" is that Rust references do not normally convey ownership (they are always `Copy`, and cannot implement `Drop`). Whenever a PyO3 API returns an owned reference, PyO3 stores it internally, so that PyO3 can decrease the reference count just before PyO3 releases the GIL.

Expand Down
32 changes: 16 additions & 16 deletions guide/src/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Consult the table below to determine which type your constructor should return:

## Inheritance

By default, `PyAny` is used as the base class. To override this default,
By default, `PyObject` is used as the base class. To override this default,
use the `extends` parameter for `pyclass` with the full path to the base class.

For convenience, `(T, U)` implements `Into<PyClassInitializer<T>>` where `U` is the
Expand Down Expand Up @@ -271,7 +271,7 @@ impl DictWithCounter {
fn new() -> Self {
Self::default()
}
fn set(mut self_: PyRefMut<Self>, key: String, value: &PyAny) -> PyResult<()> {
fn set(mut self_: PyRefMut<Self>, key: String, value: &PyObject) -> PyResult<()> {
self_.counter.entry(key.clone()).or_insert(0);
let py = self_.py();
let dict: &PyDict = unsafe { py.from_borrowed_ptr_or_err(self_.as_ptr())? };
Expand Down Expand Up @@ -429,7 +429,7 @@ impl MyClass {
```

Calls to these methods are protected by the GIL, so both `&self` and `&mut self` can be used.
The return type must be `PyResult<T>` or `T` for some `T` that implements `IntoPy<PyObject>`;
The return type must be `PyResult<T>` or `T` for some `T` that implements `IntoPy<Py<PyObject>>`;
the latter is allowed if the method cannot raise Python exceptions.

A `Python` parameter can be specified as part of method signature, in this case the `py` argument
Expand Down Expand Up @@ -480,13 +480,13 @@ Declares a class method callable from Python.
This may be the type object of a derived class.
* The first parameter implicitly has type `&PyType`.
* For details on `parameter-list`, see the documentation of `Method arguments` section.
* The return type must be `PyResult<T>` or `T` for some `T` that implements `IntoPy<PyObject>`.
* The return type must be `PyResult<T>` or `T` for some `T` that implements `IntoPy<Py<PyObject>>`.

## Static methods

To create a static method for a custom class, the method needs to be annotated with the
`#[staticmethod]` attribute. The return type must be `T` or `PyResult<T>` for some `T` that implements
`IntoPy<PyObject>`.
`IntoPy<Py<PyObject>>`.

```rust
# use pyo3::prelude::*;
Expand Down Expand Up @@ -675,7 +675,7 @@ The [`PyObjectProtocol`] trait provides several basic customizations.

To customize object attribute access, define the following methods:

* `fn __getattr__(&self, name: FromPyObject) -> PyResult<impl IntoPy<PyObject>>`
* `fn __getattr__(&self, name: FromPyObject) -> PyResult<impl IntoPy<Py<PyObject>>>`
* `fn __setattr__(&mut self, name: FromPyObject, value: FromPyObject) -> PyResult<()>`
* `fn __delattr__(&mut self, name: FromPyObject) -> PyResult<()>`

Expand Down Expand Up @@ -740,7 +740,7 @@ use pyo3::gc::{PyGCProtocol, PyVisit};

#[pyclass]
struct ClassWithGCSupport {
obj: Option<PyObject>,
obj: Option<Py<PyObject>>,
}

#[pyproto]
Expand Down Expand Up @@ -781,8 +781,8 @@ struct GCTracked {} // Fails because it does not implement PyGCProtocol
Iterators can be defined using the
[`PyIterProtocol`](https://docs.rs/pyo3/latest/pyo3/class/iter/trait.PyIterProtocol.html) trait.
It includes two methods `__iter__` and `__next__`:
* `fn __iter__(slf: PyRefMut<Self>) -> PyResult<impl IntoPy<PyObject>>`
* `fn __next__(slf: PyRefMut<Self>) -> PyResult<Option<impl IntoPy<PyObject>>>`
* `fn __iter__(slf: PyRefMut<Self>) -> PyResult<impl IntoPy<Py<PyObject>>>`
* `fn __next__(slf: PyRefMut<Self>) -> PyResult<Option<impl IntoPy<Py<PyObject>>>>`

Returning `Ok(None)` from `__next__` indicates that that there are no further items.
These two methods can be take either `PyRef<Self>` or `PyRefMut<Self>` as their
Expand All @@ -797,15 +797,15 @@ use pyo3::PyIterProtocol;

#[pyclass]
struct MyIterator {
iter: Box<Iterator<Item = PyObject> + Send>,
iter: Box<Iterator<Item = Py<PyObject>> + Send>,
}

#[pyproto]
impl PyIterProtocol for MyIterator {
fn __iter__(slf: PyRef<Self>) -> PyResult<Py<MyIterator>> {
Ok(slf.into())
}
fn __next__(mut slf: PyRefMut<Self>) -> PyResult<Option<PyObject>> {
fn __next__(mut slf: PyRefMut<Self>) -> PyResult<Option<Py<PyObject>>> {
Ok(slf.iter.next())
}
}
Expand Down Expand Up @@ -894,8 +894,8 @@ impl pyo3::pyclass::PyClassAlloc for MyClass {}

unsafe impl pyo3::PyTypeInfo for MyClass {
type Type = MyClass;
type BaseType = PyAny;
type BaseLayout = pyo3::pycell::PyCellBase<PyAny>;
type BaseType = PyObject;
type BaseLayout = pyo3::pycell::PyCellBase<PyObject>;
type Layout = PyCell<Self>;
type Initializer = PyClassInitializer<Self>;
type AsRefTarget = PyCell<Self>;
Expand All @@ -916,11 +916,11 @@ unsafe impl pyo3::PyTypeInfo for MyClass {
impl pyo3::pyclass::PyClass for MyClass {
type Dict = pyo3::pyclass_slots::PyClassDummySlot;
type WeakRef = pyo3::pyclass_slots::PyClassDummySlot;
type BaseNativeType = PyAny;
type BaseNativeType = PyObject;
}

impl pyo3::IntoPy<PyObject> for MyClass {
fn into_py(self, py: pyo3::Python) -> pyo3::PyObject {
impl pyo3::IntoPy<Py<PyObject>> for MyClass {
fn into_py(self, py: pyo3::Python) -> pyo3::Py<pyo3::PyObject> {
pyo3::IntoPy::into_py(pyo3::Py::new(py, self).unwrap(), py)
}
}
Expand Down
25 changes: 12 additions & 13 deletions guide/src/conversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ and [`PyRefMut`]. They work like the reference wrappers of
## The `ToPyObject` trait

[`ToPyObject`] is a conversion trait that allows various objects to be
converted into [`PyObject`]. `IntoPy<PyObject>` serves the
converted into [`&PyObject`]. `IntoPy<Py<PyObject>>` serves the
same purpose, except that it consumes `self`.


## `*args` and `**kwargs` for Python object calls

There are several ways how to pass positional and keyword arguments to a Python object call.
[`PyAny`] provides two methods:
[`PyObject`] provides two methods:

* `call` - call any callable Python object.
* `call_method` - call a specific method on the object, shorthand for `get_attr` then `call`.
Expand All @@ -48,7 +48,7 @@ use pyo3::types::{PyDict, PyTuple};

struct SomeObject;
impl SomeObject {
fn new(py: Python) -> PyObject {
fn new(py: Python) -> &PyObject {
PyDict::new(py).to_object(py)
}
}
Expand All @@ -64,15 +64,15 @@ fn main() {
let obj = SomeObject::new(py);

// call object without empty arguments
obj.call0(py);
obj.call0();

// call object with PyTuple
let args = PyTuple::new(py, &[arg1, arg2, arg3]);
obj.call1(py, args);
obj.call1(args);

// pass arguments as rust tuple
let args = (arg1, arg2, arg3);
obj.call1(py, args);
obj.call1(args);
}
```

Expand All @@ -89,7 +89,7 @@ use std::collections::HashMap;
struct SomeObject;

impl SomeObject {
fn new(py: Python) -> PyObject {
fn new(py: Python) -> &PyObject {
PyDict::new(py).to_object(py)
}
}
Expand All @@ -107,16 +107,16 @@ fn main() {

// call object with PyDict
let kwargs = [(key1, val1)].into_py_dict(py);
obj.call(py, (), Some(kwargs));
obj.call((), Some(kwargs));

// pass arguments as Vec
let kwargs = vec![(key1, val1), (key2, val2)];
obj.call(py, (), Some(kwargs.into_py_dict(py)));
obj.call((), Some(kwargs.into_py_dict(py)));

// pass arguments as HashMap
let mut kwargs = HashMap::<&str, i32>::new();
kwargs.insert(key1, 1);
obj.call(py, (), Some(kwargs.into_py_dict(py)));
obj.call((), Some(kwargs.into_py_dict(py)));
}
```

Expand All @@ -132,12 +132,11 @@ Eventually, traits such as [`ToPyObject`] will be replaced by this trait and a [
[`IntoPy`], just like with `From` and `Into`.

[`IntoPy`]: https://docs.rs/pyo3/latest/pyo3/conversion/trait.IntoPy.html
[`FromPy`]: https://docs.rs/pyo3/latest/pyo3/conversion/trait.FromPy.html
[`FromPy`]: https://docs.rs/pyo3/latest/pyo3/conversion.FromPy.html
[`FromPyObject`]: https://docs.rs/pyo3/latest/pyo3/conversion/trait.FromPyObject.html
[`ToPyObject`]: https://docs.rs/pyo3/latest/pyo3/conversion/trait.ToPyObject.html
[`PyObject`]: https://docs.rs/pyo3/latest/pyo3/struct.PyObject.html
[`PyObject`]: https://docs.rs/pyo3/latest/pyo3/types/struct.PyObject.html
[`PyTuple`]: https://docs.rs/pyo3/latest/pyo3/types/struct.PyTuple.html
[`PyAny`]: https://docs.rs/pyo3/latest/pyo3/struct.PyAny.html
[`IntoPyDict`]: https://docs.rs/pyo3/latest/pyo3/types/trait.IntoPyDict.html

[`PyRef`]: https://pyo3.rs/master/doc/pyo3/pycell/struct.PyRef.html
Expand Down
8 changes: 4 additions & 4 deletions guide/src/exception.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ have Rust types as well.
# use pyo3::exceptions;
# use pyo3::prelude::*;
# fn check_for_error() -> bool {false}
fn my_func(arg: PyObject) -> PyResult<()> {
fn my_func(arg: &PyObject) -> PyResult<()> {
if check_for_error() {
Err(exceptions::ValueError::py_err("argument is wrong"))
} else {
Expand Down Expand Up @@ -190,15 +190,15 @@ use pyo3::import_exception;

import_exception!(io, UnsupportedOperation);

fn tell(file: PyObject) -> PyResult<u64> {
fn tell(file: &PyObject) -> PyResult<u64> {
use pyo3::exceptions::*;

let gil = Python::acquire_gil();
let py = gil.python();

match file.call_method0(py, "tell") {
match file.call_method0("tell") {
Err(_) => Err(UnsupportedOperation::py_err("not supported: tell")),
Ok(x) => x.extract::<u64>(py),
Ok(x) => x.extract::<u64>(),
}
}

Expand Down
10 changes: 5 additions & 5 deletions guide/src/function.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ Currently, there are no conversions between `Fn`s in Rust and callables in Pytho

### Calling Python functions in Rust

You can use [`PyAny::is_callable`] to check if you have a callable object. `is_callable` will return `true` for functions (including lambdas), methods and objects with a `__call__` method. You can call the object with [`PyAny::call`] with the args as first parameter and the kwargs (or `None`) as second parameter. There are also [`PyAny::call0`] with no args and [`PyAny::call1`] with only positional args.
You can use [`PyObject::is_callable`] to check if you have a callable object. `is_callable` will return `true` for functions (including lambdas), methods and objects with a `__call__` method. You can call the object with [`PyObject::call`] with the args as first parameter and the kwargs (or `None`) as second parameter. There are also [`PyObject::call0`] with no args and [`PyObject::call1`] with only positional args.

### Calling Rust functions in Python

If you have a static function, you can expose it with `#[pyfunction]` and use [`wrap_pyfunction!`] to get the corresponding [`PyObject`]. For dynamic functions, e.g. lambdas and functions that were passed as arguments, you must put them in some kind of owned container, e.g. a `Box`. (A long-term solution will be a special container similar to wasm-bindgen's `Closure`). You can then use a `#[pyclass]` struct with that container as a field as a way to pass the function over the FFI barrier. You can even make that class callable with `__call__` so it looks like a function in Python code.

[`PyAny::is_callable`]: https://docs.rs/pyo3/latest/pyo3/struct.PyAny.html#tymethod.is_callable
[`PyAny::call`]: https://docs.rs/pyo3/latest/pyo3/struct.PyAny.html#tymethod.call
[`PyAny::call0`]: https://docs.rs/pyo3/latest/pyo3/struct.PyAny.html#tymethod.call0
[`PyAny::call1`]: https://docs.rs/pyo3/latest/pyo3/struct.PyAny.html#tymethod.call1
[`PyObject::is_callable`]: https://docs.rs/pyo3/latest/pyo3/struct.PyObject.html#tymethod.is_callable
[`PyObject::call`]: https://docs.rs/pyo3/latest/pyo3/struct.PyObject.html#tymethod.call
[`PyObject::call0`]: https://docs.rs/pyo3/latest/pyo3/struct.PyObject.html#tymethod.call0
[`PyObject::call1`]: https://docs.rs/pyo3/latest/pyo3/struct.PyObject.html#tymethod.call1
[`PyObject`]: https://docs.rs/pyo3/latest/pyo3/struct.PyObject.html
[`wrap_pyfunction!`]: https://docs.rs/pyo3/latest/pyo3/macro.wrap_pyfunction.html
4 changes: 2 additions & 2 deletions guide/src/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ In addition, you can also extract `&PyCell<T>`, though you rarely need it.

Before:
```ignore
let obj: &PyAny = create_obj();
let obj: &PyObject = create_obj();
let obj_ref: &MyClass = obj.extract().unwrap();
let obj_ref_mut: &mut MyClass = obj.extract().unwrap();
```
Expand All @@ -170,7 +170,7 @@ After:
# let typeobj = py.get_type::<MyClass>();
# let d = [("c", typeobj)].into_py_dict(py);
# let create_obj = || py.eval("c()", None, Some(d)).unwrap();
let obj: &PyAny = create_obj();
let obj: &PyObject = create_obj();
let obj_cell: &PyCell<MyClass> = obj.extract().unwrap();
let obj_cloned: MyClass = obj.extract().unwrap(); // extracted by cloning the object
{
Expand Down
2 changes: 1 addition & 1 deletion guide/src/python_from_rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn main() -> PyResult<()> {

[`Python::eval`](https://pyo3.rs/master/doc/pyo3/struct.Python.html#method.eval) is
a method to execute a [Python expression](https://docs.python.org/3.7/reference/expressions.html)
and return the evaluated value as a `&PyAny` object.
and return the evaluated value as a `&PyObject` object.

```rust
use pyo3::prelude::*;
Expand Down
2 changes: 1 addition & 1 deletion guide/src/rust_cpython.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl PyList {

fn new(py: Python) -> &PyList {...}

fn get_item(&self, index: isize) -> &PyAny {...}
fn get_item(&self, index: isize) -> &PyObject {...}
}
```

Expand Down
Loading