diff --git a/CHANGELOG.md b/CHANGELOG.md index f33840b0596..91800c5020d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added + +- Add `PyAny::py()` as a convenience for `PyNativeType::py()`. [#1751](https://github.com/PyO3/pyo3/pull/1751) + ### Changed - Change `PyErr::fetch()` to return `Option`. [#1717](https://github.com/PyO3/pyo3/pull/1717) diff --git a/guide/src/class.md b/guide/src/class.md index 3b5d3509cfb..cbe3dc3cb36 100644 --- a/guide/src/class.md +++ b/guide/src/class.md @@ -293,7 +293,7 @@ that inherit native types. Even in such cases, you can unsafely get a base class # #[cfg(not(Py_LIMITED_API))] { # use pyo3::prelude::*; use pyo3::types::PyDict; -use pyo3::{AsPyPointer, PyNativeType}; +use pyo3::AsPyPointer; use std::collections::HashMap; #[pyclass(extends=PyDict)] diff --git a/src/buffer.rs b/src/buffer.rs index fbded572133..e87d2356a72 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -18,8 +18,7 @@ //! `PyBuffer` implementation use crate::{ - err, exceptions::PyBufferError, ffi, AsPyPointer, FromPyObject, PyAny, PyNativeType, PyResult, - Python, + err, exceptions::PyBufferError, ffi, AsPyPointer, FromPyObject, PyAny, PyResult, Python, }; use std::marker::PhantomData; use std::os::raw; diff --git a/src/conversions/array.rs b/src/conversions/array.rs index 31cde938242..c586a5d725a 100644 --- a/src/conversions/array.rs +++ b/src/conversions/array.rs @@ -35,7 +35,7 @@ mod min_const_generics { for<'a> T: Default + FromPyObject<'a> + crate::buffer::Element, { fn extract(obj: &'source PyAny) -> PyResult { - use crate::{AsPyPointer, PyNativeType}; + use crate::AsPyPointer; // first try buffer protocol if unsafe { crate::ffi::PyObject_CheckBuffer(obj.as_ptr()) } == 1 { if let Ok(buf) = crate::buffer::PyBuffer::get(obj) { diff --git a/src/conversions/osstr.rs b/src/conversions/osstr.rs index ff874a97df6..b42416ee0aa 100644 --- a/src/conversions/osstr.rs +++ b/src/conversions/osstr.rs @@ -1,7 +1,6 @@ use crate::types::PyString; #[cfg(windows)] use crate::PyErr; -use crate::PyNativeType; use crate::{ ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyObject, PyResult, PyTryFrom, Python, ToPyObject, diff --git a/src/conversions/path.rs b/src/conversions/path.rs index 68affa2bd06..ab1689b360d 100644 --- a/src/conversions/path.rs +++ b/src/conversions/path.rs @@ -1,5 +1,5 @@ use crate::types::PyType; -use crate::{FromPyObject, IntoPy, PyAny, PyNativeType, PyObject, PyResult, Python, ToPyObject}; +use crate::{FromPyObject, IntoPy, PyAny, PyObject, PyResult, Python, ToPyObject}; use std::borrow::Cow; use std::ffi::OsString; use std::path::{Path, PathBuf}; diff --git a/src/derive_utils.rs b/src/derive_utils.rs index a175fd3d304..6d79b2c4dad 100644 --- a/src/derive_utils.rs +++ b/src/derive_utils.rs @@ -6,7 +6,6 @@ use crate::err::{PyErr, PyResult}; use crate::exceptions::PyTypeError; -use crate::instance::PyNativeType; use crate::pyclass::PyClass; use crate::types::{PyAny, PyDict, PyModule, PyString, PyTuple}; use crate::{ffi, PyCell, Python}; diff --git a/src/err/mod.rs b/src/err/mod.rs index 4f91013d0f9..fdbd145b97e 100644 --- a/src/err/mod.rs +++ b/src/err/mod.rs @@ -8,8 +8,7 @@ use crate::{ ffi, }; use crate::{ - AsPyPointer, FromPyPointer, IntoPy, Py, PyAny, PyNativeType, PyObject, Python, - ToBorrowedObject, ToPyObject, + AsPyPointer, FromPyPointer, IntoPy, Py, PyAny, PyObject, Python, ToBorrowedObject, ToPyObject, }; use std::borrow::Cow; use std::cell::UnsafeCell; diff --git a/src/exceptions.rs b/src/exceptions.rs index f98b0932c0f..db43728ae48 100644 --- a/src/exceptions.rs +++ b/src/exceptions.rs @@ -30,7 +30,7 @@ macro_rules! impl_exception_boilerplate { impl std::error::Error for $name { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { unsafe { - use $crate::{AsPyPointer, PyNativeType}; + use $crate::AsPyPointer; let cause: &$crate::exceptions::PyBaseException = self .py() .from_owned_ptr_or_opt($crate::ffi::PyException_GetCause(self.as_ptr()))?; diff --git a/src/instance.rs b/src/instance.rs index b93511292f8..9d7d48b5693 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -19,6 +19,7 @@ use std::ptr::NonNull; /// types. pub unsafe trait PyNativeType: Sized { /// Returns a GIL marker constrained to the lifetime of this type. + #[inline] fn py(&self) -> Python { unsafe { Python::assume_gil_acquired() } } diff --git a/src/num_bigint.rs b/src/num_bigint.rs index 27ec0668b20..bc2494865d5 100644 --- a/src/num_bigint.rs +++ b/src/num_bigint.rs @@ -62,8 +62,8 @@ //! ``` use crate::{ - err, ffi, types::*, AsPyPointer, FromPyObject, IntoPy, Py, PyAny, PyErr, PyNativeType, - PyObject, PyResult, Python, ToPyObject, + err, ffi, types::*, AsPyPointer, FromPyObject, IntoPy, Py, PyAny, PyErr, PyObject, PyResult, + Python, ToPyObject, }; use num_bigint::{BigInt, BigUint}; diff --git a/src/num_complex.rs b/src/num_complex.rs index 1eda1779103..8a3985e2a18 100644 --- a/src/num_complex.rs +++ b/src/num_complex.rs @@ -100,8 +100,8 @@ //! assert result == [complex(1,-1), complex(-2,0)] //! ``` use crate::{ - ffi, types::PyComplex, AsPyPointer, FromPyObject, PyAny, PyErr, PyNativeType, PyObject, - PyResult, Python, ToPyObject, + ffi, types::PyComplex, AsPyPointer, FromPyObject, PyAny, PyErr, PyObject, PyResult, Python, + ToPyObject, }; use num_complex::Complex; use std::os::raw::c_double; diff --git a/src/type_object.rs b/src/type_object.rs index bcaed4523fa..0f16f566f99 100644 --- a/src/type_object.rs +++ b/src/type_object.rs @@ -39,7 +39,7 @@ pub unsafe trait PyTypeInfo: Sized { const MODULE: Option<&'static str>; /// Utility type to make Py::as_ref work. - type AsRefTarget: crate::PyNativeType; + type AsRefTarget: PyNativeType; /// PyTypeObject instance for this type. fn type_object_raw(py: Python) -> *mut ffi::PyTypeObject; diff --git a/src/types/any.rs b/src/types/any.rs index a1c7b9b1ba3..eebbe96eb6f 100644 --- a/src/types/any.rs +++ b/src/types/any.rs @@ -6,7 +6,7 @@ use crate::err::{PyDowncastError, PyErr, PyResult}; use crate::exceptions::PyTypeError; use crate::type_object::PyTypeObject; use crate::types::{PyDict, PyIterator, PyList, PyString, PyTuple, PyType}; -use crate::{err, ffi, Py, PyNativeType, PyObject}; +use crate::{err, ffi, Py, PyNativeType, PyObject, Python}; use std::cell::UnsafeCell; use std::cmp::Ordering; use std::os::raw::c_int; @@ -676,6 +676,12 @@ impl PyAny { pub fn is_instance(&self) -> PyResult { T::type_object(self.py()).is_instance(self) } + + /// Returns a GIL marker constrained to the lifetime of this type. + #[inline] + pub fn py(&self) -> Python<'_> { + PyNativeType::py(self) + } } #[cfg(test)] diff --git a/src/types/bytearray.rs b/src/types/bytearray.rs index ef859d2d268..7e9349abf56 100644 --- a/src/types/bytearray.rs +++ b/src/types/bytearray.rs @@ -1,6 +1,5 @@ // Copyright (c) 2017-present PyO3 Project and Contributors use crate::err::{PyErr, PyResult}; -use crate::instance::PyNativeType; use crate::{ffi, AsPyPointer, Py, PyAny, Python}; use std::os::raw::c_char; use std::slice; diff --git a/src/types/complex.rs b/src/types/complex.rs index dae969e6d5f..a294bfc71a8 100644 --- a/src/types/complex.rs +++ b/src/types/complex.rs @@ -40,7 +40,6 @@ impl PyComplex { #[cfg_attr(docsrs, doc(cfg(not(any(Py_LIMITED_API, PyPy)))))] mod not_limited_impls { use super::*; - use crate::instance::PyNativeType; use std::ops::{Add, Div, Mul, Neg, Sub}; impl PyComplex { diff --git a/src/types/dict.rs b/src/types/dict.rs index 7d05d8a5401..fbf8390fce0 100644 --- a/src/types/dict.rs +++ b/src/types/dict.rs @@ -5,8 +5,8 @@ use crate::types::{PyAny, PyList}; #[cfg(not(PyPy))] use crate::IntoPyPointer; use crate::{ - ffi, AsPyPointer, FromPyObject, IntoPy, PyNativeType, PyObject, PyTryFrom, Python, - ToBorrowedObject, ToPyObject, + ffi, AsPyPointer, FromPyObject, IntoPy, PyObject, PyTryFrom, Python, ToBorrowedObject, + ToPyObject, }; use std::collections::{BTreeMap, HashMap}; use std::ptr::NonNull; diff --git a/src/types/floatob.rs b/src/types/floatob.rs index 96257c90fb1..e476ff8f75f 100644 --- a/src/types/floatob.rs +++ b/src/types/floatob.rs @@ -2,8 +2,7 @@ // // based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython use crate::{ - ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyErr, PyNativeType, PyObject, PyResult, Python, - ToPyObject, + ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python, ToPyObject, }; use std::os::raw::c_double; diff --git a/src/types/iterator.rs b/src/types/iterator.rs index d615eff2c9e..6148196f663 100644 --- a/src/types/iterator.rs +++ b/src/types/iterator.rs @@ -2,7 +2,7 @@ // // based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython -use crate::{ffi, AsPyPointer, PyAny, PyErr, PyNativeType, PyResult, Python}; +use crate::{ffi, AsPyPointer, PyAny, PyErr, PyResult, Python}; #[cfg(any(not(Py_LIMITED_API), Py_3_8))] use crate::{PyDowncastError, PyTryFrom}; diff --git a/src/types/list.rs b/src/types/list.rs index 535f676f82f..bcec829b2e3 100644 --- a/src/types/list.rs +++ b/src/types/list.rs @@ -5,8 +5,7 @@ use crate::err::{self, PyResult}; use crate::ffi::{self, Py_ssize_t}; use crate::{ - AsPyPointer, IntoPy, IntoPyPointer, PyAny, PyNativeType, PyObject, Python, ToBorrowedObject, - ToPyObject, + AsPyPointer, IntoPy, IntoPyPointer, PyAny, PyObject, Python, ToBorrowedObject, ToPyObject, }; /// Represents a Python `list`. diff --git a/src/types/module.rs b/src/types/module.rs index 207b0eb232f..4d9c43559a2 100644 --- a/src/types/module.rs +++ b/src/types/module.rs @@ -6,7 +6,6 @@ use crate::callback::IntoPyCallbackOutput; use crate::err::{PyErr, PyResult}; use crate::exceptions; use crate::ffi; -use crate::instance::PyNativeType; use crate::pyclass::PyClass; use crate::type_object::PyTypeObject; use crate::types::{PyAny, PyDict, PyList}; diff --git a/src/types/num.rs b/src/types/num.rs index 710d9fbbb3d..44dea0f88b6 100644 --- a/src/types/num.rs +++ b/src/types/num.rs @@ -3,8 +3,8 @@ // based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython use crate::{ - exceptions, ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyErr, PyNativeType, PyObject, - PyResult, Python, ToPyObject, + exceptions, ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python, + ToPyObject, }; use std::convert::TryFrom; use std::i64; diff --git a/src/types/sequence.rs b/src/types/sequence.rs index 4b49973ff0e..108d3a34b83 100644 --- a/src/types/sequence.rs +++ b/src/types/sequence.rs @@ -2,7 +2,6 @@ use crate::err::{self, PyDowncastError, PyErr, PyResult}; use crate::ffi::{self, Py_ssize_t}; -use crate::instance::PyNativeType; use crate::types::{PyAny, PyList, PyTuple}; use crate::AsPyPointer; use crate::{FromPyObject, PyTryFrom, ToBorrowedObject}; diff --git a/src/types/set.rs b/src/types/set.rs index daa959f64fe..6218f5e86dd 100644 --- a/src/types/set.rs +++ b/src/types/set.rs @@ -5,8 +5,7 @@ use crate::err::{self, PyErr, PyResult}; #[cfg(Py_LIMITED_API)] use crate::types::PyIterator; use crate::{ - ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyNativeType, PyObject, Python, - ToBorrowedObject, ToPyObject, + ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyObject, Python, ToBorrowedObject, ToPyObject, }; use std::cmp; use std::collections::{BTreeSet, HashSet}; diff --git a/src/types/slice.rs b/src/types/slice.rs index dee887291df..2b88beb3fe4 100644 --- a/src/types/slice.rs +++ b/src/types/slice.rs @@ -2,7 +2,6 @@ use crate::err::{PyErr, PyResult}; use crate::ffi::{self, Py_ssize_t}; -use crate::instance::PyNativeType; use crate::{AsPyPointer, PyAny, PyObject, Python, ToPyObject}; use std::os::raw::c_long; diff --git a/src/types/string.rs b/src/types/string.rs index 5cf39a5094b..fc45d0a32b0 100644 --- a/src/types/string.rs +++ b/src/types/string.rs @@ -2,8 +2,8 @@ use crate::types::PyBytes; use crate::{ - ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyNativeType, PyObject, PyResult, PyTryFrom, - Python, ToPyObject, + ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyObject, PyResult, PyTryFrom, Python, + ToPyObject, }; use std::borrow::Cow; use std::os::raw::c_char; diff --git a/src/types/tuple.rs b/src/types/tuple.rs index 4c09b75f240..c5c4cd1df24 100644 --- a/src/types/tuple.rs +++ b/src/types/tuple.rs @@ -2,8 +2,8 @@ use crate::ffi::{self, Py_ssize_t}; use crate::{ - exceptions, AsPyPointer, FromPyObject, IntoPy, IntoPyPointer, Py, PyAny, PyErr, PyNativeType, - PyObject, PyResult, PyTryFrom, Python, ToPyObject, + exceptions, AsPyPointer, FromPyObject, IntoPy, IntoPyPointer, Py, PyAny, PyErr, PyObject, + PyResult, PyTryFrom, Python, ToPyObject, }; /// Represents a Python `tuple` object. diff --git a/src/types/typeobject.rs b/src/types/typeobject.rs index b16b9f3e733..09a1f8f29ec 100644 --- a/src/types/typeobject.rs +++ b/src/types/typeobject.rs @@ -3,7 +3,6 @@ // based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython use crate::err::{self, PyResult}; -use crate::instance::PyNativeType; use crate::type_object::PyTypeObject; use crate::{ffi, AsPyPointer, PyAny, Python}; diff --git a/tests/test_arithmetics.rs b/tests/test_arithmetics.rs index 19c30eb98d6..dc64155ecb9 100644 --- a/tests/test_arithmetics.rs +++ b/tests/test_arithmetics.rs @@ -4,7 +4,6 @@ use pyo3::class::basic::CompareOp; use pyo3::class::*; use pyo3::prelude::*; use pyo3::py_run; -use pyo3::PyNativeType; mod common; diff --git a/tests/ui/wrong_aspyref_lifetimes.rs b/tests/ui/wrong_aspyref_lifetimes.rs index 5dcd84d1d6c..76c7e196d85 100644 --- a/tests/ui/wrong_aspyref_lifetimes.rs +++ b/tests/ui/wrong_aspyref_lifetimes.rs @@ -1,4 +1,4 @@ -use pyo3::{types::PyDict, Py, PyNativeType, Python}; +use pyo3::{types::PyDict, Py, Python}; fn main() { let gil = Python::acquire_gil();