Skip to content

Commit

Permalink
Rename PyAny -> PyObject
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Apr 20, 2020
1 parent 1189310 commit b796776
Show file tree
Hide file tree
Showing 70 changed files with 575 additions and 569 deletions.
4 changes: 2 additions & 2 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<Py<PyAny>>,
obj: Vec<Py<PyObject>>,
}

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

fn push(&mut self, py: Python, obj: &PyAny) {
fn push(&mut self, py: Python, obj: &PyObject) {
self.obj.push(obj.to_object(py).into());
}
}
Expand Down
32 changes: 16 additions & 16 deletions guide/src/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,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 @@ -54,11 +54,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<pyo3::Py<pyo3::PyAny>> for MyClass {
fn into_py(self, py: pyo3::Python) -> pyo3::Py<pyo3::PyAny> {
impl pyo3::IntoPy<pyo3::Py<pyo3::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 Expand Up @@ -240,7 +240,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 @@ -340,7 +340,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 @@ -498,7 +498,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<Py<PyAny>>`;
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 @@ -549,13 +549,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<Py<PyAny>>`.
* 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<Py<PyAny>>`.
`IntoPy<Py<PyObject>>`.

```rust
# use pyo3::prelude::*;
Expand Down Expand Up @@ -699,7 +699,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<Py<PyAny>>>`
* `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 @@ -764,7 +764,7 @@ use pyo3::gc::{PyGCProtocol, PyVisit};

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

#[pyproto]
Expand Down Expand Up @@ -805,8 +805,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<Py<PyAny>>>`
* `fn __next__(slf: PyRefMut<Self>) -> PyResult<Option<impl IntoPy<Py<PyAny>>>>`
* `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 @@ -821,15 +821,15 @@ use pyo3::PyIterProtocol;

#[pyclass]
struct MyIterator {
iter: Box<Iterator<Item = Py<PyAny>> + 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<Py<PyAny>>> {
fn __next__(mut slf: PyRefMut<Self>) -> PyResult<Option<Py<PyObject>>> {
Ok(slf.iter.next())
}
}
Expand Down
8 changes: 4 additions & 4 deletions guide/src/conversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ 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 [`&PyAny`]. `IntoPy<Py<PyAny>>` serves the
converted into [`&PyObject`]. `IntoPy<Py<PyObject>>` serves the
same purpose, except that it consumes `self`.


Expand All @@ -48,7 +48,7 @@ use pyo3::types::{PyDict, PyTuple};

struct SomeObject;
impl SomeObject {
fn new(py: Python) -> &PyAny {
fn new(py: Python) -> &PyObject {
PyDict::new(py).to_object(py)
}
}
Expand Down Expand Up @@ -89,7 +89,7 @@ use std::collections::HashMap;
struct SomeObject;

impl SomeObject {
fn new(py: Python) -> &PyAny {
fn new(py: Python) -> &PyObject {
PyDict::new(py).to_object(py)
}
}
Expand Down Expand Up @@ -135,7 +135,7 @@ Eventually, traits such as [`ToPyObject`] will be replaced by this trait and a [
[`FromPy`]: https://docs.rs/pyo3/latest/pyo3/trait.FromPy.html
[`FromPyObject`]: https://docs.rs/pyo3/latest/pyo3/types/trait.FromPyObject.html
[`ToPyObject`]: https://docs.rs/pyo3/latest/pyo3/trait.ToPyObject.html
[`PyAny`]: https://docs.rs/pyo3/latest/pyo3/struct.PyAny.html
[`PyObject`]: https://docs.rs/pyo3/latest/pyo3/struct.PyObject.html
[`PyTuple`]: https://docs.rs/pyo3/latest/pyo3/types/struct.PyTuple.html
[`ObjectProtocol`]: https://docs.rs/pyo3/latest/pyo3/trait.ObjectProtocol.html
[`IntoPyDict`]: https://docs.rs/pyo3/latest/pyo3/types/trait.IntoPyDict.html
Expand Down
4 changes: 2 additions & 2 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: &PyAny) -> 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,7 +190,7 @@ use pyo3::import_exception;

import_exception!(io, UnsupportedOperation);

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

let gil = Python::acquire_gil();
Expand Down
4 changes: 2 additions & 2 deletions guide/src/function.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ You can use [`ObjectProtocol::is_callable`] to check if you have a callable obje

### 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 [`PyAny`]. 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.
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.

[`ObjectProtocol::is_callable`]: https://docs.rs/pyo3/latest/pyo3/trait.ObjectProtocol.html#tymethod.is_callable
[`ObjectProtocol::call`]: https://docs.rs/pyo3/latest/pyo3/trait.ObjectProtocol.html#tymethod.call
[`ObjectProtocol::call0`]: https://docs.rs/pyo3/latest/pyo3/trait.ObjectProtocol.html#tymethod.call0
[`ObjectProtocol::call1`]: https://docs.rs/pyo3/latest/pyo3/trait.ObjectProtocol.html#tymethod.call1
[`PyAny`]: https://docs.rs/pyo3/latest/pyo3/struct.PyAny
[`PyObject`]: https://docs.rs/pyo3/latest/pyo3/struct.PyObject
[`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 @@ -121,7 +121,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 @@ -137,7 +137,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 @@ -7,7 +7,7 @@ PyO3 is built for an extension module or not.

[`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 @@ -69,7 +69,7 @@ impl PyList {
fn new(py: Python) -> &PyList {...}
fn get_item(&self, index: isize) -> &PyAny {...}
fn get_item(&self, index: isize) -> &PyObject {...}
}
```

Expand Down
20 changes: 10 additions & 10 deletions guide/src/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ In PyO3, holding the GIL is modeled by acquiring a token of the type
* It can be passed to functions that require a proof of holding the GIL,
such as [`Py::clone_ref`][clone_ref].
* Its lifetime can be used to create Rust references that implicitly guarantee
holding the GIL, such as [`&'py PyAny`][PyAny].
holding the GIL, such as [`&'py PyObject`][PyObject].

The latter two points are the reason why some APIs in PyO3 require the `py:
Python` argument, while others don't.
Expand All @@ -46,11 +46,11 @@ references is done at runtime using `PyCell`, a scheme very similar to

## Object types

### `PyAny`
### `PyObject`

**Represents:** a Python object of unspecified type, restricted to a GIL
lifetime. Currently, `PyAny` can only ever occur as a reference, usually
`&PyAny`.
lifetime. Currently, `PyObject` can only ever occur as a reference, usually
`&PyObject`.

**Used:** Whenever you want to refer to some Python object only as long as
holding the GIL. For example, intermediate values and arguments to
Expand Down Expand Up @@ -83,15 +83,15 @@ or returning objects from functions implemented in Rust back to Python.
### `PyTuple`, `PyDict`, and many more

**Represents:** a native Python object of known type, restricted to a GIL
lifetime just like `PyAny`.
lifetime just like `PyObject`.

**Used:** Whenever you want to operate with native Python types while holding
the GIL. Like `PyAny`, this is the most convenient form to use for function
the GIL. Like `PyObject`, this is the most convenient form to use for function
arguments and intermediate values.

**Conversions:**

- To `PyAny`: `obj.as_ref()`
- To `PyObject`: `obj.as_ref()`
- To `Py<T>`: `Py::from(obj)`


Expand All @@ -107,7 +107,7 @@ Rust references.

**Conversions:**

- From `PyAny`: `.downcast()`
- From `PyObject`: `.downcast()`


### `PyRef<SomeType>` and `PyRefMut<SomeType>`
Expand All @@ -116,7 +116,7 @@ Rust references.
borrows, analog to `Ref` and `RefMut` used by `RefCell`.

**Used:** while borrowing a `PyCell`. They can also be used with `.extract()`
on types like `Py<T>` and `PyAny` to get a reference quickly.
on types like `Py<T>` and `PyObject` to get a reference quickly.



Expand All @@ -135,6 +135,6 @@ This trait marks structs that mirror native Python types, such as `PyList`.

[eval]: https://docs.rs/pyo3/latest/pyo3/struct.Python.html#method.eval
[clone_ref]: https://docs.rs/pyo3/latest/pyo3/struct.Py.html#method.clone_ref
[PyAny]: https://docs.rs/pyo3/latest/pyo3/types/struct.PyAny.html
[PyObject]: https://docs.rs/pyo3/latest/pyo3/types/struct.PyObject.html
[PyList_append]: https://docs.rs/pyo3/latest/pyo3/types/struct.PyList.html#method.append
[RefCell]: https://doc.rust-lang.org/std/cell/struct.RefCell.html
2 changes: 1 addition & 1 deletion pyo3-derive-backend/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ pub fn add_fn_to_module(
let wrapper = function_c_wrapper(&func.sig.ident, &spec);

Ok(quote! {
fn #function_wrapper_ident(py: pyo3::Python) -> pyo3::Py<pyo3::PyAny> {
fn #function_wrapper_ident(py: pyo3::Python) -> pyo3::Py<pyo3::PyObject> {
#wrapper

let _def = pyo3::class::PyMethodDef {
Expand Down
12 changes: 6 additions & 6 deletions pyo3-derive-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Default for PyClassArgs {
// We need the 0 as value for the constant we're later building using quote for when there
// are no other flags
flags: vec![parse_quote! { 0 }],
base: parse_quote! { pyo3::PyAny },
base: parse_quote! { pyo3::PyObject },
has_extends: false,
}
}
Expand Down Expand Up @@ -357,19 +357,19 @@ fn impl_class(
let base_layout = if attr.has_extends {
quote! { <Self::BaseType as pyo3::derive_utils::PyBaseTypeUtils>::LayoutAsBase }
} else {
quote! { pyo3::pycell::PyCellBase<pyo3::PyAny> }
quote! { pyo3::pycell::PyCellBase<pyo3::PyObject> }
};
let base_nativetype = if attr.has_extends {
quote! { <Self::BaseType as pyo3::derive_utils::PyBaseTypeUtils>::BaseNativeType }
} else {
quote! { pyo3::PyAny }
quote! { pyo3::PyObject }
};

// If #cls is not extended type, we allow Self->Py<PyAny> conversion
// If #cls is not extended type, we allow Self->Py<PyObject> conversion
let into_pyobject = if !attr.has_extends {
quote! {
impl pyo3::IntoPy<Py<PyAny>> for #cls {
fn into_py(self, py: pyo3::Python) -> pyo3::Py<PyAny> {
impl pyo3::IntoPy<Py<PyObject>> for #cls {
fn into_py(self, py: pyo3::Python) -> pyo3::Py<PyObject> {
pyo3::IntoPy::into_py(pyo3::Py::new(py, self).unwrap(), py)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pyo3-derive-backend/src/pymethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ pub(crate) fn impl_wrap_setter(
pyo3::run_callback(_py, || {
let _slf = _py.from_borrowed_ptr::<pyo3::PyCell<#cls>>(_slf);
#borrow_self
let _value = _py.from_borrowed_ptr::<pyo3::types::PyAny>(_value);
let _value = _py.from_borrowed_ptr::<pyo3::types::PyObject>(_value);
let _val = pyo3::FromPyObject::extract(_value)?;
pyo3::callback::convert(_py, {#setter_impl})
})
Expand Down
4 changes: 2 additions & 2 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

//! `PyBuffer` implementation
use crate::err::{self, PyResult};
use crate::{exceptions, ffi, AsPyPointer, PyAny, Python};
use crate::{exceptions, ffi, AsPyPointer, PyObject, Python};
use std::ffi::CStr;
use std::os::raw;
use std::pin::Pin;
Expand Down Expand Up @@ -160,7 +160,7 @@ fn validate(b: &ffi::Py_buffer) {

impl PyBuffer {
/// Get the underlying buffer from the specified python object.
pub fn get(py: Python, obj: &PyAny) -> PyResult<PyBuffer> {
pub fn get(py: Python, obj: &PyObject) -> PyResult<PyBuffer> {
unsafe {
let mut buf = Box::pin(mem::zeroed::<ffi::Py_buffer>());
err::error_on_minusone(
Expand Down
4 changes: 2 additions & 2 deletions src/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::err::PyResult;
use crate::exceptions::OverflowError;
use crate::ffi::{self, Py_hash_t};
use crate::IntoPyPointer;
use crate::{IntoPy, Py, PyAny, Python};
use crate::{IntoPy, Py, PyObject, Python};
use std::isize;
use std::os::raw::c_int;

Expand Down Expand Up @@ -48,7 +48,7 @@ where

impl<T> IntoPyCallbackOutput<*mut ffi::PyObject> for T
where
T: IntoPy<Py<PyAny>>,
T: IntoPy<Py<PyObject>>,
{
fn convert(self, py: Python) -> PyResult<*mut ffi::PyObject> {
Ok(self.into_py(py).into_ptr())
Expand Down
Loading

0 comments on commit b796776

Please sign in to comment.