remove toborrowedobject trait
This commit is contained in:
parent
8ef9e54e13
commit
71f9f18d54
|
@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
- Move `PyTypeObject::type_object` method to `PyTypeInfo` trait, and deprecate `PyTypeObject` trait. [#2284](https://github.com/PyO3/pyo3/pull/2284)
|
||||
- The deprecated `pyproto` feature is now disabled by default. [#2321](https://github.com/PyO3/pyo3/pull/2321)
|
||||
- Deprecate `ToBorrowedObject` trait (it is only used as a wrapper for `ToPyObject`). [#2330](https://github.com/PyO3/pyo3/pull/2330)
|
||||
|
||||
## [0.16.4] - 2022-04-14
|
||||
|
||||
|
|
|
@ -75,10 +75,12 @@ 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 AsPyPointer (we know
|
||||
/// that every AsPyPointer is also ToPyObject) and uses [AsPyPointer::as_ptr()]
|
||||
/// A deprecated conversion trait which relied on the unstable `specialization` feature
|
||||
/// of the Rust language.
|
||||
#[deprecated(
|
||||
since = "0.17.0",
|
||||
note = "this trait is no longer used by PyO3, use ToPyObject or IntoPy<PyObject>"
|
||||
)]
|
||||
pub trait ToBorrowedObject: ToPyObject {
|
||||
/// Converts self into a Python object and calls the specified closure
|
||||
/// on the native FFI pointer underlying the Python object.
|
||||
|
@ -98,6 +100,7 @@ pub trait ToBorrowedObject: ToPyObject {
|
|||
}
|
||||
}
|
||||
|
||||
#[allow(deprecated)]
|
||||
impl<T> ToBorrowedObject for T where T: ToPyObject {}
|
||||
|
||||
/// Defines a conversion from a Rust type to a Python object.
|
||||
|
|
|
@ -7,9 +7,7 @@ use crate::{
|
|||
exceptions::{self, PyBaseException},
|
||||
ffi,
|
||||
};
|
||||
use crate::{
|
||||
AsPyPointer, IntoPy, IntoPyPointer, Py, PyAny, PyObject, Python, ToBorrowedObject, ToPyObject,
|
||||
};
|
||||
use crate::{AsPyPointer, IntoPy, IntoPyPointer, Py, PyAny, PyObject, Python, ToPyObject};
|
||||
use std::borrow::Cow;
|
||||
use std::cell::UnsafeCell;
|
||||
use std::ffi::CString;
|
||||
|
@ -411,11 +409,11 @@ impl PyErr {
|
|||
/// If `exc` is a tuple, all exceptions in the tuple (and recursively in subtuples) are searched for a match.
|
||||
pub fn matches<T>(&self, py: Python<'_>, exc: T) -> bool
|
||||
where
|
||||
T: ToBorrowedObject,
|
||||
T: ToPyObject,
|
||||
{
|
||||
exc.with_borrowed_ptr(py, |exc| unsafe {
|
||||
ffi::PyErr_GivenExceptionMatches(self.type_ptr(py), exc) != 0
|
||||
})
|
||||
unsafe {
|
||||
ffi::PyErr_GivenExceptionMatches(self.type_ptr(py), exc.to_object(py).as_ptr()) != 0
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the current exception is instance of `T`.
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
// Copyright (c) 2017-present PyO3 Project and Contributors
|
||||
use crate::conversion::{PyTryFrom, ToBorrowedObject};
|
||||
use crate::conversion::PyTryFrom;
|
||||
use crate::err::{self, PyDowncastError, PyErr, PyResult};
|
||||
use crate::gil;
|
||||
use crate::pycell::{PyBorrowError, PyBorrowMutError, PyCell};
|
||||
|
@ -563,9 +563,12 @@ impl<T> Py<T> {
|
|||
where
|
||||
N: ToPyObject,
|
||||
{
|
||||
attr_name.with_borrowed_ptr(py, |attr_name| unsafe {
|
||||
PyObject::from_owned_ptr_or_err(py, ffi::PyObject_GetAttr(self.as_ptr(), attr_name))
|
||||
})
|
||||
unsafe {
|
||||
PyObject::from_owned_ptr_or_err(
|
||||
py,
|
||||
ffi::PyObject_GetAttr(self.as_ptr(), attr_name.to_object(py).as_ptr()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets an attribute value.
|
||||
|
@ -595,11 +598,16 @@ impl<T> Py<T> {
|
|||
N: ToPyObject,
|
||||
V: ToPyObject,
|
||||
{
|
||||
attr_name.with_borrowed_ptr(py, move |attr_name| {
|
||||
value.with_borrowed_ptr(py, |value| unsafe {
|
||||
err::error_on_minusone(py, ffi::PyObject_SetAttr(self.as_ptr(), attr_name, value))
|
||||
})
|
||||
})
|
||||
unsafe {
|
||||
err::error_on_minusone(
|
||||
py,
|
||||
ffi::PyObject_SetAttr(
|
||||
self.as_ptr(),
|
||||
attr_name.to_object(py).as_ptr(),
|
||||
value.to_object(py).as_ptr(),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Calls the object.
|
||||
|
@ -656,10 +664,10 @@ impl<T> Py<T> {
|
|||
args: impl IntoPy<Py<PyTuple>>,
|
||||
kwargs: Option<&PyDict>,
|
||||
) -> PyResult<PyObject> {
|
||||
name.with_borrowed_ptr(py, |name| unsafe {
|
||||
unsafe {
|
||||
let args = args.into_py(py).into_ptr();
|
||||
let kwargs = kwargs.into_ptr();
|
||||
let ptr = ffi::PyObject_GetAttr(self.as_ptr(), name);
|
||||
let ptr = ffi::PyObject_GetAttr(self.as_ptr(), name.to_object(py).as_ptr());
|
||||
if ptr.is_null() {
|
||||
return Err(PyErr::fetch(py));
|
||||
}
|
||||
|
@ -668,7 +676,7 @@ impl<T> Py<T> {
|
|||
ffi::Py_XDECREF(args);
|
||||
ffi::Py_XDECREF(kwargs);
|
||||
result
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Calls a method on the object with only positional arguments.
|
||||
|
|
|
@ -294,9 +294,11 @@
|
|||
//! [Features chapter of the guide]: https://pyo3.rs/latest/features.html#features-reference "Features Reference - PyO3 user guide"
|
||||
//! [`Ungil`]: crate::marker::Ungil
|
||||
pub use crate::class::*;
|
||||
#[allow(deprecated)]
|
||||
pub use crate::conversion::ToBorrowedObject;
|
||||
pub use crate::conversion::{
|
||||
AsPyPointer, FromPyObject, FromPyPointer, IntoPy, IntoPyPointer, PyTryFrom, PyTryInto,
|
||||
ToBorrowedObject, ToPyObject,
|
||||
ToPyObject,
|
||||
};
|
||||
pub use crate::err::{PyDowncastError, PyErr, PyErrArguments, PyResult};
|
||||
#[cfg(not(PyPy))]
|
||||
|
|
155
src/types/any.rs
155
src/types/any.rs
|
@ -1,7 +1,5 @@
|
|||
use crate::class::basic::CompareOp;
|
||||
use crate::conversion::{
|
||||
AsPyPointer, FromPyObject, IntoPy, IntoPyPointer, PyTryFrom, ToBorrowedObject, ToPyObject,
|
||||
};
|
||||
use crate::conversion::{AsPyPointer, FromPyObject, IntoPy, IntoPyPointer, PyTryFrom, ToPyObject};
|
||||
use crate::err::{PyDowncastError, PyErr, PyResult};
|
||||
use crate::exceptions::PyTypeError;
|
||||
use crate::type_object::PyTypeInfo;
|
||||
|
@ -103,9 +101,9 @@ impl PyAny {
|
|||
where
|
||||
N: ToPyObject,
|
||||
{
|
||||
attr_name.with_borrowed_ptr(self.py(), |attr_name| unsafe {
|
||||
Ok(ffi::PyObject_HasAttr(self.as_ptr(), attr_name) != 0)
|
||||
})
|
||||
unsafe {
|
||||
Ok(ffi::PyObject_HasAttr(self.as_ptr(), attr_name.to_object(self.py()).as_ptr()) != 0)
|
||||
}
|
||||
}
|
||||
|
||||
/// Retrieves an attribute value.
|
||||
|
@ -134,10 +132,12 @@ impl PyAny {
|
|||
where
|
||||
N: ToPyObject,
|
||||
{
|
||||
attr_name.with_borrowed_ptr(self.py(), |attr_name| unsafe {
|
||||
self.py()
|
||||
.from_owned_ptr_or_err(ffi::PyObject_GetAttr(self.as_ptr(), attr_name))
|
||||
})
|
||||
unsafe {
|
||||
self.py().from_owned_ptr_or_err(ffi::PyObject_GetAttr(
|
||||
self.as_ptr(),
|
||||
attr_name.to_object(self.py()).as_ptr(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets an attribute value.
|
||||
|
@ -164,17 +164,20 @@ impl PyAny {
|
|||
/// ```
|
||||
pub fn setattr<N, V>(&self, attr_name: N, value: V) -> PyResult<()>
|
||||
where
|
||||
N: ToBorrowedObject,
|
||||
V: ToBorrowedObject,
|
||||
N: ToPyObject,
|
||||
V: ToPyObject,
|
||||
{
|
||||
attr_name.with_borrowed_ptr(self.py(), move |attr_name| {
|
||||
value.with_borrowed_ptr(self.py(), |value| unsafe {
|
||||
err::error_on_minusone(
|
||||
self.py(),
|
||||
ffi::PyObject_SetAttr(self.as_ptr(), attr_name, value),
|
||||
)
|
||||
})
|
||||
})
|
||||
let py = self.py();
|
||||
unsafe {
|
||||
err::error_on_minusone(
|
||||
py,
|
||||
ffi::PyObject_SetAttr(
|
||||
self.as_ptr(),
|
||||
attr_name.to_object(py).as_ptr(),
|
||||
value.to_object(py).as_ptr(),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Deletes an attribute.
|
||||
|
@ -184,9 +187,13 @@ impl PyAny {
|
|||
where
|
||||
N: ToPyObject,
|
||||
{
|
||||
attr_name.with_borrowed_ptr(self.py(), |attr_name| unsafe {
|
||||
err::error_on_minusone(self.py(), ffi::PyObject_DelAttr(self.as_ptr(), attr_name))
|
||||
})
|
||||
let py = self.py();
|
||||
unsafe {
|
||||
err::error_on_minusone(
|
||||
self.py(),
|
||||
ffi::PyObject_DelAttr(self.as_ptr(), attr_name.to_object(py).as_ptr()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns an [`Ordering`] between `self` and `other`.
|
||||
|
@ -239,26 +246,29 @@ impl PyAny {
|
|||
where
|
||||
O: ToPyObject,
|
||||
{
|
||||
self._compare(other.to_object(self.py()))
|
||||
}
|
||||
|
||||
fn _compare(&self, other: PyObject) -> PyResult<Ordering> {
|
||||
let py = self.py();
|
||||
let other = other.as_ptr();
|
||||
// Almost the same as ffi::PyObject_RichCompareBool, but this one doesn't try self == other.
|
||||
// See https://github.com/PyO3/pyo3/issues/985 for more.
|
||||
let do_compare = |other, op| unsafe {
|
||||
PyObject::from_owned_ptr_or_err(py, ffi::PyObject_RichCompare(self.as_ptr(), other, op))
|
||||
.and_then(|obj| obj.is_true(py))
|
||||
};
|
||||
other.with_borrowed_ptr(py, |other| {
|
||||
if do_compare(other, ffi::Py_EQ)? {
|
||||
Ok(Ordering::Equal)
|
||||
} else if do_compare(other, ffi::Py_LT)? {
|
||||
Ok(Ordering::Less)
|
||||
} else if do_compare(other, ffi::Py_GT)? {
|
||||
Ok(Ordering::Greater)
|
||||
} else {
|
||||
Err(PyTypeError::new_err(
|
||||
"PyAny::compare(): All comparisons returned false",
|
||||
))
|
||||
}
|
||||
})
|
||||
if do_compare(other, ffi::Py_EQ)? {
|
||||
Ok(Ordering::Equal)
|
||||
} else if do_compare(other, ffi::Py_LT)? {
|
||||
Ok(Ordering::Less)
|
||||
} else if do_compare(other, ffi::Py_GT)? {
|
||||
Ok(Ordering::Greater)
|
||||
} else {
|
||||
Err(PyTypeError::new_err(
|
||||
"PyAny::compare(): All comparisons returned false",
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// Tests whether two Python objects obey a given [`CompareOp`].
|
||||
|
@ -296,13 +306,11 @@ impl PyAny {
|
|||
O: ToPyObject,
|
||||
{
|
||||
unsafe {
|
||||
other.with_borrowed_ptr(self.py(), |other| {
|
||||
self.py().from_owned_ptr_or_err(ffi::PyObject_RichCompare(
|
||||
self.as_ptr(),
|
||||
other,
|
||||
compare_op as c_int,
|
||||
))
|
||||
})
|
||||
self.py().from_owned_ptr_or_err(ffi::PyObject_RichCompare(
|
||||
self.as_ptr(),
|
||||
other.to_object(self.py()).as_ptr(),
|
||||
compare_op as c_int,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -519,9 +527,9 @@ impl PyAny {
|
|||
args: impl IntoPy<Py<PyTuple>>,
|
||||
kwargs: Option<&PyDict>,
|
||||
) -> PyResult<&PyAny> {
|
||||
name.with_borrowed_ptr(self.py(), |name| unsafe {
|
||||
unsafe {
|
||||
let py = self.py();
|
||||
let ptr = ffi::PyObject_GetAttr(self.as_ptr(), name);
|
||||
let ptr = ffi::PyObject_GetAttr(self.as_ptr(), name.to_object(py).as_ptr());
|
||||
if ptr.is_null() {
|
||||
return Err(PyErr::fetch(py));
|
||||
}
|
||||
|
@ -533,7 +541,7 @@ impl PyAny {
|
|||
ffi::Py_XDECREF(args);
|
||||
ffi::Py_XDECREF(kwargs);
|
||||
result
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Calls a method on the object without arguments.
|
||||
|
@ -639,12 +647,14 @@ impl PyAny {
|
|||
/// This is equivalent to the Python expression `self[key]`.
|
||||
pub fn get_item<K>(&self, key: K) -> PyResult<&PyAny>
|
||||
where
|
||||
K: ToBorrowedObject,
|
||||
K: ToPyObject,
|
||||
{
|
||||
key.with_borrowed_ptr(self.py(), |key| unsafe {
|
||||
self.py()
|
||||
.from_owned_ptr_or_err(ffi::PyObject_GetItem(self.as_ptr(), key))
|
||||
})
|
||||
unsafe {
|
||||
self.py().from_owned_ptr_or_err(ffi::PyObject_GetItem(
|
||||
self.as_ptr(),
|
||||
key.to_object(self.py()).as_ptr(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets a collection item value.
|
||||
|
@ -652,14 +662,20 @@ impl PyAny {
|
|||
/// This is equivalent to the Python expression `self[key] = value`.
|
||||
pub fn set_item<K, V>(&self, key: K, value: V) -> PyResult<()>
|
||||
where
|
||||
K: ToBorrowedObject,
|
||||
V: ToBorrowedObject,
|
||||
K: ToPyObject,
|
||||
V: ToPyObject,
|
||||
{
|
||||
key.with_borrowed_ptr(self.py(), move |key| {
|
||||
value.with_borrowed_ptr(self.py(), |value| unsafe {
|
||||
err::error_on_minusone(self.py(), ffi::PyObject_SetItem(self.as_ptr(), key, value))
|
||||
})
|
||||
})
|
||||
let py = self.py();
|
||||
unsafe {
|
||||
err::error_on_minusone(
|
||||
py,
|
||||
ffi::PyObject_SetItem(
|
||||
self.as_ptr(),
|
||||
key.to_object(py).as_ptr(),
|
||||
value.to_object(py).as_ptr(),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Deletes an item from the collection.
|
||||
|
@ -667,11 +683,14 @@ impl PyAny {
|
|||
/// This is equivalent to the Python expression `del self[key]`.
|
||||
pub fn del_item<K>(&self, key: K) -> PyResult<()>
|
||||
where
|
||||
K: ToBorrowedObject,
|
||||
K: ToPyObject,
|
||||
{
|
||||
key.with_borrowed_ptr(self.py(), |key| unsafe {
|
||||
err::error_on_minusone(self.py(), ffi::PyObject_DelItem(self.as_ptr(), key))
|
||||
})
|
||||
unsafe {
|
||||
err::error_on_minusone(
|
||||
self.py(),
|
||||
ffi::PyObject_DelItem(self.as_ptr(), key.to_object(self.py()).as_ptr()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Takes an object and returns an iterator for it.
|
||||
|
@ -789,15 +808,15 @@ impl PyAny {
|
|||
/// Determines if self contains `value`.
|
||||
///
|
||||
/// This is equivalent to the Python expression `value in self`.
|
||||
#[inline]
|
||||
pub fn contains<V>(&self, value: V) -> PyResult<bool>
|
||||
where
|
||||
V: ToBorrowedObject,
|
||||
V: ToPyObject,
|
||||
{
|
||||
let r = value.with_borrowed_ptr(self.py(), |ptr| unsafe {
|
||||
ffi::PySequence_Contains(self.as_ptr(), ptr)
|
||||
});
|
||||
match r {
|
||||
self._contains(value.to_object(self.py()))
|
||||
}
|
||||
|
||||
fn _contains(&self, value: PyObject) -> PyResult<bool> {
|
||||
match unsafe { ffi::PySequence_Contains(self.as_ptr(), value.as_ptr()) } {
|
||||
0 => Ok(false),
|
||||
1 => Ok(true),
|
||||
_ => Err(PyErr::fetch(self.py())),
|
||||
|
|
|
@ -4,10 +4,7 @@ use crate::err::{self, PyErr, PyResult};
|
|||
use crate::types::{PyAny, PyList};
|
||||
#[cfg(not(PyPy))]
|
||||
use crate::IntoPyPointer;
|
||||
use crate::{
|
||||
ffi, AsPyPointer, FromPyObject, IntoPy, PyObject, PyTryFrom, Python, ToBorrowedObject,
|
||||
ToPyObject,
|
||||
};
|
||||
use crate::{ffi, AsPyPointer, FromPyObject, IntoPy, PyObject, PyTryFrom, Python, ToPyObject};
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::ptr::NonNull;
|
||||
use std::{cmp, collections, hash};
|
||||
|
@ -82,15 +79,15 @@ impl PyDict {
|
|||
/// This is equivalent to the Python expression `key in self`.
|
||||
pub fn contains<K>(&self, key: K) -> PyResult<bool>
|
||||
where
|
||||
K: ToBorrowedObject,
|
||||
K: ToPyObject,
|
||||
{
|
||||
key.with_borrowed_ptr(self.py(), |key| unsafe {
|
||||
match ffi::PyDict_Contains(self.as_ptr(), key) {
|
||||
unsafe {
|
||||
match ffi::PyDict_Contains(self.as_ptr(), key.to_object(self.py()).as_ptr()) {
|
||||
1 => Ok(true),
|
||||
0 => Ok(false),
|
||||
_ => Err(PyErr::fetch(self.py())),
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets an item from the dictionary.
|
||||
|
@ -100,15 +97,15 @@ impl PyDict {
|
|||
/// To get a `KeyError` for non-existing keys, use `PyAny::get_item`.
|
||||
pub fn get_item<K>(&self, key: K) -> Option<&PyAny>
|
||||
where
|
||||
K: ToBorrowedObject,
|
||||
K: ToPyObject,
|
||||
{
|
||||
key.with_borrowed_ptr(self.py(), |key| unsafe {
|
||||
let ptr = ffi::PyDict_GetItem(self.as_ptr(), key);
|
||||
unsafe {
|
||||
let ptr = ffi::PyDict_GetItem(self.as_ptr(), key.to_object(self.py()).as_ptr());
|
||||
NonNull::new(ptr).map(|p| {
|
||||
// PyDict_GetItem return s borrowed ptr, must make it owned for safety (see #890).
|
||||
self.py().from_owned_ptr(ffi::_Py_NewRef(p.as_ptr()))
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets an item value.
|
||||
|
@ -119,11 +116,17 @@ impl PyDict {
|
|||
K: ToPyObject,
|
||||
V: ToPyObject,
|
||||
{
|
||||
key.with_borrowed_ptr(self.py(), move |key| {
|
||||
value.with_borrowed_ptr(self.py(), |value| unsafe {
|
||||
err::error_on_minusone(self.py(), ffi::PyDict_SetItem(self.as_ptr(), key, value))
|
||||
})
|
||||
})
|
||||
let py = self.py();
|
||||
unsafe {
|
||||
err::error_on_minusone(
|
||||
py,
|
||||
ffi::PyDict_SetItem(
|
||||
self.as_ptr(),
|
||||
key.to_object(py).as_ptr(),
|
||||
value.to_object(py).as_ptr(),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Deletes an item.
|
||||
|
@ -131,11 +134,15 @@ impl PyDict {
|
|||
/// This is equivalent to the Python statement `del self[key]`.
|
||||
pub fn del_item<K>(&self, key: K) -> PyResult<()>
|
||||
where
|
||||
K: ToBorrowedObject,
|
||||
K: ToPyObject,
|
||||
{
|
||||
key.with_borrowed_ptr(self.py(), |key| unsafe {
|
||||
err::error_on_minusone(self.py(), ffi::PyDict_DelItem(self.as_ptr(), key))
|
||||
})
|
||||
let py = self.py();
|
||||
unsafe {
|
||||
err::error_on_minusone(
|
||||
py,
|
||||
ffi::PyDict_DelItem(self.as_ptr(), key.to_object(py).as_ptr()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a list of dict keys.
|
||||
|
|
|
@ -9,8 +9,7 @@ use crate::ffi::{self, Py_ssize_t};
|
|||
use crate::internal_tricks::get_ssize_index;
|
||||
use crate::types::PySequence;
|
||||
use crate::{
|
||||
AsPyPointer, IntoPy, IntoPyPointer, Py, PyAny, PyObject, PyTryFrom, Python, ToBorrowedObject,
|
||||
ToPyObject,
|
||||
AsPyPointer, IntoPy, IntoPyPointer, Py, PyAny, PyObject, PyTryFrom, Python, ToPyObject,
|
||||
};
|
||||
|
||||
/// Represents a Python `list`.
|
||||
|
@ -222,11 +221,15 @@ impl PyList {
|
|||
/// Appends an item to the list.
|
||||
pub fn append<I>(&self, item: I) -> PyResult<()>
|
||||
where
|
||||
I: ToBorrowedObject,
|
||||
I: ToPyObject,
|
||||
{
|
||||
item.with_borrowed_ptr(self.py(), |item| unsafe {
|
||||
err::error_on_minusone(self.py(), ffi::PyList_Append(self.as_ptr(), item))
|
||||
})
|
||||
let py = self.py();
|
||||
unsafe {
|
||||
err::error_on_minusone(
|
||||
py,
|
||||
ffi::PyList_Append(self.as_ptr(), item.to_object(py).as_ptr()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Inserts an item at the specified index.
|
||||
|
@ -234,14 +237,19 @@ impl PyList {
|
|||
/// If `index >= self.len()`, inserts at the end.
|
||||
pub fn insert<I>(&self, index: usize, item: I) -> PyResult<()>
|
||||
where
|
||||
I: ToBorrowedObject,
|
||||
I: ToPyObject,
|
||||
{
|
||||
item.with_borrowed_ptr(self.py(), |item| unsafe {
|
||||
let py = self.py();
|
||||
unsafe {
|
||||
err::error_on_minusone(
|
||||
self.py(),
|
||||
ffi::PyList_Insert(self.as_ptr(), get_ssize_index(index), item),
|
||||
py,
|
||||
ffi::PyList_Insert(
|
||||
self.as_ptr(),
|
||||
get_ssize_index(index),
|
||||
item.to_object(py).as_ptr(),
|
||||
),
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Determines if self contains `value`.
|
||||
|
@ -250,7 +258,7 @@ impl PyList {
|
|||
#[inline]
|
||||
pub fn contains<V>(&self, value: V) -> PyResult<bool>
|
||||
where
|
||||
V: ToBorrowedObject,
|
||||
V: ToPyObject,
|
||||
{
|
||||
self.as_sequence().contains(value)
|
||||
}
|
||||
|
@ -261,7 +269,7 @@ impl PyList {
|
|||
#[inline]
|
||||
pub fn index<V>(&self, value: V) -> PyResult<usize>
|
||||
where
|
||||
V: ToBorrowedObject,
|
||||
V: ToPyObject,
|
||||
{
|
||||
self.as_sequence().index(value)
|
||||
}
|
||||
|
|
|
@ -2,10 +2,7 @@
|
|||
|
||||
use crate::err::{PyDowncastError, PyErr, PyResult};
|
||||
use crate::types::{PyAny, PySequence};
|
||||
use crate::{
|
||||
ffi, AsPyPointer, IntoPyPointer, Py, PyNativeType, PyTryFrom, Python, ToBorrowedObject,
|
||||
ToPyObject,
|
||||
};
|
||||
use crate::{ffi, AsPyPointer, IntoPyPointer, Py, PyNativeType, PyTryFrom, Python, ToPyObject};
|
||||
|
||||
/// Represents a reference to a Python object supporting the mapping protocol.
|
||||
#[repr(transparent)]
|
||||
|
@ -38,7 +35,7 @@ impl PyMapping {
|
|||
/// This is equivalent to the Python expression `key in self`.
|
||||
pub fn contains<K>(&self, key: K) -> PyResult<bool>
|
||||
where
|
||||
K: ToBorrowedObject,
|
||||
K: ToPyObject,
|
||||
{
|
||||
PyAny::contains(self, key)
|
||||
}
|
||||
|
@ -51,7 +48,7 @@ impl PyMapping {
|
|||
#[inline]
|
||||
pub fn get_item<K>(&self, key: K) -> PyResult<&PyAny>
|
||||
where
|
||||
K: ToBorrowedObject,
|
||||
K: ToPyObject,
|
||||
{
|
||||
PyAny::get_item(self, key)
|
||||
}
|
||||
|
@ -74,7 +71,7 @@ impl PyMapping {
|
|||
#[inline]
|
||||
pub fn del_item<K>(&self, key: K) -> PyResult<()>
|
||||
where
|
||||
K: ToBorrowedObject,
|
||||
K: ToPyObject,
|
||||
{
|
||||
PyAny::del_item(self, key)
|
||||
}
|
||||
|
|
|
@ -3,9 +3,9 @@
|
|||
use crate::err::{self, PyDowncastError, PyErr, PyResult};
|
||||
use crate::internal_tricks::get_ssize_index;
|
||||
use crate::types::{PyAny, PyList, PyTuple};
|
||||
use crate::{ffi, PyNativeType};
|
||||
use crate::{ffi, PyNativeType, ToPyObject};
|
||||
use crate::{AsPyPointer, IntoPyPointer, Py, Python};
|
||||
use crate::{FromPyObject, PyTryFrom, ToBorrowedObject};
|
||||
use crate::{FromPyObject, PyTryFrom};
|
||||
|
||||
/// Represents a reference to a Python object supporting the sequence protocol.
|
||||
#[repr(transparent)]
|
||||
|
@ -123,15 +123,18 @@ impl PySequence {
|
|||
#[inline]
|
||||
pub fn set_item<I>(&self, i: usize, item: I) -> PyResult<()>
|
||||
where
|
||||
I: ToBorrowedObject,
|
||||
I: ToPyObject,
|
||||
{
|
||||
let py = self.py();
|
||||
unsafe {
|
||||
item.with_borrowed_ptr(self.py(), |item| {
|
||||
err::error_on_minusone(
|
||||
self.py(),
|
||||
ffi::PySequence_SetItem(self.as_ptr(), get_ssize_index(i), item),
|
||||
)
|
||||
})
|
||||
err::error_on_minusone(
|
||||
py,
|
||||
ffi::PySequence_SetItem(
|
||||
self.as_ptr(),
|
||||
get_ssize_index(i),
|
||||
item.to_object(py).as_ptr(),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -185,11 +188,10 @@ impl PySequence {
|
|||
#[cfg(not(PyPy))]
|
||||
pub fn count<V>(&self, value: V) -> PyResult<usize>
|
||||
where
|
||||
V: ToBorrowedObject,
|
||||
V: ToPyObject,
|
||||
{
|
||||
let r = value.with_borrowed_ptr(self.py(), |ptr| unsafe {
|
||||
ffi::PySequence_Count(self.as_ptr(), ptr)
|
||||
});
|
||||
let r =
|
||||
unsafe { ffi::PySequence_Count(self.as_ptr(), value.to_object(self.py()).as_ptr()) };
|
||||
if r == -1 {
|
||||
Err(PyErr::fetch(self.py()))
|
||||
} else {
|
||||
|
@ -203,11 +205,10 @@ impl PySequence {
|
|||
#[inline]
|
||||
pub fn contains<V>(&self, value: V) -> PyResult<bool>
|
||||
where
|
||||
V: ToBorrowedObject,
|
||||
V: ToPyObject,
|
||||
{
|
||||
let r = value.with_borrowed_ptr(self.py(), |ptr| unsafe {
|
||||
ffi::PySequence_Contains(self.as_ptr(), ptr)
|
||||
});
|
||||
let r =
|
||||
unsafe { ffi::PySequence_Contains(self.as_ptr(), value.to_object(self.py()).as_ptr()) };
|
||||
match r {
|
||||
0 => Ok(false),
|
||||
1 => Ok(true),
|
||||
|
@ -221,11 +222,10 @@ impl PySequence {
|
|||
#[inline]
|
||||
pub fn index<V>(&self, value: V) -> PyResult<usize>
|
||||
where
|
||||
V: ToBorrowedObject,
|
||||
V: ToPyObject,
|
||||
{
|
||||
let r = value.with_borrowed_ptr(self.py(), |ptr| unsafe {
|
||||
ffi::PySequence_Index(self.as_ptr(), ptr)
|
||||
});
|
||||
let r =
|
||||
unsafe { ffi::PySequence_Index(self.as_ptr(), value.to_object(self.py()).as_ptr()) };
|
||||
if r == -1 {
|
||||
Err(PyErr::fetch(self.py()))
|
||||
} else {
|
||||
|
|
|
@ -4,9 +4,7 @@
|
|||
use crate::err::{self, PyErr, PyResult};
|
||||
#[cfg(Py_LIMITED_API)]
|
||||
use crate::types::PyIterator;
|
||||
use crate::{
|
||||
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyObject, Python, ToBorrowedObject, ToPyObject,
|
||||
};
|
||||
use crate::{ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyObject, Python, ToPyObject};
|
||||
use std::cmp;
|
||||
use std::collections::{BTreeSet, HashSet};
|
||||
use std::{collections, hash, ptr};
|
||||
|
@ -69,13 +67,13 @@ impl PySet {
|
|||
where
|
||||
K: ToPyObject,
|
||||
{
|
||||
key.with_borrowed_ptr(self.py(), |key| unsafe {
|
||||
match ffi::PySet_Contains(self.as_ptr(), key) {
|
||||
unsafe {
|
||||
match ffi::PySet_Contains(self.as_ptr(), key.to_object(self.py()).as_ptr()) {
|
||||
1 => Ok(true),
|
||||
0 => Ok(false),
|
||||
_ => Err(PyErr::fetch(self.py())),
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes the element from the set if it is present.
|
||||
|
@ -83,9 +81,9 @@ impl PySet {
|
|||
where
|
||||
K: ToPyObject,
|
||||
{
|
||||
key.with_borrowed_ptr(self.py(), |key| unsafe {
|
||||
ffi::PySet_Discard(self.as_ptr(), key);
|
||||
})
|
||||
unsafe {
|
||||
ffi::PySet_Discard(self.as_ptr(), key.to_object(self.py()).as_ptr());
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds an element to the set.
|
||||
|
@ -93,9 +91,12 @@ impl PySet {
|
|||
where
|
||||
K: ToPyObject,
|
||||
{
|
||||
key.with_borrowed_ptr(self.py(), move |key| unsafe {
|
||||
err::error_on_minusone(self.py(), ffi::PySet_Add(self.as_ptr(), key))
|
||||
})
|
||||
unsafe {
|
||||
err::error_on_minusone(
|
||||
self.py(),
|
||||
ffi::PySet_Add(self.as_ptr(), key.to_object(self.py()).as_ptr()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes and returns an arbitrary element from the set.
|
||||
|
@ -303,15 +304,15 @@ impl PyFrozenSet {
|
|||
/// This is equivalent to the Python expression `key in self`.
|
||||
pub fn contains<K>(&self, key: K) -> PyResult<bool>
|
||||
where
|
||||
K: ToBorrowedObject,
|
||||
K: ToPyObject,
|
||||
{
|
||||
key.with_borrowed_ptr(self.py(), |key| unsafe {
|
||||
match ffi::PySet_Contains(self.as_ptr(), key) {
|
||||
unsafe {
|
||||
match ffi::PySet_Contains(self.as_ptr(), key.to_object(self.py()).as_ptr()) {
|
||||
1 => Ok(true),
|
||||
0 => Ok(false),
|
||||
_ => Err(PyErr::fetch(self.py())),
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns an iterator of values in this frozen set.
|
||||
|
|
|
@ -7,7 +7,7 @@ use crate::internal_tricks::get_ssize_index;
|
|||
use crate::types::PySequence;
|
||||
use crate::{
|
||||
exceptions, AsPyPointer, FromPyObject, IntoPy, IntoPyPointer, Py, PyAny, PyErr, PyObject,
|
||||
PyResult, PyTryFrom, Python, ToBorrowedObject, ToPyObject,
|
||||
PyResult, PyTryFrom, Python, ToPyObject,
|
||||
};
|
||||
|
||||
#[inline]
|
||||
|
@ -215,7 +215,7 @@ impl PyTuple {
|
|||
#[inline]
|
||||
pub fn contains<V>(&self, value: V) -> PyResult<bool>
|
||||
where
|
||||
V: ToBorrowedObject,
|
||||
V: ToPyObject,
|
||||
{
|
||||
self.as_sequence().contains(value)
|
||||
}
|
||||
|
@ -226,7 +226,7 @@ impl PyTuple {
|
|||
#[inline]
|
||||
pub fn index<V>(&self, value: V) -> PyResult<usize>
|
||||
where
|
||||
V: ToBorrowedObject,
|
||||
V: ToPyObject,
|
||||
{
|
||||
self.as_sequence().index(value)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue