Merge pull request #1751 from davidhewitt/pyany-py
pyany: add PyAny::py()
This commit is contained in:
commit
37d39aa83a
|
@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- Add `PyAny::py()` as a convenience for `PyNativeType::py()`. [#1751](https://github.com/PyO3/pyo3/pull/1751)
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
- Change `PyErr::fetch()` to return `Option<PyErr>`. [#1717](https://github.com/PyO3/pyo3/pull/1717)
|
- Change `PyErr::fetch()` to return `Option<PyErr>`. [#1717](https://github.com/PyO3/pyo3/pull/1717)
|
||||||
|
|
|
@ -293,7 +293,7 @@ that inherit native types. Even in such cases, you can unsafely get a base class
|
||||||
# #[cfg(not(Py_LIMITED_API))] {
|
# #[cfg(not(Py_LIMITED_API))] {
|
||||||
# use pyo3::prelude::*;
|
# use pyo3::prelude::*;
|
||||||
use pyo3::types::PyDict;
|
use pyo3::types::PyDict;
|
||||||
use pyo3::{AsPyPointer, PyNativeType};
|
use pyo3::AsPyPointer;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
#[pyclass(extends=PyDict)]
|
#[pyclass(extends=PyDict)]
|
||||||
|
|
|
@ -18,8 +18,7 @@
|
||||||
|
|
||||||
//! `PyBuffer` implementation
|
//! `PyBuffer` implementation
|
||||||
use crate::{
|
use crate::{
|
||||||
err, exceptions::PyBufferError, ffi, AsPyPointer, FromPyObject, PyAny, PyNativeType, PyResult,
|
err, exceptions::PyBufferError, ffi, AsPyPointer, FromPyObject, PyAny, PyResult, Python,
|
||||||
Python,
|
|
||||||
};
|
};
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::os::raw;
|
use std::os::raw;
|
||||||
|
|
|
@ -35,7 +35,7 @@ mod min_const_generics {
|
||||||
for<'a> T: Default + FromPyObject<'a> + crate::buffer::Element,
|
for<'a> T: Default + FromPyObject<'a> + crate::buffer::Element,
|
||||||
{
|
{
|
||||||
fn extract(obj: &'source PyAny) -> PyResult<Self> {
|
fn extract(obj: &'source PyAny) -> PyResult<Self> {
|
||||||
use crate::{AsPyPointer, PyNativeType};
|
use crate::AsPyPointer;
|
||||||
// first try buffer protocol
|
// first try buffer protocol
|
||||||
if unsafe { crate::ffi::PyObject_CheckBuffer(obj.as_ptr()) } == 1 {
|
if unsafe { crate::ffi::PyObject_CheckBuffer(obj.as_ptr()) } == 1 {
|
||||||
if let Ok(buf) = crate::buffer::PyBuffer::get(obj) {
|
if let Ok(buf) = crate::buffer::PyBuffer::get(obj) {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use crate::types::PyString;
|
use crate::types::PyString;
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
use crate::PyErr;
|
use crate::PyErr;
|
||||||
use crate::PyNativeType;
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyObject, PyResult, PyTryFrom, Python,
|
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyObject, PyResult, PyTryFrom, Python,
|
||||||
ToPyObject,
|
ToPyObject,
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::types::PyType;
|
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::borrow::Cow;
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
|
|
||||||
use crate::err::{PyErr, PyResult};
|
use crate::err::{PyErr, PyResult};
|
||||||
use crate::exceptions::PyTypeError;
|
use crate::exceptions::PyTypeError;
|
||||||
use crate::instance::PyNativeType;
|
|
||||||
use crate::pyclass::PyClass;
|
use crate::pyclass::PyClass;
|
||||||
use crate::types::{PyAny, PyDict, PyModule, PyString, PyTuple};
|
use crate::types::{PyAny, PyDict, PyModule, PyString, PyTuple};
|
||||||
use crate::{ffi, PyCell, Python};
|
use crate::{ffi, PyCell, Python};
|
||||||
|
|
|
@ -8,8 +8,7 @@ use crate::{
|
||||||
ffi,
|
ffi,
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
AsPyPointer, FromPyPointer, IntoPy, Py, PyAny, PyNativeType, PyObject, Python,
|
AsPyPointer, FromPyPointer, IntoPy, Py, PyAny, PyObject, Python, ToBorrowedObject, ToPyObject,
|
||||||
ToBorrowedObject, ToPyObject,
|
|
||||||
};
|
};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::cell::UnsafeCell;
|
use std::cell::UnsafeCell;
|
||||||
|
|
|
@ -30,7 +30,7 @@ macro_rules! impl_exception_boilerplate {
|
||||||
impl std::error::Error for $name {
|
impl std::error::Error for $name {
|
||||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||||
unsafe {
|
unsafe {
|
||||||
use $crate::{AsPyPointer, PyNativeType};
|
use $crate::AsPyPointer;
|
||||||
let cause: &$crate::exceptions::PyBaseException = self
|
let cause: &$crate::exceptions::PyBaseException = self
|
||||||
.py()
|
.py()
|
||||||
.from_owned_ptr_or_opt($crate::ffi::PyException_GetCause(self.as_ptr()))?;
|
.from_owned_ptr_or_opt($crate::ffi::PyException_GetCause(self.as_ptr()))?;
|
||||||
|
|
|
@ -19,6 +19,7 @@ use std::ptr::NonNull;
|
||||||
/// types.
|
/// types.
|
||||||
pub unsafe trait PyNativeType: Sized {
|
pub unsafe trait PyNativeType: Sized {
|
||||||
/// Returns a GIL marker constrained to the lifetime of this type.
|
/// Returns a GIL marker constrained to the lifetime of this type.
|
||||||
|
#[inline]
|
||||||
fn py(&self) -> Python {
|
fn py(&self) -> Python {
|
||||||
unsafe { Python::assume_gil_acquired() }
|
unsafe { Python::assume_gil_acquired() }
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,8 +62,8 @@
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
err, ffi, types::*, AsPyPointer, FromPyObject, IntoPy, Py, PyAny, PyErr, PyNativeType,
|
err, ffi, types::*, AsPyPointer, FromPyObject, IntoPy, Py, PyAny, PyErr, PyObject, PyResult,
|
||||||
PyObject, PyResult, Python, ToPyObject,
|
Python, ToPyObject,
|
||||||
};
|
};
|
||||||
|
|
||||||
use num_bigint::{BigInt, BigUint};
|
use num_bigint::{BigInt, BigUint};
|
||||||
|
|
|
@ -100,8 +100,8 @@
|
||||||
//! assert result == [complex(1,-1), complex(-2,0)]
|
//! assert result == [complex(1,-1), complex(-2,0)]
|
||||||
//! ```
|
//! ```
|
||||||
use crate::{
|
use crate::{
|
||||||
ffi, types::PyComplex, AsPyPointer, FromPyObject, PyAny, PyErr, PyNativeType, PyObject,
|
ffi, types::PyComplex, AsPyPointer, FromPyObject, PyAny, PyErr, PyObject, PyResult, Python,
|
||||||
PyResult, Python, ToPyObject,
|
ToPyObject,
|
||||||
};
|
};
|
||||||
use num_complex::Complex;
|
use num_complex::Complex;
|
||||||
use std::os::raw::c_double;
|
use std::os::raw::c_double;
|
||||||
|
|
|
@ -39,7 +39,7 @@ pub unsafe trait PyTypeInfo: Sized {
|
||||||
const MODULE: Option<&'static str>;
|
const MODULE: Option<&'static str>;
|
||||||
|
|
||||||
/// Utility type to make Py::as_ref work.
|
/// Utility type to make Py::as_ref work.
|
||||||
type AsRefTarget: crate::PyNativeType;
|
type AsRefTarget: PyNativeType;
|
||||||
|
|
||||||
/// PyTypeObject instance for this type.
|
/// PyTypeObject instance for this type.
|
||||||
fn type_object_raw(py: Python) -> *mut ffi::PyTypeObject;
|
fn type_object_raw(py: Python) -> *mut ffi::PyTypeObject;
|
||||||
|
|
|
@ -6,7 +6,7 @@ use crate::err::{PyDowncastError, PyErr, PyResult};
|
||||||
use crate::exceptions::PyTypeError;
|
use crate::exceptions::PyTypeError;
|
||||||
use crate::type_object::PyTypeObject;
|
use crate::type_object::PyTypeObject;
|
||||||
use crate::types::{PyDict, PyIterator, PyList, PyString, PyTuple, PyType};
|
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::cell::UnsafeCell;
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::os::raw::c_int;
|
use std::os::raw::c_int;
|
||||||
|
@ -676,6 +676,12 @@ impl PyAny {
|
||||||
pub fn is_instance<T: PyTypeObject>(&self) -> PyResult<bool> {
|
pub fn is_instance<T: PyTypeObject>(&self) -> PyResult<bool> {
|
||||||
T::type_object(self.py()).is_instance(self)
|
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)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
// Copyright (c) 2017-present PyO3 Project and Contributors
|
// Copyright (c) 2017-present PyO3 Project and Contributors
|
||||||
use crate::err::{PyErr, PyResult};
|
use crate::err::{PyErr, PyResult};
|
||||||
use crate::instance::PyNativeType;
|
|
||||||
use crate::{ffi, AsPyPointer, Py, PyAny, Python};
|
use crate::{ffi, AsPyPointer, Py, PyAny, Python};
|
||||||
use std::os::raw::c_char;
|
use std::os::raw::c_char;
|
||||||
use std::slice;
|
use std::slice;
|
||||||
|
|
|
@ -40,7 +40,6 @@ impl PyComplex {
|
||||||
#[cfg_attr(docsrs, doc(cfg(not(any(Py_LIMITED_API, PyPy)))))]
|
#[cfg_attr(docsrs, doc(cfg(not(any(Py_LIMITED_API, PyPy)))))]
|
||||||
mod not_limited_impls {
|
mod not_limited_impls {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::instance::PyNativeType;
|
|
||||||
use std::ops::{Add, Div, Mul, Neg, Sub};
|
use std::ops::{Add, Div, Mul, Neg, Sub};
|
||||||
|
|
||||||
impl PyComplex {
|
impl PyComplex {
|
||||||
|
|
|
@ -5,8 +5,8 @@ use crate::types::{PyAny, PyList};
|
||||||
#[cfg(not(PyPy))]
|
#[cfg(not(PyPy))]
|
||||||
use crate::IntoPyPointer;
|
use crate::IntoPyPointer;
|
||||||
use crate::{
|
use crate::{
|
||||||
ffi, AsPyPointer, FromPyObject, IntoPy, PyNativeType, PyObject, PyTryFrom, Python,
|
ffi, AsPyPointer, FromPyObject, IntoPy, PyObject, PyTryFrom, Python, ToBorrowedObject,
|
||||||
ToBorrowedObject, ToPyObject,
|
ToPyObject,
|
||||||
};
|
};
|
||||||
use std::collections::{BTreeMap, HashMap};
|
use std::collections::{BTreeMap, HashMap};
|
||||||
use std::ptr::NonNull;
|
use std::ptr::NonNull;
|
||||||
|
|
|
@ -2,8 +2,7 @@
|
||||||
//
|
//
|
||||||
// based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython
|
// based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython
|
||||||
use crate::{
|
use crate::{
|
||||||
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyErr, PyNativeType, PyObject, PyResult, Python,
|
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python, ToPyObject,
|
||||||
ToPyObject,
|
|
||||||
};
|
};
|
||||||
use std::os::raw::c_double;
|
use std::os::raw::c_double;
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
//
|
//
|
||||||
// based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython
|
// 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))]
|
#[cfg(any(not(Py_LIMITED_API), Py_3_8))]
|
||||||
use crate::{PyDowncastError, PyTryFrom};
|
use crate::{PyDowncastError, PyTryFrom};
|
||||||
|
|
||||||
|
|
|
@ -6,8 +6,7 @@ use crate::err::{self, PyResult};
|
||||||
use crate::ffi::{self, Py_ssize_t};
|
use crate::ffi::{self, Py_ssize_t};
|
||||||
use crate::internal_tricks::get_ssize_index;
|
use crate::internal_tricks::get_ssize_index;
|
||||||
use crate::{
|
use crate::{
|
||||||
AsPyPointer, IntoPy, IntoPyPointer, PyAny, PyNativeType, PyObject, Python, ToBorrowedObject,
|
AsPyPointer, IntoPy, IntoPyPointer, PyAny, PyObject, Python, ToBorrowedObject, ToPyObject,
|
||||||
ToPyObject,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Represents a Python `list`.
|
/// Represents a Python `list`.
|
||||||
|
|
|
@ -6,7 +6,6 @@ use crate::callback::IntoPyCallbackOutput;
|
||||||
use crate::err::{PyErr, PyResult};
|
use crate::err::{PyErr, PyResult};
|
||||||
use crate::exceptions;
|
use crate::exceptions;
|
||||||
use crate::ffi;
|
use crate::ffi;
|
||||||
use crate::instance::PyNativeType;
|
|
||||||
use crate::pyclass::PyClass;
|
use crate::pyclass::PyClass;
|
||||||
use crate::type_object::PyTypeObject;
|
use crate::type_object::PyTypeObject;
|
||||||
use crate::types::{PyAny, PyDict, PyList};
|
use crate::types::{PyAny, PyDict, PyList};
|
||||||
|
|
|
@ -3,8 +3,8 @@
|
||||||
// based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython
|
// based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
exceptions, ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyErr, PyNativeType, PyObject,
|
exceptions, ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python,
|
||||||
PyResult, Python, ToPyObject,
|
ToPyObject,
|
||||||
};
|
};
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::i64;
|
use std::i64;
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
use crate::err::{self, PyDowncastError, PyErr, PyResult};
|
use crate::err::{self, PyDowncastError, PyErr, PyResult};
|
||||||
use crate::ffi::{self, Py_ssize_t};
|
use crate::ffi::{self, Py_ssize_t};
|
||||||
use crate::instance::PyNativeType;
|
|
||||||
use crate::types::{PyAny, PyList, PyTuple};
|
use crate::types::{PyAny, PyList, PyTuple};
|
||||||
use crate::AsPyPointer;
|
use crate::AsPyPointer;
|
||||||
use crate::{FromPyObject, PyTryFrom, ToBorrowedObject};
|
use crate::{FromPyObject, PyTryFrom, ToBorrowedObject};
|
||||||
|
|
|
@ -5,8 +5,7 @@ use crate::err::{self, PyErr, PyResult};
|
||||||
#[cfg(Py_LIMITED_API)]
|
#[cfg(Py_LIMITED_API)]
|
||||||
use crate::types::PyIterator;
|
use crate::types::PyIterator;
|
||||||
use crate::{
|
use crate::{
|
||||||
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyNativeType, PyObject, Python,
|
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyObject, Python, ToBorrowedObject, ToPyObject,
|
||||||
ToBorrowedObject, ToPyObject,
|
|
||||||
};
|
};
|
||||||
use std::cmp;
|
use std::cmp;
|
||||||
use std::collections::{BTreeSet, HashSet};
|
use std::collections::{BTreeSet, HashSet};
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
use crate::err::{PyErr, PyResult};
|
use crate::err::{PyErr, PyResult};
|
||||||
use crate::ffi::{self, Py_ssize_t};
|
use crate::ffi::{self, Py_ssize_t};
|
||||||
use crate::instance::PyNativeType;
|
|
||||||
use crate::{AsPyPointer, PyAny, PyObject, Python, ToPyObject};
|
use crate::{AsPyPointer, PyAny, PyObject, Python, ToPyObject};
|
||||||
use std::os::raw::c_long;
|
use std::os::raw::c_long;
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
|
|
||||||
use crate::types::PyBytes;
|
use crate::types::PyBytes;
|
||||||
use crate::{
|
use crate::{
|
||||||
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyNativeType, PyObject, PyResult, PyTryFrom,
|
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyObject, PyResult, PyTryFrom, Python,
|
||||||
Python, ToPyObject,
|
ToPyObject,
|
||||||
};
|
};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::os::raw::c_char;
|
use std::os::raw::c_char;
|
||||||
|
|
|
@ -3,8 +3,8 @@
|
||||||
use crate::ffi::{self, Py_ssize_t};
|
use crate::ffi::{self, Py_ssize_t};
|
||||||
use crate::internal_tricks::get_ssize_index;
|
use crate::internal_tricks::get_ssize_index;
|
||||||
use crate::{
|
use crate::{
|
||||||
exceptions, AsPyPointer, FromPyObject, IntoPy, IntoPyPointer, Py, PyAny, PyErr, PyNativeType,
|
exceptions, AsPyPointer, FromPyObject, IntoPy, IntoPyPointer, Py, PyAny, PyErr, PyObject,
|
||||||
PyObject, PyResult, PyTryFrom, Python, ToPyObject,
|
PyResult, PyTryFrom, Python, ToPyObject,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Represents a Python `tuple` object.
|
/// Represents a Python `tuple` object.
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
// based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython
|
// based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython
|
||||||
|
|
||||||
use crate::err::{self, PyResult};
|
use crate::err::{self, PyResult};
|
||||||
use crate::instance::PyNativeType;
|
|
||||||
use crate::type_object::PyTypeObject;
|
use crate::type_object::PyTypeObject;
|
||||||
use crate::{ffi, AsPyPointer, PyAny, Python};
|
use crate::{ffi, AsPyPointer, PyAny, Python};
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,6 @@ use pyo3::class::basic::CompareOp;
|
||||||
use pyo3::class::*;
|
use pyo3::class::*;
|
||||||
use pyo3::prelude::*;
|
use pyo3::prelude::*;
|
||||||
use pyo3::py_run;
|
use pyo3::py_run;
|
||||||
use pyo3::PyNativeType;
|
|
||||||
|
|
||||||
mod common;
|
mod common;
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use pyo3::{types::PyDict, Py, PyNativeType, Python};
|
use pyo3::{types::PyDict, Py, Python};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
|
|
Loading…
Reference in New Issue