Skip to content
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

Remove intoPyObject #583

Merged
merged 2 commits into from
Aug 26, 2019
Merged
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
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [0.8.0]

### Added

* `module` argument to `pyclass` macro. [#499](https://github.com/PyO3/pyo3/pull/499)
* `py_run!` macro [#512](https://github.com/PyO3/pyo3/pull/512)
* Use existing fields and methods before calling custom __getattr__. [#505](https://github.com/PyO3/pyo3/pull/512)
* `PyBytes` can now be indexed just like `Vec<u8>`
* Implement `IntoPyObject` for `PyRef` and `PyRefMut`.
* Implement `IntoPy<PyObject>` for `PyRef` and `PyRefMut`.

## Removed

* `IntoPyObject` was replaced with `IntoPy<PyObject>`
* `#[pyclass(subclass)]` is hidden a `unsound-subclass` feature because it's causing segmentation faults.

### Fixed
Expand Down
12 changes: 6 additions & 6 deletions guide/src/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,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 `IntoPyObject`;
The return type must be `PyResult<T>` or `T` for some `T` that implements `IntoPy<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 @@ -376,13 +376,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 `IntoPyObject`.
* The return type must be `PyResult<T>` or `T` for some `T` that implements `IntoPy<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
`IntoPyObject`.
`IntoPy<PyObject>`.

```rust
# use pyo3::prelude::*;
Expand Down Expand Up @@ -483,7 +483,7 @@ The [`PyObjectProtocol`](https://docs.rs/pyo3/0.7.0/pyo3/class/basic/trait.PyObj

To customize object attribute access, define the following methods:

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

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

Returning `Ok(None)` from `__next__` indicates that that there are no further items.

Expand Down
7 changes: 3 additions & 4 deletions guide/src/conversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ PyO3 provides some handy traits to convert between Python types and Rust types.

The easiest way to convert a Python object to a Rust value is using `.extract()?`.

## `ToPyObject` and `IntoPyObject` trait
## `ToPyObject` trait

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

## `FromPyObject` and `RefFromPyObject` trait
Expand Down Expand Up @@ -104,10 +104,9 @@ fn main() {

Many conversions in PyO3 can't use `std::convert::Into` because they need a GIL token. That's why the `IntoPy<T>` trait offers an `into_py` method that works just like `into`, except for taking a `Python<'_>` argument.

Eventually, traits such as `IntoPyObject` will be replaced by this trait and a `FromPy` trait will be added that will implement `IntoPy`, just like with `From` and `Into`.
Eventually, traits such as `ToPyObject` will be replaced by this trait and a `FromPy` trait will be added that will implement `IntoPy`, just like with `From` and `Into`.

[`ToPyObject`]: https://docs.rs/pyo3/0.7.0/pyo3/trait.ToPyObject.html
[IntoPyObject]: https://docs.rs/pyo3/0.7.0/pyo3/trait.IntoPyObject.html
[PyObject]: https://docs.rs/pyo3/0.7.0/pyo3/struct.PyObject.html
[PyTuple]: https://docs.rs/pyo3/0.7.0/pyo3/types/struct.PyTuple.html
[ObjectProtocol]: https://docs.rs/pyo3/0.7.0/pyo3/trait.ObjectProtocol.html
Expand Down
6 changes: 3 additions & 3 deletions pyo3-derive-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,9 @@ fn impl_class(
}
}

impl pyo3::IntoPyObject for #cls {
fn into_object(self, py: pyo3::Python) -> pyo3::PyObject {
pyo3::Py::new(py, self).unwrap().into_object(py)
impl pyo3::IntoPy<PyObject> for #cls {
fn into_py(self, py: pyo3::Python) -> pyo3::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 @@ -337,7 +337,7 @@ pub(crate) fn impl_wrap_getter(cls: &syn::Type, name: &syn::Ident, takes_py: boo

match result {
Ok(val) => {
pyo3::IntoPyPointer::into_ptr(val.into_object(_py))
pyo3::IntoPyPointer::into_ptr(pyo3::IntoPy::<PyObject>::into_py(val, _py))
}
Err(e) => {
e.restore(_py);
Expand Down
8 changes: 4 additions & 4 deletions src/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use crate::err::PyResult;
use crate::exceptions::OverflowError;
use crate::ffi::{self, Py_hash_t};
use crate::Python;
use crate::{IntoPyObject, IntoPyPointer};
use crate::IntoPyPointer;
use crate::{IntoPy, PyObject, Python};
use std::os::raw::c_int;
use std::{isize, ptr};

Expand All @@ -21,12 +21,12 @@ pub struct PyObjectCallbackConverter;

impl<S> CallbackConverter<S> for PyObjectCallbackConverter
where
S: IntoPyObject,
S: IntoPy<PyObject>,
{
type R = *mut ffi::PyObject;

fn convert(val: S, py: Python) -> *mut ffi::PyObject {
val.into_object(py).into_ptr()
val.into_py(py).into_ptr()
}

#[inline]
Expand Down
18 changes: 9 additions & 9 deletions src/class/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
use crate::callback::{BoolCallbackConverter, HashConverter, PyObjectCallbackConverter};
use crate::class::methods::PyMethodDef;
use crate::err::{PyErr, PyResult};
use crate::exceptions;
use crate::ffi;
use crate::objectprotocol::ObjectProtocol;
use crate::type_object::PyTypeInfo;
use crate::types::PyAny;
use crate::FromPyObject;
use crate::IntoPyPointer;
use crate::Python;
use crate::{FromPyObject, IntoPyObject};
use crate::{exceptions, IntoPy, PyObject};
use std::os::raw::c_int;
use std::ptr;

Expand Down Expand Up @@ -109,7 +109,7 @@ pub trait PyObjectProtocol<'p>: PyTypeInfo {

pub trait PyObjectGetAttrProtocol<'p>: PyObjectProtocol<'p> {
type Name: FromPyObject<'p>;
type Success: IntoPyObject;
type Success: IntoPy<PyObject>;
type Result: Into<PyResult<Self::Success>>;
}
pub trait PyObjectSetAttrProtocol<'p>: PyObjectProtocol<'p> {
Expand All @@ -122,16 +122,16 @@ pub trait PyObjectDelAttrProtocol<'p>: PyObjectProtocol<'p> {
type Result: Into<PyResult<()>>;
}
pub trait PyObjectStrProtocol<'p>: PyObjectProtocol<'p> {
type Success: IntoPyObject;
type Success: IntoPy<PyObject>;
type Result: Into<PyResult<Self::Success>>;
}
pub trait PyObjectReprProtocol<'p>: PyObjectProtocol<'p> {
type Success: IntoPyObject;
type Success: IntoPy<PyObject>;
type Result: Into<PyResult<Self::Success>>;
}
pub trait PyObjectFormatProtocol<'p>: PyObjectProtocol<'p> {
type Format: FromPyObject<'p>;
type Success: IntoPyObject;
type Success: IntoPy<PyObject>;
type Result: Into<PyResult<Self::Success>>;
}
pub trait PyObjectHashProtocol<'p>: PyObjectProtocol<'p> {
Expand All @@ -141,12 +141,12 @@ pub trait PyObjectBoolProtocol<'p>: PyObjectProtocol<'p> {
type Result: Into<PyResult<bool>>;
}
pub trait PyObjectBytesProtocol<'p>: PyObjectProtocol<'p> {
type Success: IntoPyObject;
type Success: IntoPy<PyObject>;
type Result: Into<PyResult<Self::Success>>;
}
pub trait PyObjectRichcmpProtocol<'p>: PyObjectProtocol<'p> {
type Other: FromPyObject<'p>;
type Success: IntoPyObject;
type Success: IntoPy<PyObject>;
type Result: Into<PyResult<Self::Success>>;
}

Expand Down Expand Up @@ -463,7 +463,7 @@ where
Err(e) => Err(e),
};
match res {
Ok(val) => val.into_object(py).into_ptr(),
Ok(val) => val.into_py(py).into_ptr(),
Err(e) => {
e.restore(py);
ptr::null_mut()
Expand Down
5 changes: 3 additions & 2 deletions src/class/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use crate::class::methods::PyMethodDef;
use crate::err::PyResult;
use crate::type_object::PyTypeInfo;
use crate::PyObject;

/// Context manager interface
#[allow(unused_variables)]
Expand All @@ -32,15 +33,15 @@ pub trait PyContextProtocol<'p>: PyTypeInfo {
}

pub trait PyContextEnterProtocol<'p>: PyContextProtocol<'p> {
type Success: crate::IntoPyObject;
type Success: crate::IntoPy<PyObject>;
type Result: Into<PyResult<Self::Success>>;
}

pub trait PyContextExitProtocol<'p>: PyContextProtocol<'p> {
type ExcType: crate::FromPyObject<'p>;
type ExcValue: crate::FromPyObject<'p>;
type Traceback: crate::FromPyObject<'p>;
type Success: crate::IntoPyObject;
type Success: crate::IntoPy<PyObject>;
type Result: Into<PyResult<Self::Success>>;
}

Expand Down
6 changes: 3 additions & 3 deletions src/class/descr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
use crate::callback::{PyObjectCallbackConverter, UnitCallbackConverter};
use crate::class::methods::PyMethodDef;
use crate::err::PyResult;
use crate::ffi;
use crate::type_object::PyTypeInfo;
use crate::types::{PyAny, PyType};
use crate::{FromPyObject, IntoPyObject};
use crate::FromPyObject;
use crate::{ffi, IntoPy, PyObject};
use std::os::raw::c_int;

/// Descriptor interface
Expand Down Expand Up @@ -49,7 +49,7 @@ pub trait PyDescrProtocol<'p>: PyTypeInfo {
pub trait PyDescrGetProtocol<'p>: PyDescrProtocol<'p> {
type Inst: FromPyObject<'p>;
type Owner: FromPyObject<'p>;
type Success: IntoPyObject;
type Success: IntoPy<PyObject>;
type Result: Into<PyResult<Self::Success>>;
}

Expand Down
11 changes: 5 additions & 6 deletions src/class/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

use crate::callback::{CallbackConverter, PyObjectCallbackConverter};
use crate::err::PyResult;
use crate::ffi;
use crate::instance::PyRefMut;
use crate::type_object::PyTypeInfo;
use crate::IntoPyObject;
use crate::IntoPyPointer;
use crate::Python;
use crate::{ffi, IntoPy, PyObject};
use std::ptr;

/// Python Iterator Interface.
Expand All @@ -34,12 +33,12 @@ pub trait PyIterProtocol<'p>: PyTypeInfo + Sized {
}

pub trait PyIterIterProtocol<'p>: PyIterProtocol<'p> {
type Success: crate::IntoPyObject;
type Success: crate::IntoPy<PyObject>;
type Result: Into<PyResult<Self::Success>>;
}

pub trait PyIterNextProtocol<'p>: PyIterProtocol<'p> {
type Success: crate::IntoPyObject;
type Success: crate::IntoPy<PyObject>;
type Result: Into<PyResult<Option<Self::Success>>>;
}

Expand Down Expand Up @@ -111,13 +110,13 @@ struct IterNextConverter;

impl<T> CallbackConverter<Option<T>> for IterNextConverter
where
T: IntoPyObject,
T: IntoPy<PyObject>,
{
type R = *mut ffi::PyObject;

fn convert(val: Option<T>, py: Python) -> *mut ffi::PyObject {
match val {
Some(val) => val.into_object(py).into_ptr(),
Some(val) => val.into_py(py).into_ptr(),
None => unsafe {
ffi::PyErr_SetNone(ffi::PyExc_StopIteration);
ptr::null_mut()
Expand Down
2 changes: 1 addition & 1 deletion src/class/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ macro_rules! py_unary_func {
let py = $crate::Python::assume_gil_acquired();
let slf = py.mut_from_borrowed_ptr::<T>(slf);
let res = slf.$f().into();
$crate::callback::cb_convert($conv, py, res)
$crate::callback::cb_convert($conv, py, res.map(|x| x))
}
Some(wrap::<$class>)
}};
Expand Down
10 changes: 5 additions & 5 deletions src/class/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
use crate::callback::{LenResultConverter, PyObjectCallbackConverter};
use crate::class::methods::PyMethodDef;
use crate::err::{PyErr, PyResult};
use crate::exceptions;
use crate::ffi;
use crate::type_object::PyTypeInfo;
use crate::FromPyObject;
use crate::Python;
use crate::{FromPyObject, IntoPyObject};
use crate::{exceptions, IntoPy, PyObject};

/// Mapping interface
#[allow(unused_variables)]
Expand Down Expand Up @@ -74,7 +74,7 @@ pub trait PyMappingLenProtocol<'p>: PyMappingProtocol<'p> {

pub trait PyMappingGetItemProtocol<'p>: PyMappingProtocol<'p> {
type Key: FromPyObject<'p>;
type Success: IntoPyObject;
type Success: IntoPy<PyObject>;
type Result: Into<PyResult<Self::Success>>;
}

Expand All @@ -90,7 +90,7 @@ pub trait PyMappingDelItemProtocol<'p>: PyMappingProtocol<'p> {
}

pub trait PyMappingIterProtocol<'p>: PyMappingProtocol<'p> {
type Success: IntoPyObject;
type Success: IntoPy<PyObject>;
type Result: Into<PyResult<Self::Success>>;
}

Expand All @@ -100,7 +100,7 @@ pub trait PyMappingContainsProtocol<'p>: PyMappingProtocol<'p> {
}

pub trait PyMappingReversedProtocol<'p>: PyMappingProtocol<'p> {
type Success: IntoPyObject;
type Success: IntoPy<PyObject>;
type Result: Into<PyResult<Self::Success>>;
}

Expand Down
Loading