diff --git a/CHANGELOG.md b/CHANGELOG.md index e580c45a..e80cdbdc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. * `PyDict::from_sequence()`, equivalent to `dict([(key, val), ...])` * Bindings for the `datetime` standard library types: `PyDate`, `PyTime`, `PyDateTime`, `PyTzInfo`, `PyDelta` with associated `ffi` types, by pganssle [#200](https://github.com/PyO3/pyo3/pull/200). * `PyString`, `PyUnicode`, and `PyBytes` now have an `as_bytes()` method that returns `&[u8]`. + * `PyObjectProtocol::get_type_ptr()` by ijl in [#242](https://github.com/PyO3/pyo3/pull/242) ### Removed * Removed most entries from the prelude. The new prelude is small and clear. diff --git a/pyo3-derive-backend/src/py_class.rs b/pyo3-derive-backend/src/py_class.rs index be032b8e..784ba205 100644 --- a/pyo3-derive-backend/src/py_class.rs +++ b/pyo3-derive-backend/src/py_class.rs @@ -257,24 +257,6 @@ fn impl_class( } } - impl ::pyo3::ToBorrowedObject for #cls { - fn with_borrowed_ptr(&self, _py: ::pyo3::Python, f: F) -> R - where F: FnOnce(*mut ::pyo3::ffi::PyObject) -> R - { - use ::pyo3::python::ToPyPointer; - f(self.as_ptr()) - } - } - - impl<'a> ::pyo3::ToBorrowedObject for &'a mut #cls { - fn with_borrowed_ptr(&self, _py: ::pyo3::Python, f: F) -> R - where F: FnOnce(*mut ::pyo3::ffi::PyObject) -> R - { - use ::pyo3::python::ToPyPointer; - f(self.as_ptr()) - } - } - #extra } } diff --git a/src/conversion.rs b/src/conversion.rs index d1976343..da224209 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -16,6 +16,10 @@ pub trait ToPyObject { fn to_object(&self, py: Python) -> PyObject; } +/// This trait has two implementations: The slow one is implemented for +/// all [ToPyObject] and creates a new object using [ToPyObject::to_object], +/// while the fast one is only implemented for ToPyPointer (we know +/// that every ToPyObject is also ToPyObject) and uses [ToPyPointer::as_ptr()] pub trait ToBorrowedObject: ToPyObject { /// Converts self into a Python object and calls the specified closure /// on the native FFI pointer underlying the Python object. @@ -37,6 +41,18 @@ pub trait ToBorrowedObject: ToPyObject { impl ToBorrowedObject for T where T: ToPyObject {} +impl ToBorrowedObject for T +where + T: ToPyObject + ToPyPointer, +{ + fn with_borrowed_ptr(&self, _py: Python, f: F) -> R + where + F: FnOnce(*mut ffi::PyObject) -> R, + { + f(self.as_ptr()) + } +} + /// Conversion trait that allows various objects to be converted into `PyObject` /// by consuming original object. pub trait IntoPyObject { diff --git a/src/object.rs b/src/object.rs index 025c5bd7..0570953d 100644 --- a/src/object.rs +++ b/src/object.rs @@ -258,16 +258,6 @@ impl ToPyObject for PyObject { } } -impl ToBorrowedObject for PyObject { - #[inline] - fn with_borrowed_ptr(&self, _py: Python, f: F) -> R - where - F: FnOnce(*mut ffi::PyObject) -> R, - { - f(self.as_ptr()) - } -} - impl ToPyPointer for PyObject { /// Gets the underlying FFI pointer, returns a borrowed pointer. #[inline] diff --git a/src/objectprotocol.rs b/src/objectprotocol.rs index 7b5d3961..1b5e9c74 100644 --- a/src/objectprotocol.rs +++ b/src/objectprotocol.rs @@ -44,7 +44,7 @@ pub trait ObjectProtocol { /// On Python 2, this is equivalent to the Python expression `cmp(self, other)`. /// /// On Python 3, this is equivalent to: - /// ```python,ignore + /// ```python /// if self == other: /// return Equal /// elif a < b: diff --git a/src/typeob.rs b/src/typeob.rs index 514b0025..fd43b4a9 100644 --- a/src/typeob.rs +++ b/src/typeob.rs @@ -72,7 +72,9 @@ pub const PY_TYPE_FLAG_DICT: usize = 1 << 3; /// Calling of `__new__` method of base class is developer's responsibility. /// /// Example of custom class implementation with `__new__` method: -/// ```rust,ignore +/// ``` +/// #![feature(specialization)] +/// /// use pyo3::prelude::*; /// /// #[pyclass] @@ -84,8 +86,7 @@ pub const PY_TYPE_FLAG_DICT: usize = 1 << 3; /// impl MyClass { /// #[new] /// fn __new__(obj: &PyRawObject) -> PyResult<()> { -/// obj.init(|token| MyClass{token: token}); -/// MyClass::BaseType::__new__(obj) +/// obj.init(|token| MyClass { token }) /// } /// } /// ``` diff --git a/src/types/boolobject.rs b/src/types/boolobject.rs index 67454e53..b48569a9 100644 --- a/src/types/boolobject.rs +++ b/src/types/boolobject.rs @@ -1,5 +1,5 @@ // Copyright (c) 2017-present PyO3 Project and Contributors -use conversion::{IntoPyObject, PyTryFrom, ToBorrowedObject, ToPyObject}; +use conversion::{IntoPyObject, PyTryFrom, ToPyObject}; use ffi; use object::PyObject; use python::{Python, ToPyPointer}; @@ -44,23 +44,6 @@ impl ToPyObject for bool { } } -impl ToBorrowedObject for bool { - #[inline] - fn with_borrowed_ptr(&self, _py: Python, f: F) -> R - where - F: FnOnce(*mut ffi::PyObject) -> R, - { - // Avoid unnecessary Py_INCREF/Py_DECREF pair - f(unsafe { - if *self { - ffi::Py_True() - } else { - ffi::Py_False() - } - }) - } -} - impl IntoPyObject for bool { #[inline] fn into_object(self, py: Python) -> PyObject { diff --git a/src/types/mod.rs b/src/types/mod.rs index c37cafce..53860cb5 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -143,16 +143,6 @@ macro_rules! pyobject_native_type_convert( } } - impl<$($type_param,)*> $crate::ToBorrowedObject for $name - { - #[inline] - fn with_borrowed_ptr(&self, _py: $crate::Python, f: F) -> R - where F: FnOnce(*mut $crate::ffi::PyObject) -> R - { - f(self.0.as_ptr()) - } - } - impl<$($type_param,)*> ::std::fmt::Debug for $name { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> diff --git a/tests/test_gc.rs b/tests/test_gc.rs index ec0ee2e9..fbf4a533 100644 --- a/tests/test_gc.rs +++ b/tests/test_gc.rs @@ -52,6 +52,7 @@ fn class_with_freelist() { struct TestDropCall { drop_called: Arc, } + impl Drop for TestDropCall { fn drop(&mut self) { self.drop_called.store(true, Ordering::Relaxed); @@ -98,6 +99,7 @@ fn data_is_dropped() { struct ClassWithDrop { token: PyToken, } + impl Drop for ClassWithDrop { fn drop(&mut self) { unsafe { @@ -188,6 +190,7 @@ fn gc_integration() { struct GCIntegration2 { token: PyToken, } + #[test] fn gc_integration2() { let gil = Python::acquire_gil(); @@ -200,6 +203,7 @@ fn gc_integration2() { struct WeakRefSupport { token: PyToken, } + #[test] fn weakref_support() { let gil = Python::acquire_gil(); @@ -237,7 +241,7 @@ impl Drop for BaseClassWithDrop { } } -#[pyclass(extends=BaseClassWithDrop)] +#[pyclass(extends = BaseClassWithDrop)] struct SubClassWithDrop { token: PyToken, data: Option>,