Skip to content

Commit 37d39aa

Browse files
authored
Merge pull request #1751 from davidhewitt/pyany-py
pyany: add PyAny::py()
2 parents df41903 + ebada76 commit 37d39aa

30 files changed

+36
-39
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
88

99
## [Unreleased]
1010

11+
### Added
12+
13+
- Add `PyAny::py()` as a convenience for `PyNativeType::py()`. [#1751](https://github.com/PyO3/pyo3/pull/1751)
14+
1115
### Changed
1216

1317
- Change `PyErr::fetch()` to return `Option<PyErr>`. [#1717](https://github.com/PyO3/pyo3/pull/1717)

guide/src/class.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ that inherit native types. Even in such cases, you can unsafely get a base class
293293
# #[cfg(not(Py_LIMITED_API))] {
294294
# use pyo3::prelude::*;
295295
use pyo3::types::PyDict;
296-
use pyo3::{AsPyPointer, PyNativeType};
296+
use pyo3::AsPyPointer;
297297
use std::collections::HashMap;
298298

299299
#[pyclass(extends=PyDict)]

src/buffer.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818

1919
//! `PyBuffer` implementation
2020
use crate::{
21-
err, exceptions::PyBufferError, ffi, AsPyPointer, FromPyObject, PyAny, PyNativeType, PyResult,
22-
Python,
21+
err, exceptions::PyBufferError, ffi, AsPyPointer, FromPyObject, PyAny, PyResult, Python,
2322
};
2423
use std::marker::PhantomData;
2524
use std::os::raw;

src/conversions/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ mod min_const_generics {
3535
for<'a> T: Default + FromPyObject<'a> + crate::buffer::Element,
3636
{
3737
fn extract(obj: &'source PyAny) -> PyResult<Self> {
38-
use crate::{AsPyPointer, PyNativeType};
38+
use crate::AsPyPointer;
3939
// first try buffer protocol
4040
if unsafe { crate::ffi::PyObject_CheckBuffer(obj.as_ptr()) } == 1 {
4141
if let Ok(buf) = crate::buffer::PyBuffer::get(obj) {

src/conversions/osstr.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::types::PyString;
22
#[cfg(windows)]
33
use crate::PyErr;
4-
use crate::PyNativeType;
54
use crate::{
65
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyObject, PyResult, PyTryFrom, Python,
76
ToPyObject,

src/conversions/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::types::PyType;
2-
use crate::{FromPyObject, IntoPy, PyAny, PyNativeType, PyObject, PyResult, Python, ToPyObject};
2+
use crate::{FromPyObject, IntoPy, PyAny, PyObject, PyResult, Python, ToPyObject};
33
use std::borrow::Cow;
44
use std::ffi::OsString;
55
use std::path::{Path, PathBuf};

src/derive_utils.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
77
use crate::err::{PyErr, PyResult};
88
use crate::exceptions::PyTypeError;
9-
use crate::instance::PyNativeType;
109
use crate::pyclass::PyClass;
1110
use crate::types::{PyAny, PyDict, PyModule, PyString, PyTuple};
1211
use crate::{ffi, PyCell, Python};

src/err/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ use crate::{
88
ffi,
99
};
1010
use crate::{
11-
AsPyPointer, FromPyPointer, IntoPy, Py, PyAny, PyNativeType, PyObject, Python,
12-
ToBorrowedObject, ToPyObject,
11+
AsPyPointer, FromPyPointer, IntoPy, Py, PyAny, PyObject, Python, ToBorrowedObject, ToPyObject,
1312
};
1413
use std::borrow::Cow;
1514
use std::cell::UnsafeCell;

src/exceptions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ macro_rules! impl_exception_boilerplate {
3030
impl std::error::Error for $name {
3131
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
3232
unsafe {
33-
use $crate::{AsPyPointer, PyNativeType};
33+
use $crate::AsPyPointer;
3434
let cause: &$crate::exceptions::PyBaseException = self
3535
.py()
3636
.from_owned_ptr_or_opt($crate::ffi::PyException_GetCause(self.as_ptr()))?;

src/instance.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use std::ptr::NonNull;
1919
/// types.
2020
pub unsafe trait PyNativeType: Sized {
2121
/// Returns a GIL marker constrained to the lifetime of this type.
22+
#[inline]
2223
fn py(&self) -> Python {
2324
unsafe { Python::assume_gil_acquired() }
2425
}

src/num_bigint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
6262
//! ```
6363
6464
use crate::{
65-
err, ffi, types::*, AsPyPointer, FromPyObject, IntoPy, Py, PyAny, PyErr, PyNativeType,
66-
PyObject, PyResult, Python, ToPyObject,
65+
err, ffi, types::*, AsPyPointer, FromPyObject, IntoPy, Py, PyAny, PyErr, PyObject, PyResult,
66+
Python, ToPyObject,
6767
};
6868

6969
use num_bigint::{BigInt, BigUint};

src/num_complex.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@
100100
//! assert result == [complex(1,-1), complex(-2,0)]
101101
//! ```
102102
use crate::{
103-
ffi, types::PyComplex, AsPyPointer, FromPyObject, PyAny, PyErr, PyNativeType, PyObject,
104-
PyResult, Python, ToPyObject,
103+
ffi, types::PyComplex, AsPyPointer, FromPyObject, PyAny, PyErr, PyObject, PyResult, Python,
104+
ToPyObject,
105105
};
106106
use num_complex::Complex;
107107
use std::os::raw::c_double;

src/type_object.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub unsafe trait PyTypeInfo: Sized {
3939
const MODULE: Option<&'static str>;
4040

4141
/// Utility type to make Py::as_ref work.
42-
type AsRefTarget: crate::PyNativeType;
42+
type AsRefTarget: PyNativeType;
4343

4444
/// PyTypeObject instance for this type.
4545
fn type_object_raw(py: Python) -> *mut ffi::PyTypeObject;

src/types/any.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::err::{PyDowncastError, PyErr, PyResult};
66
use crate::exceptions::PyTypeError;
77
use crate::type_object::PyTypeObject;
88
use crate::types::{PyDict, PyIterator, PyList, PyString, PyTuple, PyType};
9-
use crate::{err, ffi, Py, PyNativeType, PyObject};
9+
use crate::{err, ffi, Py, PyNativeType, PyObject, Python};
1010
use std::cell::UnsafeCell;
1111
use std::cmp::Ordering;
1212
use std::os::raw::c_int;
@@ -676,6 +676,12 @@ impl PyAny {
676676
pub fn is_instance<T: PyTypeObject>(&self) -> PyResult<bool> {
677677
T::type_object(self.py()).is_instance(self)
678678
}
679+
680+
/// Returns a GIL marker constrained to the lifetime of this type.
681+
#[inline]
682+
pub fn py(&self) -> Python<'_> {
683+
PyNativeType::py(self)
684+
}
679685
}
680686

681687
#[cfg(test)]

src/types/bytearray.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) 2017-present PyO3 Project and Contributors
22
use crate::err::{PyErr, PyResult};
3-
use crate::instance::PyNativeType;
43
use crate::{ffi, AsPyPointer, Py, PyAny, Python};
54
use std::os::raw::c_char;
65
use std::slice;

src/types/complex.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ impl PyComplex {
4040
#[cfg_attr(docsrs, doc(cfg(not(any(Py_LIMITED_API, PyPy)))))]
4141
mod not_limited_impls {
4242
use super::*;
43-
use crate::instance::PyNativeType;
4443
use std::ops::{Add, Div, Mul, Neg, Sub};
4544

4645
impl PyComplex {

src/types/dict.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use crate::types::{PyAny, PyList};
55
#[cfg(not(PyPy))]
66
use crate::IntoPyPointer;
77
use crate::{
8-
ffi, AsPyPointer, FromPyObject, IntoPy, PyNativeType, PyObject, PyTryFrom, Python,
9-
ToBorrowedObject, ToPyObject,
8+
ffi, AsPyPointer, FromPyObject, IntoPy, PyObject, PyTryFrom, Python, ToBorrowedObject,
9+
ToPyObject,
1010
};
1111
use std::collections::{BTreeMap, HashMap};
1212
use std::ptr::NonNull;

src/types/floatob.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
//
33
// based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython
44
use crate::{
5-
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyErr, PyNativeType, PyObject, PyResult, Python,
6-
ToPyObject,
5+
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python, ToPyObject,
76
};
87
use std::os::raw::c_double;
98

src/types/iterator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython
44

5-
use crate::{ffi, AsPyPointer, PyAny, PyErr, PyNativeType, PyResult, Python};
5+
use crate::{ffi, AsPyPointer, PyAny, PyErr, PyResult, Python};
66
#[cfg(any(not(Py_LIMITED_API), Py_3_8))]
77
use crate::{PyDowncastError, PyTryFrom};
88

src/types/list.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use crate::err::{self, PyResult};
66
use crate::ffi::{self, Py_ssize_t};
77
use crate::internal_tricks::get_ssize_index;
88
use crate::{
9-
AsPyPointer, IntoPy, IntoPyPointer, PyAny, PyNativeType, PyObject, Python, ToBorrowedObject,
10-
ToPyObject,
9+
AsPyPointer, IntoPy, IntoPyPointer, PyAny, PyObject, Python, ToBorrowedObject, ToPyObject,
1110
};
1211

1312
/// Represents a Python `list`.

src/types/module.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::callback::IntoPyCallbackOutput;
66
use crate::err::{PyErr, PyResult};
77
use crate::exceptions;
88
use crate::ffi;
9-
use crate::instance::PyNativeType;
109
use crate::pyclass::PyClass;
1110
use crate::type_object::PyTypeObject;
1211
use crate::types::{PyAny, PyDict, PyList};

src/types/num.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython
44

55
use crate::{
6-
exceptions, ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyErr, PyNativeType, PyObject,
7-
PyResult, Python, ToPyObject,
6+
exceptions, ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python,
7+
ToPyObject,
88
};
99
use std::convert::TryFrom;
1010
use std::i64;

src/types/sequence.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use crate::err::{self, PyDowncastError, PyErr, PyResult};
44
use crate::ffi::{self, Py_ssize_t};
5-
use crate::instance::PyNativeType;
65
use crate::types::{PyAny, PyList, PyTuple};
76
use crate::AsPyPointer;
87
use crate::{FromPyObject, PyTryFrom, ToBorrowedObject};

src/types/set.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use crate::err::{self, PyErr, PyResult};
55
#[cfg(Py_LIMITED_API)]
66
use crate::types::PyIterator;
77
use crate::{
8-
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyNativeType, PyObject, Python,
9-
ToBorrowedObject, ToPyObject,
8+
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyObject, Python, ToBorrowedObject, ToPyObject,
109
};
1110
use std::cmp;
1211
use std::collections::{BTreeSet, HashSet};

src/types/slice.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use crate::err::{PyErr, PyResult};
44
use crate::ffi::{self, Py_ssize_t};
5-
use crate::instance::PyNativeType;
65
use crate::{AsPyPointer, PyAny, PyObject, Python, ToPyObject};
76
use std::os::raw::c_long;
87

src/types/string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
use crate::types::PyBytes;
44
use crate::{
5-
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyNativeType, PyObject, PyResult, PyTryFrom,
6-
Python, ToPyObject,
5+
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyObject, PyResult, PyTryFrom, Python,
6+
ToPyObject,
77
};
88
use std::borrow::Cow;
99
use std::os::raw::c_char;

src/types/tuple.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
use crate::ffi::{self, Py_ssize_t};
44
use crate::internal_tricks::get_ssize_index;
55
use crate::{
6-
exceptions, AsPyPointer, FromPyObject, IntoPy, IntoPyPointer, Py, PyAny, PyErr, PyNativeType,
7-
PyObject, PyResult, PyTryFrom, Python, ToPyObject,
6+
exceptions, AsPyPointer, FromPyObject, IntoPy, IntoPyPointer, Py, PyAny, PyErr, PyObject,
7+
PyResult, PyTryFrom, Python, ToPyObject,
88
};
99

1010
/// Represents a Python `tuple` object.

src/types/typeobject.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython
44

55
use crate::err::{self, PyResult};
6-
use crate::instance::PyNativeType;
76
use crate::type_object::PyTypeObject;
87
use crate::{ffi, AsPyPointer, PyAny, Python};
98

tests/test_arithmetics.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use pyo3::class::basic::CompareOp;
44
use pyo3::class::*;
55
use pyo3::prelude::*;
66
use pyo3::py_run;
7-
use pyo3::PyNativeType;
87

98
mod common;
109

tests/ui/wrong_aspyref_lifetimes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use pyo3::{types::PyDict, Py, PyNativeType, Python};
1+
use pyo3::{types::PyDict, Py, Python};
22

33
fn main() {
44
let gil = Python::acquire_gil();

0 commit comments

Comments
 (0)