Merge branch 'master' of github.com:PyO3/PyO3

This commit is contained in:
Nikolay Kim 2017-06-27 05:06:09 +06:00
commit 8bc1195f12
4 changed files with 28 additions and 10 deletions

View File

@ -4,14 +4,32 @@
## `ToPyObject` and `IntoPyObject` trait
`ToPyObject` trait is a conversion trait that allows various objects to be converted into `PyObject`.
TODO
[`ToPyObject`][ToPyObject] trait is a conversion trait that allows various objects to be converted into [`PyObject`][PyObject]. [`IntoPyObject`][IntoPyObject] serves the same purpose except it consumes `self`.
## `IntoPyTuple` trait
TODO
[`IntoPyTuple`][IntoPyTuple] trait is a conversion trait that allows various objects to be converted into [`PyTuple`][PyTuple] object.
For example, [`IntoPyTuple`][IntoPyTuple] trait is implemented for `()` so that you can convert it into a empty [`PyTuple`][PyTuple]
```rust
extern crate pyo3;
use pyo3::{Python, IntoPyTuple};
fn main() {
let gil = Python::acquire_gil();
let py = gil.python();
let py_tuple = ().into_tuple(py);
}
```
## `FromPyObject` and `RefFromPyObject` trait
TODO
[ToPyObject]: https://pyo3.github.io/PyO3/pyo3/trait.ToPyObject.html
[IntoPyObject]: https://pyo3.github.io/PyO3/pyo3/trait.IntoPyObject.html
[PyObject]: https://pyo3.github.io/PyO3/pyo3/struct.PyObject.html
[IntoPyTuple]: https://pyo3.github.io/PyO3/pyo3/trait.IntoPyTuple.html
[PyTuple]: https://pyo3.github.io/PyO3/pyo3/struct.PyTuple.html

View File

@ -97,7 +97,7 @@ pub trait PyObjectFormatProtocol<'p>: PyObjectProtocol<'p> {
type Result: Into<PyResult<Self::Success>>;
}
pub trait PyObjectHashProtocol<'p>: PyObjectProtocol<'p> {
type Result: Into<PyResult<usize>>;
type Result: Into<PyResult<isize>>;
}
pub trait PyObjectBoolProtocol<'p>: PyObjectProtocol<'p> {
type Result: Into<PyResult<bool>>;
@ -322,7 +322,7 @@ impl<T> PyObjectHashProtocolImpl for T
{
#[inline]
fn tp_hash() -> Option<ffi::hashfunc> {
py_unary_func!(PyObjectHashProtocol, T::__hash__, usize, HashConverter, ffi::Py_hash_t)
py_unary_func!(PyObjectHashProtocol, T::__hash__, isize, HashConverter, ffi::Py_hash_t)
}
}

View File

@ -85,7 +85,7 @@ pub trait ObjectProtocol {
/// Retrieves the hash code of the object.
/// This is equivalent to the Python expression: 'hash(self)'
fn hash(&self) -> PyResult<ffi::Py_hash_t>;
fn hash(&self) -> PyResult<isize>;
/// Returns whether the object is considered to be true.
/// This is equivalent to the Python expression: 'not not self'
@ -275,7 +275,7 @@ impl<T> ObjectProtocol for T where T: PyObjectWithToken + ToPyPointer {
}
#[inline]
fn hash(&self) -> PyResult<ffi::Py_hash_t> {
fn hash(&self) -> PyResult<isize> {
let v = unsafe { ffi::PyObject_Hash(self.as_ptr()) };
if v == -1 {
Err(PyErr::fetch(self.token()))

View File

@ -539,8 +539,8 @@ struct Comparisons {
#[py::proto]
impl PyObjectProtocol for Comparisons {
fn __hash__(&self) -> PyResult<usize> {
Ok(self.val as usize)
fn __hash__(&self) -> PyResult<isize> {
Ok(self.val as isize)
}
fn __bool__(&self) -> PyResult<bool> {
Ok(self.val != 0)