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

pyany: add PyAny::py() #1751

Merged
merged 1 commit into from
Aug 17, 2021
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<PyErr>`. [#1717](https://github.com/PyO3/pyo3/pull/1717)
Expand Down
2 changes: 1 addition & 1 deletion guide/src/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
3 changes: 1 addition & 2 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/conversions/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mod min_const_generics {
for<'a> T: Default + FromPyObject<'a> + crate::buffer::Element,
{
fn extract(obj: &'source PyAny) -> PyResult<Self> {
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) {
Expand Down
1 change: 0 additions & 1 deletion src/conversions/osstr.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/conversions/path.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
1 change: 0 additions & 1 deletion src/derive_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
3 changes: 1 addition & 2 deletions src/err/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))?;
Expand Down
1 change: 1 addition & 0 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
}
Expand Down
4 changes: 2 additions & 2 deletions src/num_bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
4 changes: 2 additions & 2 deletions src/num_complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/type_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion src/types/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -676,6 +676,12 @@ impl PyAny {
pub fn is_instance<T: PyTypeObject>(&self) -> PyResult<bool> {
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)]
Expand Down
1 change: 0 additions & 1 deletion src/types/bytearray.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/types/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/types/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/types/floatob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/types/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
3 changes: 1 addition & 2 deletions src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
1 change: 0 additions & 1 deletion src/types/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
4 changes: 2 additions & 2 deletions src/types/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/types/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
3 changes: 1 addition & 2 deletions src/types/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
1 change: 0 additions & 1 deletion src/types/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/types/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/types/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion src/types/typeobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
1 change: 0 additions & 1 deletion tests/test_arithmetics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use pyo3::class::basic::CompareOp;
use pyo3::class::*;
use pyo3::prelude::*;
use pyo3::py_run;
use pyo3::PyNativeType;

mod common;

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/wrong_aspyref_lifetimes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use pyo3::{types::PyDict, Py, PyNativeType, Python};
use pyo3::{types::PyDict, Py, Python};

fn main() {
let gil = Python::acquire_gil();
Expand Down