Add more lints
This commit is contained in:
parent
b8dd626194
commit
6f1cf1b662
|
@ -15,4 +15,7 @@ rustflags = [
|
|||
"-Dclippy::todo",
|
||||
"-Dclippy::unnecessary_wraps",
|
||||
"-Dclippy::useless_transmute",
|
||||
"-Delided_lifetimes_in_paths",
|
||||
"-Dunused_lifetimes",
|
||||
"-Drust_2021_prelude_collisions"
|
||||
]
|
|
@ -86,7 +86,7 @@ fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
|
|||
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
|
||||
/// import the module.
|
||||
#[pymodule]
|
||||
fn string_sum(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn string_sum(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ macro_rules! test_module {
|
|||
};
|
||||
}
|
||||
|
||||
fn bench_call_0(b: &mut Bencher) {
|
||||
fn bench_call_0(b: &mut Bencher<'_>) {
|
||||
Python::with_gil(|py| {
|
||||
let module = test_module!(py, "def foo(): pass");
|
||||
|
||||
|
@ -22,7 +22,7 @@ fn bench_call_0(b: &mut Bencher) {
|
|||
})
|
||||
}
|
||||
|
||||
fn bench_call_method_0(b: &mut Bencher) {
|
||||
fn bench_call_method_0(b: &mut Bencher<'_>) {
|
||||
Python::with_gil(|py| {
|
||||
let module = test_module!(
|
||||
py,
|
||||
|
|
|
@ -4,7 +4,7 @@ use pyo3::prelude::*;
|
|||
use pyo3::types::IntoPyDict;
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
|
||||
fn iter_dict(b: &mut Bencher) {
|
||||
fn iter_dict(b: &mut Bencher<'_>) {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
const LEN: usize = 100_000;
|
||||
|
@ -18,14 +18,14 @@ fn iter_dict(b: &mut Bencher) {
|
|||
});
|
||||
}
|
||||
|
||||
fn dict_new(b: &mut Bencher) {
|
||||
fn dict_new(b: &mut Bencher<'_>) {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
const LEN: usize = 50_000;
|
||||
b.iter(|| (0..LEN as u64).map(|i| (i, i * 2)).into_py_dict(py));
|
||||
}
|
||||
|
||||
fn dict_get_item(b: &mut Bencher) {
|
||||
fn dict_get_item(b: &mut Bencher<'_>) {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
const LEN: usize = 50_000;
|
||||
|
@ -38,7 +38,7 @@ fn dict_get_item(b: &mut Bencher) {
|
|||
});
|
||||
}
|
||||
|
||||
fn extract_hashmap(b: &mut Bencher) {
|
||||
fn extract_hashmap(b: &mut Bencher<'_>) {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
const LEN: usize = 100_000;
|
||||
|
@ -46,7 +46,7 @@ fn extract_hashmap(b: &mut Bencher) {
|
|||
b.iter(|| HashMap::<u64, u64>::extract(dict));
|
||||
}
|
||||
|
||||
fn extract_btreemap(b: &mut Bencher) {
|
||||
fn extract_btreemap(b: &mut Bencher<'_>) {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
const LEN: usize = 100_000;
|
||||
|
@ -55,7 +55,7 @@ fn extract_btreemap(b: &mut Bencher) {
|
|||
}
|
||||
|
||||
#[cfg(feature = "hashbrown")]
|
||||
fn extract_hashbrown_map(b: &mut Bencher) {
|
||||
fn extract_hashbrown_map(b: &mut Bencher<'_>) {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
const LEN: usize = 100_000;
|
||||
|
|
|
@ -2,7 +2,7 @@ use criterion::{criterion_group, criterion_main, Bencher, Criterion};
|
|||
|
||||
use pyo3::{exceptions::PyValueError, prelude::*};
|
||||
|
||||
fn err_new_restore_and_fetch(b: &mut Bencher) {
|
||||
fn err_new_restore_and_fetch(b: &mut Bencher<'_>) {
|
||||
Python::with_gil(|py| {
|
||||
b.iter(|| {
|
||||
PyValueError::new_err("some exception message").restore(py);
|
||||
|
@ -11,7 +11,7 @@ fn err_new_restore_and_fetch(b: &mut Bencher) {
|
|||
})
|
||||
}
|
||||
|
||||
fn err_new_without_gil(b: &mut Bencher) {
|
||||
fn err_new_without_gil(b: &mut Bencher<'_>) {
|
||||
b.iter(|| PyValueError::new_err("some exception message"))
|
||||
}
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ enum ManyTypes {
|
|||
String(String),
|
||||
}
|
||||
|
||||
fn enum_from_pyobject(b: &mut Bencher) {
|
||||
fn enum_from_pyobject(b: &mut Bencher<'_>) {
|
||||
Python::with_gil(|py| {
|
||||
let obj = PyString::new(py, "hello world");
|
||||
b.iter(|| {
|
||||
|
|
|
@ -2,7 +2,7 @@ use criterion::{criterion_group, criterion_main, BatchSize, Bencher, Criterion};
|
|||
|
||||
use pyo3::{prelude::*, GILPool};
|
||||
|
||||
fn bench_clean_gilpool_new(b: &mut Bencher) {
|
||||
fn bench_clean_gilpool_new(b: &mut Bencher<'_>) {
|
||||
Python::with_gil(|_py| {
|
||||
b.iter(|| {
|
||||
let _ = unsafe { GILPool::new() };
|
||||
|
@ -10,14 +10,14 @@ fn bench_clean_gilpool_new(b: &mut Bencher) {
|
|||
});
|
||||
}
|
||||
|
||||
fn bench_clean_acquire_gil(b: &mut Bencher) {
|
||||
fn bench_clean_acquire_gil(b: &mut Bencher<'_>) {
|
||||
// Acquiring first GIL will also create a "clean" GILPool, so this measures the Python overhead.
|
||||
b.iter(|| {
|
||||
let _ = Python::acquire_gil();
|
||||
});
|
||||
}
|
||||
|
||||
fn bench_dirty_acquire_gil(b: &mut Bencher) {
|
||||
fn bench_dirty_acquire_gil(b: &mut Bencher<'_>) {
|
||||
let obj = Python::with_gil(|py| py.None());
|
||||
b.iter_batched(
|
||||
|| {
|
||||
|
|
|
@ -3,7 +3,7 @@ use criterion::{criterion_group, criterion_main, Bencher, Criterion};
|
|||
use pyo3::prelude::*;
|
||||
use pyo3::types::PyList;
|
||||
|
||||
fn iter_list(b: &mut Bencher) {
|
||||
fn iter_list(b: &mut Bencher<'_>) {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
const LEN: usize = 100_000;
|
||||
|
@ -17,14 +17,14 @@ fn iter_list(b: &mut Bencher) {
|
|||
});
|
||||
}
|
||||
|
||||
fn list_new(b: &mut Bencher) {
|
||||
fn list_new(b: &mut Bencher<'_>) {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
const LEN: usize = 50_000;
|
||||
b.iter(|| PyList::new(py, 0..LEN));
|
||||
}
|
||||
|
||||
fn list_get_item(b: &mut Bencher) {
|
||||
fn list_get_item(b: &mut Bencher<'_>) {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
const LEN: usize = 50_000;
|
||||
|
@ -38,7 +38,7 @@ fn list_get_item(b: &mut Bencher) {
|
|||
}
|
||||
|
||||
#[cfg(not(Py_LIMITED_API))]
|
||||
fn list_get_item_unchecked(b: &mut Bencher) {
|
||||
fn list_get_item_unchecked(b: &mut Bencher<'_>) {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
const LEN: usize = 50_000;
|
||||
|
|
|
@ -26,7 +26,7 @@ impl MyClass {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn first_time_init(b: &mut criterion::Bencher) {
|
||||
pub fn first_time_init(b: &mut criterion::Bencher<'_>) {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
b.iter(|| {
|
||||
|
|
|
@ -2,7 +2,7 @@ use criterion::{criterion_group, criterion_main, Bencher, Criterion};
|
|||
|
||||
use pyo3::prelude::*;
|
||||
|
||||
fn drop_many_objects(b: &mut Bencher) {
|
||||
fn drop_many_objects(b: &mut Bencher<'_>) {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
b.iter(|| {
|
||||
|
|
|
@ -4,7 +4,7 @@ use pyo3::prelude::*;
|
|||
use pyo3::types::PySet;
|
||||
use std::collections::{BTreeSet, HashSet};
|
||||
|
||||
fn iter_set(b: &mut Bencher) {
|
||||
fn iter_set(b: &mut Bencher<'_>) {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
const LEN: usize = 100_000;
|
||||
|
@ -18,7 +18,7 @@ fn iter_set(b: &mut Bencher) {
|
|||
});
|
||||
}
|
||||
|
||||
fn extract_hashset(b: &mut Bencher) {
|
||||
fn extract_hashset(b: &mut Bencher<'_>) {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
const LEN: usize = 100_000;
|
||||
|
@ -26,7 +26,7 @@ fn extract_hashset(b: &mut Bencher) {
|
|||
b.iter(|| HashSet::<u64>::extract(set));
|
||||
}
|
||||
|
||||
fn extract_btreeset(b: &mut Bencher) {
|
||||
fn extract_btreeset(b: &mut Bencher<'_>) {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
const LEN: usize = 100_000;
|
||||
|
@ -35,7 +35,7 @@ fn extract_btreeset(b: &mut Bencher) {
|
|||
}
|
||||
|
||||
#[cfg(feature = "hashbrown")]
|
||||
fn extract_hashbrown_set(b: &mut Bencher) {
|
||||
fn extract_hashbrown_set(b: &mut Bencher<'_>) {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
const LEN: usize = 100_000;
|
||||
|
|
|
@ -3,7 +3,7 @@ use criterion::{criterion_group, criterion_main, Bencher, Criterion};
|
|||
use pyo3::prelude::*;
|
||||
use pyo3::types::PyTuple;
|
||||
|
||||
fn iter_tuple(b: &mut Bencher) {
|
||||
fn iter_tuple(b: &mut Bencher<'_>) {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
const LEN: usize = 100_000;
|
||||
|
@ -17,14 +17,14 @@ fn iter_tuple(b: &mut Bencher) {
|
|||
});
|
||||
}
|
||||
|
||||
fn tuple_new(b: &mut Bencher) {
|
||||
fn tuple_new(b: &mut Bencher<'_>) {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
const LEN: usize = 50_000;
|
||||
b.iter(|| PyTuple::new(py, 0..LEN));
|
||||
}
|
||||
|
||||
fn tuple_get_item(b: &mut Bencher) {
|
||||
fn tuple_get_item(b: &mut Bencher<'_>) {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
const LEN: usize = 50_000;
|
||||
|
@ -38,7 +38,7 @@ fn tuple_get_item(b: &mut Bencher) {
|
|||
}
|
||||
|
||||
#[cfg(not(Py_LIMITED_API))]
|
||||
fn tuple_get_item_unchecked(b: &mut Bencher) {
|
||||
fn tuple_get_item_unchecked(b: &mut Bencher<'_>) {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
const LEN: usize = 50_000;
|
||||
|
|
|
@ -29,7 +29,7 @@ impl PyCounter {
|
|||
#[args(args = "*", kwargs = "**")]
|
||||
fn __call__(
|
||||
&mut self,
|
||||
py: Python,
|
||||
py: Python<'_>,
|
||||
args: &PyTuple,
|
||||
kwargs: Option<&PyDict>,
|
||||
) -> PyResult<Py<PyAny>> {
|
||||
|
@ -48,7 +48,7 @@ impl PyCounter {
|
|||
}
|
||||
|
||||
#[pymodule]
|
||||
pub fn decorator(_py: Python, module: &PyModule) -> PyResult<()> {
|
||||
pub fn decorator(_py: Python<'_>, module: &PyModule) -> PyResult<()> {
|
||||
module.add_class::<PyCounter>()?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ impl ExampleClass {
|
|||
|
||||
/// An example module implemented in Rust using PyO3.
|
||||
#[pymodule]
|
||||
fn maturin_starter(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn maturin_starter(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_class::<ExampleClass>()?;
|
||||
m.add_wrapped(wrap_pymodule!(submodule))?;
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ impl SubmoduleClass {
|
|||
}
|
||||
|
||||
#[pymodule]
|
||||
pub fn submodule(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
pub fn submodule(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_class::<SubmoduleClass>()?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ impl ExampleClass {
|
|||
|
||||
/// An example module implemented in Rust using PyO3.
|
||||
#[pymodule]
|
||||
fn _setuptools_rust_starter(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn _setuptools_rust_starter(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_class::<ExampleClass>()?;
|
||||
m.add_wrapped(wrap_pymodule!(submodule))?;
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ impl SubmoduleClass {
|
|||
}
|
||||
|
||||
#[pymodule]
|
||||
pub fn submodule(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
pub fn submodule(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_class::<SubmoduleClass>()?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ fn search_sequential(contents: &str, needle: &str) -> usize {
|
|||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn search_sequential_allow_threads(py: Python, contents: &str, needle: &str) -> usize {
|
||||
fn search_sequential_allow_threads(py: Python<'_>, contents: &str, needle: &str) -> usize {
|
||||
py.allow_threads(|| search_sequential(contents, needle))
|
||||
}
|
||||
|
||||
|
|
|
@ -113,7 +113,7 @@ The next step is to create the module initializer and add our class to it
|
|||
# struct Number(i32);
|
||||
#
|
||||
#[pymodule]
|
||||
fn my_module(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_class::<Number>()?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -273,7 +273,7 @@ impl SubClass {
|
|||
(SubClass { val2: 15 }, BaseClass::new())
|
||||
}
|
||||
|
||||
fn method2(self_: PyRef<Self>) -> PyResult<usize> {
|
||||
fn method2(self_: PyRef<'_, Self>) -> PyResult<usize> {
|
||||
let super_ = self_.as_ref(); // Get &BaseClass
|
||||
super_.method().map(|x| x * self_.val2)
|
||||
}
|
||||
|
@ -292,9 +292,9 @@ impl SubSubClass {
|
|||
.add_subclass(SubSubClass{val3: 20})
|
||||
}
|
||||
|
||||
fn method3(self_: PyRef<Self>) -> PyResult<usize> {
|
||||
fn method3(self_: PyRef<'_, Self>) -> PyResult<usize> {
|
||||
let v = self_.val3;
|
||||
let super_ = self_.into_super(); // Get PyRef<SubClass>
|
||||
let super_ = self_.into_super(); // Get PyRef<'_, SubClass>
|
||||
SubClass::method2(super_).map(|x| x * v)
|
||||
}
|
||||
}
|
||||
|
@ -329,7 +329,7 @@ impl DictWithCounter {
|
|||
fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
fn set(mut self_: PyRefMut<Self>, key: String, value: &PyAny) -> PyResult<()> {
|
||||
fn set(mut self_: PyRefMut<'_, Self>, key: String, value: &PyAny) -> PyResult<()> {
|
||||
self_.counter.entry(key.clone()).or_insert(0);
|
||||
let py = self_.py();
|
||||
let dict: &PyDict = unsafe { py.from_borrowed_ptr_or_err(self_.as_ptr())? };
|
||||
|
@ -524,7 +524,7 @@ gets injected by the method wrapper, e.g.
|
|||
# }
|
||||
#[pymethods]
|
||||
impl MyClass {
|
||||
fn method2(&self, py: Python) -> PyResult<i32> {
|
||||
fn method2(&self, py: Python<'_>) -> PyResult<i32> {
|
||||
Ok(10)
|
||||
}
|
||||
}
|
||||
|
@ -960,7 +960,7 @@ unsafe impl pyo3::PyTypeInfo for MyClass {
|
|||
const MODULE: Option<&'static str> = None;
|
||||
|
||||
#[inline]
|
||||
fn type_object_raw(py: pyo3::Python) -> *mut pyo3::ffi::PyTypeObject {
|
||||
fn type_object_raw(py: pyo3::Python<'_>) -> *mut pyo3::ffi::PyTypeObject {
|
||||
use pyo3::type_object::LazyStaticType;
|
||||
static TYPE_OBJECT: LazyStaticType = LazyStaticType::new();
|
||||
TYPE_OBJECT.get_or_init::<Self>(py)
|
||||
|
@ -974,7 +974,7 @@ impl pyo3::pyclass::PyClass for MyClass {
|
|||
}
|
||||
|
||||
impl pyo3::IntoPy<PyObject> for MyClass {
|
||||
fn into_py(self, py: pyo3::Python) -> pyo3::PyObject {
|
||||
fn into_py(self, py: pyo3::Python<'_>) -> pyo3::PyObject {
|
||||
pyo3::IntoPy::into_py(pyo3::Py::new(py, self).unwrap(), py)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -134,7 +134,7 @@ impl Number {
|
|||
#
|
||||
#[pymethods]
|
||||
impl Number {
|
||||
fn __pos__(slf: PyRef<Self>) -> PyRef<Self> {
|
||||
fn __pos__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
|
||||
slf
|
||||
}
|
||||
|
||||
|
@ -328,7 +328,7 @@ impl Number {
|
|||
}
|
||||
|
||||
#[pymodule]
|
||||
fn my_module(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_class::<Number>()?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -426,7 +426,7 @@ use pyo3::ffi;
|
|||
use pyo3::conversion::AsPyPointer;
|
||||
|
||||
fn wrap(obj: &PyAny) -> Result<i32, PyErr> {
|
||||
let py: Python = obj.py();
|
||||
let py: Python<'_> = obj.py();
|
||||
|
||||
unsafe {
|
||||
let ptr = obj.as_ptr();
|
||||
|
|
|
@ -17,7 +17,7 @@ impl Number {
|
|||
}
|
||||
|
||||
#[pymodule]
|
||||
fn my_module(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_class::<Number>()?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -220,7 +220,7 @@ impl Number {
|
|||
}
|
||||
|
||||
#[pymodule]
|
||||
fn my_module(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_class::<Number>()?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -22,8 +22,8 @@ When PyO3 handles a magic method, a couple of changes apply compared to other `#
|
|||
The following sections list of all magic methods PyO3 currently handles. The
|
||||
given signatures should be interpreted as follows:
|
||||
- All methods take a receiver as first argument, shown as `<self>`. It can be
|
||||
`&self`, `&mut self` or a `PyCell` reference like `self_: PyRef<Self>` and
|
||||
`self_: PyRefMut<Self>`, as described [here](../class.md#inheritance).
|
||||
`&self`, `&mut self` or a `PyCell` reference like `self_: PyRef<'_, Self>` and
|
||||
`self_: PyRefMut<'_, Self>`, as described [here](../class.md#inheritance).
|
||||
- An optional `Python<'py>` argument is always allowed as the first argument.
|
||||
- Return values can be optionally wrapped in `PyResult`.
|
||||
- `object` means that any type is allowed that can be extracted from a Python
|
||||
|
@ -118,10 +118,10 @@ struct MyIterator {
|
|||
|
||||
#[pymethods]
|
||||
impl MyIterator {
|
||||
fn __iter__(slf: PyRef<Self>) -> PyRef<Self> {
|
||||
fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
|
||||
slf
|
||||
}
|
||||
fn __next__(mut slf: PyRefMut<Self>) -> Option<PyObject> {
|
||||
fn __next__(mut slf: PyRefMut<'_, Self>) -> Option<PyObject> {
|
||||
slf.iter.next()
|
||||
}
|
||||
}
|
||||
|
@ -142,11 +142,11 @@ struct Iter {
|
|||
|
||||
#[pymethods]
|
||||
impl Iter {
|
||||
fn __iter__(slf: PyRef<Self>) -> PyRef<Self> {
|
||||
fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
|
||||
slf
|
||||
}
|
||||
|
||||
fn __next__(mut slf: PyRefMut<Self>) -> Option<usize> {
|
||||
fn __next__(mut slf: PyRefMut<'_, Self>) -> Option<usize> {
|
||||
slf.inner.next()
|
||||
}
|
||||
}
|
||||
|
@ -158,7 +158,7 @@ struct Container {
|
|||
|
||||
#[pymethods]
|
||||
impl Container {
|
||||
fn __iter__(slf: PyRef<Self>) -> PyResult<Py<Iter>> {
|
||||
fn __iter__(slf: PyRef<'_, Self>) -> PyResult<Py<Iter>> {
|
||||
let iter = Iter {
|
||||
inner: slf.iter.clone().into_iter(),
|
||||
};
|
||||
|
@ -355,7 +355,7 @@ object. `__clear__` must clear out any mutable references to other Python
|
|||
objects (thus breaking reference cycles). Immutable references do not have to be
|
||||
cleared, as every cycle must contain at least one mutable reference.
|
||||
|
||||
- `__traverse__(<self>, pyo3::class::gc::PyVisit) -> Result<(), pyo3::class::gc::PyTraverseError>`
|
||||
- `__traverse__(<self>, pyo3::class::gc::PyVisit<'_>) -> Result<(), pyo3::class::gc::PyTraverseError>`
|
||||
- `__clear__(<self>) -> ()`
|
||||
|
||||
Example:
|
||||
|
@ -372,7 +372,7 @@ struct ClassWithGCSupport {
|
|||
|
||||
#[pymethods]
|
||||
impl ClassWithGCSupport {
|
||||
fn __traverse__(&self, visit: PyVisit) -> Result<(), PyTraverseError> {
|
||||
fn __traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError> {
|
||||
if let Some(obj) = &self.obj {
|
||||
visit.call(obj)?
|
||||
}
|
||||
|
@ -580,10 +580,10 @@ For a mapping, the keys may be Python objects of arbitrary type.
|
|||
|
||||
Iterators can be defined using the [`PyIterProtocol`] trait.
|
||||
It includes two methods `__iter__` and `__next__`:
|
||||
* `fn __iter__(slf: PyRefMut<Self>) -> PyResult<impl IntoPy<PyObject>>`
|
||||
* `fn __next__(slf: PyRefMut<Self>) -> PyResult<Option<impl IntoPy<PyObject>>>`
|
||||
* `fn __iter__(slf: PyRefMut<'_, Self>) -> PyResult<impl IntoPy<PyObject>>`
|
||||
* `fn __next__(slf: PyRefMut<'_, Self>) -> PyResult<Option<impl IntoPy<PyObject>>>`
|
||||
|
||||
These two methods can be take either `PyRef<Self>` or `PyRefMut<Self>` as their
|
||||
These two methods can be take either `PyRef<'_, Self>` or `PyRefMut<'_, Self>` as their
|
||||
first argument, so that mutable borrow can be avoided if needed.
|
||||
|
||||
For details, look at the `#[pymethods]` regarding iterator methods.
|
||||
|
|
|
@ -253,7 +253,7 @@ enum RustyEnum<'a> {
|
|||
# Python::with_gil(|py| -> PyResult<()> {
|
||||
# {
|
||||
# let thing = 42_u8.to_object(py);
|
||||
# let rust_thing: RustyEnum = thing.extract(py)?;
|
||||
# let rust_thing: RustyEnum<'_> = thing.extract(py)?;
|
||||
#
|
||||
# assert_eq!(
|
||||
# 42,
|
||||
|
@ -265,7 +265,7 @@ enum RustyEnum<'a> {
|
|||
# }
|
||||
# {
|
||||
# let thing = PyString::new(py, "text");
|
||||
# let rust_thing: RustyEnum = thing.extract()?;
|
||||
# let rust_thing: RustyEnum<'_> = thing.extract()?;
|
||||
#
|
||||
# assert_eq!(
|
||||
# "text",
|
||||
|
@ -277,7 +277,7 @@ enum RustyEnum<'a> {
|
|||
# }
|
||||
# {
|
||||
# let thing = (32_u8, 73_u8).to_object(py);
|
||||
# let rust_thing: RustyEnum = thing.extract(py)?;
|
||||
# let rust_thing: RustyEnum<'_> = thing.extract(py)?;
|
||||
#
|
||||
# assert_eq!(
|
||||
# (32, 73),
|
||||
|
@ -289,7 +289,7 @@ enum RustyEnum<'a> {
|
|||
# }
|
||||
# {
|
||||
# let thing = ("foo", 73_u8).to_object(py);
|
||||
# let rust_thing: RustyEnum = thing.extract(py)?;
|
||||
# let rust_thing: RustyEnum<'_> = thing.extract(py)?;
|
||||
#
|
||||
# assert_eq!(
|
||||
# (String::from("foo"), 73),
|
||||
|
@ -313,7 +313,7 @@ enum RustyEnum<'a> {
|
|||
#
|
||||
# let class = module.getattr("Foo")?;
|
||||
# let instance = class.call0()?;
|
||||
# let rust_thing: RustyEnum = instance.extract()?;
|
||||
# let rust_thing: RustyEnum<'_> = instance.extract()?;
|
||||
#
|
||||
# assert_eq!(
|
||||
# (0, 1, 2),
|
||||
|
@ -337,7 +337,7 @@ enum RustyEnum<'a> {
|
|||
#
|
||||
# let class = module.getattr("Foo")?;
|
||||
# let instance = class.call0()?;
|
||||
# let rust_thing: RustyEnum = instance.extract()?;
|
||||
# let rust_thing: RustyEnum<'_> = instance.extract()?;
|
||||
#
|
||||
# assert_eq!(
|
||||
# (3, 4),
|
||||
|
@ -350,7 +350,7 @@ enum RustyEnum<'a> {
|
|||
#
|
||||
# {
|
||||
# let thing = PyBytes::new(py, b"text");
|
||||
# let rust_thing: RustyEnum = thing.extract()?;
|
||||
# let rust_thing: RustyEnum<'_> = thing.extract()?;
|
||||
#
|
||||
# assert_eq!(
|
||||
# b"text",
|
||||
|
@ -464,7 +464,7 @@ use pyo3::prelude::*;
|
|||
struct MyPyObjectWrapper(PyObject);
|
||||
|
||||
impl IntoPy<PyObject> for MyPyObjectWrapper {
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
|
|
@ -118,7 +118,7 @@ Export an async function that makes use of `async-std`:
|
|||
use pyo3::{prelude::*, wrap_pyfunction};
|
||||
|
||||
#[pyfunction]
|
||||
fn rust_sleep(py: Python) -> PyResult<&PyAny> {
|
||||
fn rust_sleep(py: Python<'_>) -> PyResult<&PyAny> {
|
||||
pyo3_asyncio::async_std::future_into_py(py, async {
|
||||
async_std::task::sleep(std::time::Duration::from_secs(1)).await;
|
||||
Ok(Python::with_gil(|py| py.None()))
|
||||
|
@ -126,7 +126,7 @@ fn rust_sleep(py: Python) -> PyResult<&PyAny> {
|
|||
}
|
||||
|
||||
#[pymodule]
|
||||
fn my_async_module(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn my_async_module(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_function(wrap_pyfunction!(rust_sleep, m)?)?;
|
||||
|
||||
Ok(())
|
||||
|
@ -142,7 +142,7 @@ If you want to use `tokio` instead, here's what your module should look like:
|
|||
use pyo3::{prelude::*, wrap_pyfunction};
|
||||
|
||||
#[pyfunction]
|
||||
fn rust_sleep(py: Python) -> PyResult<&PyAny> {
|
||||
fn rust_sleep(py: Python<'_>) -> PyResult<&PyAny> {
|
||||
pyo3_asyncio::tokio::future_into_py(py, async {
|
||||
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
Ok(Python::with_gil(|py| py.None()))
|
||||
|
@ -150,7 +150,7 @@ fn rust_sleep(py: Python) -> PyResult<&PyAny> {
|
|||
}
|
||||
|
||||
#[pymodule]
|
||||
fn my_async_module(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn my_async_module(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_function(wrap_pyfunction!(rust_sleep, m)?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -316,7 +316,7 @@ async fn rust_sleep() {
|
|||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn call_rust_sleep(py: Python) -> PyResult<&PyAny> {
|
||||
fn call_rust_sleep(py: Python<'_>) -> PyResult<&PyAny> {
|
||||
pyo3_asyncio::async_std::future_into_py(py, async move {
|
||||
rust_sleep().await;
|
||||
Ok(Python::with_gil(|py| py.None()))
|
||||
|
@ -466,7 +466,7 @@ tokio = "1.4"
|
|||
use pyo3::{prelude::*, wrap_pyfunction};
|
||||
|
||||
#[pyfunction]
|
||||
fn rust_sleep(py: Python) -> PyResult<&PyAny> {
|
||||
fn rust_sleep(py: Python<'_>) -> PyResult<&PyAny> {
|
||||
pyo3_asyncio::tokio::future_into_py(py, async {
|
||||
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
Ok(Python::with_gil(|py| py.None()))
|
||||
|
@ -474,7 +474,7 @@ fn rust_sleep(py: Python) -> PyResult<&PyAny> {
|
|||
}
|
||||
|
||||
#[pymodule]
|
||||
fn my_async_module(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn my_async_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_function(wrap_pyfunction!(rust_sleep, m)?)?;
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -38,7 +38,7 @@ the module like this, so that it is importable from Python:
|
|||
create_exception!(mymodule, CustomError, PyException);
|
||||
|
||||
#[pymodule]
|
||||
fn mymodule(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn mymodule(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
// ... other elements added to module ...
|
||||
m.add("CustomError", py.get_type::<CustomError>())?;
|
||||
|
||||
|
@ -151,7 +151,7 @@ struct CustomIOError;
|
|||
impl std::error::Error for CustomIOError {}
|
||||
|
||||
impl fmt::Display for CustomIOError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "Oh no!")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -116,7 +116,7 @@ struct Outer {
|
|||
#[pymethods]
|
||||
impl Outer {
|
||||
#[new]
|
||||
fn __new__(py: Python) -> PyResult<Self> {
|
||||
fn __new__(py: Python<'_>) -> PyResult<Self> {
|
||||
Ok(Self {
|
||||
inner: Py::new(py, Inner {})?,
|
||||
})
|
||||
|
|
|
@ -13,7 +13,7 @@ fn double(x: usize) -> usize {
|
|||
}
|
||||
|
||||
#[pymodule]
|
||||
fn my_extension(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn my_extension(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_function(wrap_pyfunction!(double, m)?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ The `#[pyo3]` attribute can be used to modify properties of the generated Python
|
|||
fn no_args_py() -> usize { 42 }
|
||||
|
||||
#[pymodule]
|
||||
fn module_with_functions(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn module_with_functions(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_function(wrap_pyfunction!(no_args_py, m)?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -113,7 +113,7 @@ The `#[pyo3]` attribute can be used to modify properties of the generated Python
|
|||
}
|
||||
|
||||
#[pymodule]
|
||||
fn module_with_fn(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn module_with_fn(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_function(wrap_pyfunction!(pyfunction_with_module, m)?)
|
||||
}
|
||||
```
|
||||
|
@ -132,7 +132,7 @@ fn num_kwds(kwds: Option<&PyDict>) -> usize {
|
|||
}
|
||||
|
||||
#[pymodule]
|
||||
fn module_with_functions(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn module_with_functions(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_function(wrap_pyfunction!(num_kwds, m)?).unwrap();
|
||||
Ok(())
|
||||
}
|
||||
|
@ -273,7 +273,7 @@ An example of `#[pyfn]` is below:
|
|||
use pyo3::prelude::*;
|
||||
|
||||
#[pymodule]
|
||||
fn my_extension(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn my_extension(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
|
||||
#[pyfn(m)]
|
||||
fn double(x: usize) -> usize {
|
||||
|
@ -291,7 +291,7 @@ documented in the rest of this chapter. The code above is expanded to the follow
|
|||
use pyo3::prelude::*;
|
||||
|
||||
#[pymodule]
|
||||
fn my_extension(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn my_extension(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
|
||||
#[pyfunction]
|
||||
fn double(x: usize) -> usize {
|
||||
|
|
|
@ -117,7 +117,7 @@ mod foo {
|
|||
use pyo3::prelude::*;
|
||||
|
||||
#[pymodule]
|
||||
fn private_submodule(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn private_submodule(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -126,7 +126,7 @@ use pyo3::prelude::*;
|
|||
use foo::*;
|
||||
|
||||
#[pymodule]
|
||||
fn my_module(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_wrapped(wrap_pymodule!(private_submodule))?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -139,7 +139,7 @@ mod foo {
|
|||
use pyo3::prelude::*;
|
||||
|
||||
#[pymodule]
|
||||
pub(crate) fn private_submodule(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
pub(crate) fn private_submodule(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -149,7 +149,7 @@ use pyo3::wrap_pymodule;
|
|||
use foo::*;
|
||||
|
||||
#[pymodule]
|
||||
fn my_module(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_wrapped(wrap_pymodule!(private_submodule))?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -346,7 +346,7 @@ Before:
|
|||
struct MyPyObjectWrapper(PyObject);
|
||||
|
||||
impl FromPy<MyPyObjectWrapper> for PyObject {
|
||||
fn from_py(other: MyPyObjectWrapper, _py: Python) -> Self {
|
||||
fn from_py(other: MyPyObjectWrapper, _py: Python<'_>) -> Self {
|
||||
other.0
|
||||
}
|
||||
}
|
||||
|
@ -358,7 +358,7 @@ After
|
|||
struct MyPyObjectWrapper(PyObject);
|
||||
|
||||
impl IntoPy<PyObject> for MyPyObjectWrapper {
|
||||
fn into_py(self, _py: Python) -> PyObject {
|
||||
fn into_py(self, _py: Python<'_>) -> PyObject {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
@ -676,10 +676,10 @@ let obj: &PyAny = create_obj();
|
|||
let obj_cell: &PyCell<MyClass> = obj.extract().unwrap();
|
||||
let obj_cloned: MyClass = obj.extract().unwrap(); // extracted by cloning the object
|
||||
{
|
||||
let obj_ref: PyRef<MyClass> = obj.extract().unwrap();
|
||||
let obj_ref: PyRef<'_, MyClass> = obj.extract().unwrap();
|
||||
// we need to drop obj_ref before we can extract a PyRefMut due to Rust's rules of references
|
||||
}
|
||||
let obj_ref_mut: PyRefMut<MyClass> = obj.extract().unwrap();
|
||||
let obj_ref_mut: PyRefMut<'_, MyClass> = obj.extract().unwrap();
|
||||
# })
|
||||
```
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ fn double(x: usize) -> usize {
|
|||
|
||||
/// This module is implemented in Rust.
|
||||
#[pymodule]
|
||||
fn my_extension(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn my_extension(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_function(wrap_pyfunction!(double, m)?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ fn double(x: usize) -> usize {
|
|||
|
||||
#[pymodule]
|
||||
#[pyo3(name = "custom_name")]
|
||||
fn my_extension(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn my_extension(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_function(wrap_pyfunction!(double, m)?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ references to the `PyModule` so that each module registers its own FFI code. For
|
|||
use pyo3::prelude::*;
|
||||
|
||||
#[pymodule]
|
||||
fn my_extension(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn my_extension(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
dirutil::register(py, m)?;
|
||||
osutil::register(py, m)?;
|
||||
Ok(())
|
||||
|
@ -86,7 +86,7 @@ fn my_extension(py: Python, m: &PyModule) -> PyResult<()> {
|
|||
# mod dirutil {
|
||||
use pyo3::prelude::*;
|
||||
|
||||
pub(crate) fn register(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
pub(crate) fn register(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_class::<SomeClass>()?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -99,7 +99,7 @@ struct SomeClass {/* ... */}
|
|||
# mod osutil {
|
||||
use pyo3::prelude::*;
|
||||
|
||||
pub(crate) fn register(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
pub(crate) fn register(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_function(wrap_pyfunction!(determine_current_os, m)?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -120,7 +120,7 @@ use pyo3::prelude::*;
|
|||
use osutil::*;
|
||||
|
||||
#[pymodule]
|
||||
fn my_extension(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn my_extension(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_function(wrap_pyfunction!(determine_current_os, m)?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -146,12 +146,12 @@ For example, you could define the modules `parent_module` and `parent_module.chi
|
|||
use pyo3::prelude::*;
|
||||
|
||||
#[pymodule]
|
||||
fn parent_module(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn parent_module(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
register_child_module(py, m)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn register_child_module(py: Python, parent_module: &PyModule) -> PyResult<()> {
|
||||
fn register_child_module(py: Python<'_>, parent_module: &PyModule) -> PyResult<()> {
|
||||
let child_module = PyModule::new(py, "child_module")?;
|
||||
child_module.add_function(wrap_pyfunction!(func, child_module)?)?;
|
||||
parent_module.add_submodule(child_module)?;
|
||||
|
|
|
@ -23,7 +23,7 @@ fn search_sequential(contents: &str, needle: &str) -> usize {
|
|||
To enable parallel execution of this function, the [`Python::allow_threads`] method can be used to temporarily release the GIL, thus allowing other Python threads to run. We then have a function exposed to the Python runtime which calls `search_sequential` inside a closure passed to [`Python::allow_threads`] to enable true parallelism:
|
||||
```rust, ignore
|
||||
#[pyfunction]
|
||||
fn search_sequential_allow_threads(py: Python, contents: &str, needle: &str) -> usize {
|
||||
fn search_sequential_allow_threads(py: Python<'_>, contents: &str, needle: &str) -> usize {
|
||||
py.allow_threads(|| search_sequential(contents, needle))
|
||||
}
|
||||
```
|
||||
|
|
|
@ -55,9 +55,9 @@ Here is an example of the PyList API:
|
|||
```rust,ignore
|
||||
impl PyList {
|
||||
|
||||
fn new(py: Python) -> PyList {...}
|
||||
fn new(py: Python<'_>) -> PyList {...}
|
||||
|
||||
fn get_item(&self, py: Python, index: isize) -> PyObject {...}
|
||||
fn get_item(&self, py: Python<'_>, index: isize) -> PyObject {...}
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -66,7 +66,7 @@ impl PyList {
|
|||
```rust,ignore
|
||||
impl PyList {
|
||||
|
||||
fn new(py: Python) -> &PyList {...}
|
||||
fn new(py: Python<'_>) -> &PyList {...}
|
||||
|
||||
fn get_item(&self, index: isize) -> &PyAny {...}
|
||||
}
|
||||
|
|
|
@ -133,7 +133,7 @@ struct UserModel {
|
|||
}
|
||||
|
||||
#[pymodule]
|
||||
fn trait_exposure(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn trait_exposure(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_class::<UserModel>()?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -487,7 +487,7 @@ pub struct UserModel {
|
|||
}
|
||||
|
||||
#[pymodule]
|
||||
fn trait_exposure(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn trait_exposure(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_class::<UserModel>()?;
|
||||
m.add_function(wrap_pyfunction!(solve_wrapper, m)?)?;
|
||||
Ok(())
|
||||
|
|
|
@ -103,9 +103,9 @@ let _: Py<MyClass> = obj.extract()?;
|
|||
// To MyClass with PyAny::extract, if MyClass: Clone
|
||||
let _: MyClass = obj.extract()?;
|
||||
|
||||
// To PyRef<MyClass> or PyRefMut<MyClass> with PyAny::extract
|
||||
let _: PyRef<MyClass> = obj.extract()?;
|
||||
let _: PyRefMut<MyClass> = obj.extract()?;
|
||||
// To PyRef<'_, MyClass> or PyRefMut<'_, MyClass> with PyAny::extract
|
||||
let _: PyRef<'_, MyClass> = obj.extract()?;
|
||||
let _: PyRefMut<'_, MyClass> = obj.extract()?;
|
||||
# Ok(())
|
||||
# }).unwrap();
|
||||
```
|
||||
|
@ -207,11 +207,11 @@ let _: &PyCell<MyClass> = my_class.into_ref(py);
|
|||
let _: Py<PyAny> = my_class.into_py(py);
|
||||
|
||||
# let my_class = my_class_clone;
|
||||
// To PyRef<MyClass> with Py::borrow or Py::try_borrow
|
||||
let _: PyRef<MyClass> = my_class.try_borrow(py)?;
|
||||
// To PyRef<'_, MyClass> with Py::borrow or Py::try_borrow
|
||||
let _: PyRef<'_, MyClass> = my_class.try_borrow(py)?;
|
||||
|
||||
// To PyRefMut<MyClass> with Py::borrow_mut or Py::try_borrow_mut
|
||||
let _: PyRefMut<MyClass> = my_class.try_borrow_mut(py)?;
|
||||
// To PyRefMut<'_, MyClass> with Py::borrow_mut or Py::try_borrow_mut
|
||||
let _: PyRefMut<'_, MyClass> = my_class.try_borrow_mut(py)?;
|
||||
# Ok(())
|
||||
# }).unwrap();
|
||||
# });
|
||||
|
@ -241,12 +241,12 @@ so it also exposes all of the methods on `PyAny`.
|
|||
let cell: &PyCell<MyClass> = PyCell::new(py, MyClass { })?;
|
||||
|
||||
// To PyRef<T> with .borrow() or .try_borrow()
|
||||
let py_ref: PyRef<MyClass> = cell.try_borrow()?;
|
||||
let py_ref: PyRef<'_, MyClass> = cell.try_borrow()?;
|
||||
let _: &MyClass = &*py_ref;
|
||||
# drop(py_ref);
|
||||
|
||||
// To PyRefMut<T> with .borrow_mut() or .try_borrow_mut()
|
||||
let mut py_ref_mut: PyRefMut<MyClass> = cell.try_borrow_mut()?;
|
||||
let mut py_ref_mut: PyRefMut<'_, MyClass> = cell.try_borrow_mut()?;
|
||||
let _: &mut MyClass = &mut *py_ref_mut;
|
||||
# Ok(())
|
||||
# }).unwrap();
|
||||
|
|
|
@ -23,7 +23,7 @@ pub mod kw {
|
|||
pub struct FromPyWithAttribute(pub ExprPath);
|
||||
|
||||
impl Parse for FromPyWithAttribute {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
fn parse(input: ParseStream<'_>) -> Result<Self> {
|
||||
let _: kw::from_py_with = input.parse()?;
|
||||
let _: Token![=] = input.parse()?;
|
||||
let string_literal: LitStr = input.parse()?;
|
||||
|
@ -35,7 +35,7 @@ impl Parse for FromPyWithAttribute {
|
|||
pub struct NameAttribute(pub Ident);
|
||||
|
||||
impl Parse for NameAttribute {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
fn parse(input: ParseStream<'_>) -> Result<Self> {
|
||||
let _: kw::name = input.parse()?;
|
||||
let _: Token![=] = input.parse()?;
|
||||
let string_literal: LitStr = input.parse()?;
|
||||
|
@ -48,7 +48,7 @@ impl Parse for NameAttribute {
|
|||
pub struct CrateAttribute(pub Path);
|
||||
|
||||
impl Parse for CrateAttribute {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
fn parse(input: ParseStream<'_>) -> Result<Self> {
|
||||
let _: Token![crate] = input.parse()?;
|
||||
let _: Token![=] = input.parse()?;
|
||||
let string_literal: LitStr = input.parse()?;
|
||||
|
@ -64,7 +64,7 @@ pub struct TextSignatureAttribute {
|
|||
}
|
||||
|
||||
impl Parse for TextSignatureAttribute {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
fn parse(input: ParseStream<'_>) -> Result<Self> {
|
||||
Ok(TextSignatureAttribute {
|
||||
kw: input.parse()?,
|
||||
eq_token: input.parse()?,
|
||||
|
|
|
@ -346,7 +346,7 @@ enum ContainerPyO3Attribute {
|
|||
}
|
||||
|
||||
impl Parse for ContainerPyO3Attribute {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
fn parse(input: ParseStream<'_>) -> Result<Self> {
|
||||
let lookahead = input.lookahead1();
|
||||
if lookahead.peek(attributes::kw::transparent) {
|
||||
let kw: attributes::kw::transparent = input.parse()?;
|
||||
|
@ -419,7 +419,7 @@ enum FieldPyO3Attribute {
|
|||
}
|
||||
|
||||
impl Parse for FieldPyO3Attribute {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
fn parse(input: ParseStream<'_>) -> Result<Self> {
|
||||
let lookahead = input.lookahead1();
|
||||
if lookahead.peek(attributes::kw::attribute) {
|
||||
let _: attributes::kw::attribute = input.parse()?;
|
||||
|
|
|
@ -19,7 +19,7 @@ pub struct ConstSpec {
|
|||
}
|
||||
|
||||
impl ConstSpec {
|
||||
pub fn python_name(&self) -> Cow<Ident> {
|
||||
pub fn python_name(&self) -> Cow<'_, Ident> {
|
||||
if let Some(name) = &self.attributes.name {
|
||||
Cow::Borrowed(&name.0)
|
||||
} else {
|
||||
|
@ -45,7 +45,7 @@ pub enum PyO3ConstAttribute {
|
|||
}
|
||||
|
||||
impl Parse for PyO3ConstAttribute {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
fn parse(input: ParseStream<'_>) -> Result<Self> {
|
||||
let lookahead = input.lookahead1();
|
||||
if lookahead.peek(attributes::kw::name) {
|
||||
input.parse().map(PyO3ConstAttribute::Name)
|
||||
|
|
|
@ -120,7 +120,7 @@ pub struct PyFnArgs {
|
|||
}
|
||||
|
||||
impl Parse for PyFnArgs {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
fn parse(input: syn::parse::ParseStream<'_>) -> syn::Result<Self> {
|
||||
let modname = input.parse().map_err(
|
||||
|e| err_spanned!(e.span() => "expected module as first argument to #[pyfn()]"),
|
||||
)?;
|
||||
|
@ -173,7 +173,7 @@ enum PyModulePyO3Option {
|
|||
}
|
||||
|
||||
impl Parse for PyModulePyO3Option {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
fn parse(input: ParseStream<'_>) -> Result<Self> {
|
||||
let lookahead = input.lookahead1();
|
||||
if lookahead.peek(attributes::kw::name) {
|
||||
input.parse().map(PyModulePyO3Option::Name)
|
||||
|
|
|
@ -38,7 +38,7 @@ pub struct PyClassArgs {
|
|||
}
|
||||
|
||||
impl PyClassArgs {
|
||||
fn parse(input: ParseStream, kind: PyClassKind) -> Result<Self> {
|
||||
fn parse(input: ParseStream<'_>, kind: PyClassKind) -> Result<Self> {
|
||||
let mut slf = PyClassArgs::new(kind);
|
||||
let vars = Punctuated::<Expr, Token![,]>::parse_terminated(input)?;
|
||||
for expr in vars {
|
||||
|
@ -47,11 +47,11 @@ impl PyClassArgs {
|
|||
Ok(slf)
|
||||
}
|
||||
|
||||
pub fn parse_stuct_args(input: ParseStream) -> syn::Result<Self> {
|
||||
pub fn parse_stuct_args(input: ParseStream<'_>) -> syn::Result<Self> {
|
||||
Self::parse(input, PyClassKind::Struct)
|
||||
}
|
||||
|
||||
pub fn parse_enum_args(input: ParseStream) -> syn::Result<Self> {
|
||||
pub fn parse_enum_args(input: ParseStream<'_>) -> syn::Result<Self> {
|
||||
Self::parse(input, PyClassKind::Enum)
|
||||
}
|
||||
|
||||
|
@ -197,7 +197,7 @@ enum PyClassPyO3Option {
|
|||
}
|
||||
|
||||
impl Parse for PyClassPyO3Option {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
fn parse(input: ParseStream<'_>) -> Result<Self> {
|
||||
let lookahead = input.lookahead1();
|
||||
if lookahead.peek(attributes::kw::text_signature) {
|
||||
input.parse().map(PyClassPyO3Option::TextSignature)
|
||||
|
@ -315,7 +315,7 @@ enum FieldPyO3Option {
|
|||
}
|
||||
|
||||
impl Parse for FieldPyO3Option {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
fn parse(input: ParseStream<'_>) -> Result<Self> {
|
||||
let lookahead = input.lookahead1();
|
||||
if lookahead.peek(attributes::kw::get) {
|
||||
input.parse().map(FieldPyO3Option::Get)
|
||||
|
@ -478,7 +478,7 @@ pub fn build_py_enum(
|
|||
}
|
||||
|
||||
fn impl_enum(
|
||||
enum_: PyClassEnum,
|
||||
enum_: PyClassEnum<'_>,
|
||||
args: &PyClassArgs,
|
||||
doc: PythonDoc,
|
||||
methods_type: PyClassMethodsType,
|
||||
|
@ -489,7 +489,7 @@ fn impl_enum(
|
|||
}
|
||||
|
||||
fn impl_enum_class(
|
||||
enum_: PyClassEnum,
|
||||
enum_: PyClassEnum<'_>,
|
||||
args: &PyClassArgs,
|
||||
doc: PythonDoc,
|
||||
methods_type: PyClassMethodsType,
|
||||
|
@ -630,7 +630,7 @@ fn enum_default_slots(
|
|||
gen_default_items(cls, default_items).collect()
|
||||
}
|
||||
|
||||
fn extract_variant_data(variant: &syn::Variant) -> syn::Result<PyClassEnumVariant> {
|
||||
fn extract_variant_data(variant: &syn::Variant) -> syn::Result<PyClassEnumVariant<'_>> {
|
||||
use syn::Fields;
|
||||
let ident = match variant.fields {
|
||||
Fields::Unit => &variant.ident,
|
||||
|
|
|
@ -50,7 +50,7 @@ enum PyFunctionArgPyO3Attribute {
|
|||
}
|
||||
|
||||
impl Parse for PyFunctionArgPyO3Attribute {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
fn parse(input: ParseStream<'_>) -> Result<Self> {
|
||||
let lookahead = input.lookahead1();
|
||||
if lookahead.peek(attributes::kw::from_py_with) {
|
||||
input.parse().map(PyFunctionArgPyO3Attribute::FromPyWith)
|
||||
|
@ -87,7 +87,7 @@ impl PyFunctionArgPyO3Attributes {
|
|||
}
|
||||
|
||||
impl syn::parse::Parse for PyFunctionSignature {
|
||||
fn parse(input: &ParseBuffer) -> syn::Result<Self> {
|
||||
fn parse(input: &ParseBuffer<'_>) -> syn::Result<Self> {
|
||||
let attr = Punctuated::<NestedMeta, syn::Token![,]>::parse_terminated(input)?;
|
||||
Self::from_meta(&attr)
|
||||
}
|
||||
|
@ -243,7 +243,7 @@ pub struct PyFunctionOptions {
|
|||
}
|
||||
|
||||
impl Parse for PyFunctionOptions {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
fn parse(input: ParseStream<'_>) -> Result<Self> {
|
||||
let mut options = PyFunctionOptions::default();
|
||||
|
||||
while !input.is_empty() {
|
||||
|
@ -282,7 +282,7 @@ pub enum PyFunctionOption {
|
|||
}
|
||||
|
||||
impl Parse for PyFunctionOption {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
fn parse(input: ParseStream<'_>) -> Result<Self> {
|
||||
let lookahead = input.lookahead1();
|
||||
if lookahead.peek(attributes::kw::name) {
|
||||
input.parse().map(PyFunctionOption::Name)
|
||||
|
|
|
@ -30,7 +30,7 @@ enum PyImplPyO3Option {
|
|||
}
|
||||
|
||||
impl Parse for PyImplPyO3Option {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
fn parse(input: ParseStream<'_>) -> Result<Self> {
|
||||
let lookahead = input.lookahead1();
|
||||
if lookahead.peek(syn::Token![crate]) {
|
||||
input.parse().map(PyImplPyO3Option::Crate)
|
||||
|
|
|
@ -249,7 +249,10 @@ fn ensure_function_options_valid(options: &PyFunctionOptions) -> syn::Result<()>
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn ensure_no_forbidden_protocol_attributes(spec: &FnSpec, method_name: &str) -> syn::Result<()> {
|
||||
fn ensure_no_forbidden_protocol_attributes(
|
||||
spec: &FnSpec<'_>,
|
||||
method_name: &str,
|
||||
) -> syn::Result<()> {
|
||||
if let Some(text_signature) = &spec.text_signature {
|
||||
bail_spanned!(text_signature.kw.span() => format!("`text_signature` cannot be used with `{}`", method_name));
|
||||
}
|
||||
|
@ -259,7 +262,7 @@ fn ensure_no_forbidden_protocol_attributes(spec: &FnSpec, method_name: &str) ->
|
|||
/// Also used by pyfunction.
|
||||
pub fn impl_py_method_def(
|
||||
cls: &syn::Type,
|
||||
spec: &FnSpec,
|
||||
spec: &FnSpec<'_>,
|
||||
flags: Option<TokenStream>,
|
||||
) -> Result<TokenStream> {
|
||||
let wrapper_ident = syn::Ident::new("__wrap", Span::call_site());
|
||||
|
@ -276,7 +279,7 @@ pub fn impl_py_method_def(
|
|||
})
|
||||
}
|
||||
|
||||
fn impl_py_method_def_new(cls: &syn::Type, spec: &FnSpec) -> Result<TokenStream> {
|
||||
fn impl_py_method_def_new(cls: &syn::Type, spec: &FnSpec<'_>) -> Result<TokenStream> {
|
||||
let wrapper_ident = syn::Ident::new("__pymethod__new__", Span::call_site());
|
||||
let wrapper = spec.get_wrapper_function(&wrapper_ident, Some(cls))?;
|
||||
Ok(quote! {{
|
||||
|
@ -291,7 +294,7 @@ fn impl_py_method_def_new(cls: &syn::Type, spec: &FnSpec) -> Result<TokenStream>
|
|||
}})
|
||||
}
|
||||
|
||||
fn impl_call_slot(cls: &syn::Type, mut spec: FnSpec) -> Result<TokenStream> {
|
||||
fn impl_call_slot(cls: &syn::Type, mut spec: FnSpec<'_>) -> Result<TokenStream> {
|
||||
// HACK: __call__ proto slot must always use varargs calling convention, so change the spec.
|
||||
// Probably indicates there's a refactoring opportunity somewhere.
|
||||
spec.convention = CallingConvention::Varargs;
|
||||
|
@ -307,7 +310,7 @@ fn impl_call_slot(cls: &syn::Type, mut spec: FnSpec) -> Result<TokenStream> {
|
|||
}})
|
||||
}
|
||||
|
||||
fn impl_traverse_slot(cls: &syn::Type, spec: FnSpec) -> TokenStream {
|
||||
fn impl_traverse_slot(cls: &syn::Type, spec: FnSpec<'_>) -> TokenStream {
|
||||
let ident = spec.name;
|
||||
quote! {{
|
||||
pub unsafe extern "C" fn __wrap_(
|
||||
|
@ -337,7 +340,7 @@ fn impl_traverse_slot(cls: &syn::Type, spec: FnSpec) -> TokenStream {
|
|||
}}
|
||||
}
|
||||
|
||||
fn impl_py_class_attribute(cls: &syn::Type, spec: &FnSpec) -> TokenStream {
|
||||
fn impl_py_class_attribute(cls: &syn::Type, spec: &FnSpec<'_>) -> TokenStream {
|
||||
let name = &spec.name;
|
||||
let deprecations = &spec.deprecations;
|
||||
let python_name = spec.null_terminated_python_name();
|
||||
|
@ -357,7 +360,7 @@ fn impl_py_class_attribute(cls: &syn::Type, spec: &FnSpec) -> TokenStream {
|
|||
}
|
||||
}
|
||||
|
||||
fn impl_call_setter(cls: &syn::Type, spec: &FnSpec) -> syn::Result<TokenStream> {
|
||||
fn impl_call_setter(cls: &syn::Type, spec: &FnSpec<'_>) -> syn::Result<TokenStream> {
|
||||
let (py_arg, args) = split_off_python_arg(&spec.args);
|
||||
|
||||
if args.is_empty() {
|
||||
|
@ -380,7 +383,7 @@ fn impl_call_setter(cls: &syn::Type, spec: &FnSpec) -> syn::Result<TokenStream>
|
|||
}
|
||||
|
||||
// Used here for PropertyType::Function, used in pyclass for descriptors.
|
||||
pub fn impl_py_setter_def(cls: &syn::Type, property_type: PropertyType) -> Result<TokenStream> {
|
||||
pub fn impl_py_setter_def(cls: &syn::Type, property_type: PropertyType<'_>) -> Result<TokenStream> {
|
||||
let python_name = property_type.null_terminated_python_name()?;
|
||||
let deprecations = property_type.deprecations();
|
||||
let doc = property_type.doc();
|
||||
|
@ -443,7 +446,7 @@ pub fn impl_py_setter_def(cls: &syn::Type, property_type: PropertyType) -> Resul
|
|||
})
|
||||
}
|
||||
|
||||
fn impl_call_getter(cls: &syn::Type, spec: &FnSpec) -> syn::Result<TokenStream> {
|
||||
fn impl_call_getter(cls: &syn::Type, spec: &FnSpec<'_>) -> syn::Result<TokenStream> {
|
||||
let (py_arg, args) = split_off_python_arg(&spec.args);
|
||||
ensure_spanned!(
|
||||
args.is_empty(),
|
||||
|
@ -461,7 +464,7 @@ fn impl_call_getter(cls: &syn::Type, spec: &FnSpec) -> syn::Result<TokenStream>
|
|||
}
|
||||
|
||||
// Used here for PropertyType::Function, used in pyclass for descriptors.
|
||||
pub fn impl_py_getter_def(cls: &syn::Type, property_type: PropertyType) -> Result<TokenStream> {
|
||||
pub fn impl_py_getter_def(cls: &syn::Type, property_type: PropertyType<'_>) -> Result<TokenStream> {
|
||||
let python_name = property_type.null_terminated_python_name()?;
|
||||
let deprecations = property_type.deprecations();
|
||||
let doc = property_type.doc();
|
||||
|
@ -518,7 +521,7 @@ pub fn impl_py_getter_def(cls: &syn::Type, property_type: PropertyType) -> Resul
|
|||
}
|
||||
|
||||
/// Split an argument of pyo3::Python from the front of the arg list, if present
|
||||
fn split_off_python_arg<'a>(args: &'a [FnArg<'a>]) -> (Option<&FnArg>, &[FnArg]) {
|
||||
fn split_off_python_arg<'a>(args: &'a [FnArg<'a>]) -> (Option<&FnArg<'_>>, &[FnArg<'_>]) {
|
||||
match args {
|
||||
[py, args @ ..] if utils::is_python(py.ty) => (Some(py), args),
|
||||
args => (None, args),
|
||||
|
@ -563,7 +566,7 @@ impl PropertyType<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
fn doc(&self) -> Cow<PythonDoc> {
|
||||
fn doc(&self) -> Cow<'_, PythonDoc> {
|
||||
match self {
|
||||
PropertyType::Descriptor { field, .. } => {
|
||||
Cow::Owned(utils::get_doc(&field.attrs, None))
|
||||
|
@ -712,7 +715,7 @@ impl Ty {
|
|||
cls: &syn::Type,
|
||||
py: &syn::Ident,
|
||||
ident: &syn::Ident,
|
||||
arg: &FnArg,
|
||||
arg: &FnArg<'_>,
|
||||
extract_error_mode: ExtractErrorMode,
|
||||
) -> TokenStream {
|
||||
match self {
|
||||
|
@ -911,7 +914,7 @@ impl SlotDef {
|
|||
fn generate_type_slot(
|
||||
&self,
|
||||
cls: &syn::Type,
|
||||
spec: &FnSpec,
|
||||
spec: &FnSpec<'_>,
|
||||
method_name: &str,
|
||||
) -> Result<TokenStream> {
|
||||
let SlotDef {
|
||||
|
@ -969,7 +972,7 @@ fn generate_method_arguments(arguments: &[Ty]) -> impl Iterator<Item = TokenStre
|
|||
|
||||
fn generate_method_body(
|
||||
cls: &syn::Type,
|
||||
spec: &FnSpec,
|
||||
spec: &FnSpec<'_>,
|
||||
py: &syn::Ident,
|
||||
arguments: &[Ty],
|
||||
extract_error_mode: ExtractErrorMode,
|
||||
|
@ -1022,7 +1025,7 @@ impl SlotFragmentDef {
|
|||
self
|
||||
}
|
||||
|
||||
fn generate_pyproto_fragment(&self, cls: &syn::Type, spec: &FnSpec) -> Result<TokenStream> {
|
||||
fn generate_pyproto_fragment(&self, cls: &syn::Type, spec: &FnSpec<'_>) -> Result<TokenStream> {
|
||||
let SlotFragmentDef {
|
||||
fragment,
|
||||
arguments,
|
||||
|
@ -1111,7 +1114,7 @@ const __RPOW__: SlotFragmentDef = SlotFragmentDef::new("__rpow__", &[Ty::Object,
|
|||
fn extract_proto_arguments(
|
||||
cls: &syn::Type,
|
||||
py: &syn::Ident,
|
||||
method_args: &[FnArg],
|
||||
method_args: &[FnArg<'_>],
|
||||
proto_args: &[Ty],
|
||||
extract_error_mode: ExtractErrorMode,
|
||||
) -> Result<(Vec<Ident>, usize, TokenStream)> {
|
||||
|
|
|
@ -133,7 +133,7 @@ struct DocArgs {
|
|||
}
|
||||
|
||||
impl syn::parse::Parse for DocArgs {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
fn parse(input: syn::parse::ParseStream<'_>) -> syn::Result<Self> {
|
||||
let this = Self {
|
||||
_eq_token: input.parse()?,
|
||||
token_stream: input.parse()?,
|
||||
|
|
|
@ -8,7 +8,7 @@ pub struct WrapPyFunctionArgs {
|
|||
}
|
||||
|
||||
impl Parse for WrapPyFunctionArgs {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
fn parse(input: syn::parse::ParseStream<'_>) -> syn::Result<Self> {
|
||||
let function = input.parse()?;
|
||||
let comma_and_arg = if !input.is_empty() {
|
||||
Some((input.parse()?, input.parse()?))
|
||||
|
|
|
@ -38,7 +38,7 @@ impl BytesExtractor {
|
|||
}
|
||||
|
||||
#[pymodule]
|
||||
pub fn buf_and_str(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
pub fn buf_and_str(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_class::<BytesExtractor>()?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ use pyo3::types::{
|
|||
};
|
||||
|
||||
#[pyfunction]
|
||||
fn make_date(py: Python, year: i32, month: u8, day: u8) -> PyResult<&PyDate> {
|
||||
fn make_date(py: Python<'_>, year: i32, month: u8, day: u8) -> PyResult<&PyDate> {
|
||||
PyDate::new(py, year, month, day)
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,7 @@ fn get_date_tuple<'p>(py: Python<'p>, d: &PyDate) -> &'p PyTuple {
|
|||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn date_from_timestamp(py: Python, timestamp: i64) -> PyResult<&PyDate> {
|
||||
fn date_from_timestamp(py: Python<'_>, timestamp: i64) -> PyResult<&PyDate> {
|
||||
PyDate::from_timestamp(py, timestamp)
|
||||
}
|
||||
|
||||
|
@ -94,7 +94,7 @@ fn get_time_tuple_fold<'p>(py: Python<'p>, dt: &PyTime) -> &'p PyTuple {
|
|||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn make_delta(py: Python, days: i32, seconds: i32, microseconds: i32) -> PyResult<&PyDelta> {
|
||||
fn make_delta(py: Python<'_>, days: i32, seconds: i32, microseconds: i32) -> PyResult<&PyDelta> {
|
||||
PyDelta::new(py, days, seconds, microseconds, true)
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ pub mod pyfunctions;
|
|||
pub mod subclassing;
|
||||
|
||||
#[pymodule]
|
||||
fn pyo3_pytests(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn pyo3_pytests(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
#[cfg(not(Py_LIMITED_API))]
|
||||
m.add_wrapped(wrap_pymodule!(buf_and_str::buf_and_str))?;
|
||||
#[cfg(not(Py_LIMITED_API))]
|
||||
|
|
|
@ -13,7 +13,7 @@ impl ObjStore {
|
|||
ObjStore::default()
|
||||
}
|
||||
|
||||
fn push(&mut self, py: Python, obj: &PyAny) {
|
||||
fn push(&mut self, py: Python<'_>, obj: &PyAny) {
|
||||
self.obj.push(obj.to_object(py));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ unsafe impl<T> Send for PyBuffer<T> {}
|
|||
unsafe impl<T> Sync for PyBuffer<T> {}
|
||||
|
||||
impl<T> Debug for PyBuffer<T> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("PyBuffer")
|
||||
.field("buf", &self.0.buf)
|
||||
.field("obj", &self.0.obj)
|
||||
|
@ -459,7 +459,7 @@ impl<T: Element> PyBuffer<T> {
|
|||
/// To check whether the buffer format is compatible before calling this method,
|
||||
/// you can use `<T as buffer::Element>::is_compatible_format(buf.format())`.
|
||||
/// Alternatively, `match buffer::ElementType::from_format(buf.format())`.
|
||||
pub fn copy_to_slice(&self, py: Python, target: &mut [T]) -> PyResult<()> {
|
||||
pub fn copy_to_slice(&self, py: Python<'_>, target: &mut [T]) -> PyResult<()> {
|
||||
self.copy_to_slice_impl(py, target, b'C')
|
||||
}
|
||||
|
||||
|
@ -472,11 +472,11 @@ impl<T: Element> PyBuffer<T> {
|
|||
/// To check whether the buffer format is compatible before calling this method,
|
||||
/// you can use `<T as buffer::Element>::is_compatible_format(buf.format())`.
|
||||
/// Alternatively, `match buffer::ElementType::from_format(buf.format())`.
|
||||
pub fn copy_to_fortran_slice(&self, py: Python, target: &mut [T]) -> PyResult<()> {
|
||||
pub fn copy_to_fortran_slice(&self, py: Python<'_>, target: &mut [T]) -> PyResult<()> {
|
||||
self.copy_to_slice_impl(py, target, b'F')
|
||||
}
|
||||
|
||||
fn copy_to_slice_impl(&self, py: Python, target: &mut [T], fort: u8) -> PyResult<()> {
|
||||
fn copy_to_slice_impl(&self, py: Python<'_>, target: &mut [T], fort: u8) -> PyResult<()> {
|
||||
if mem::size_of_val(target) != self.len_bytes() {
|
||||
return Err(PyBufferError::new_err(format!(
|
||||
"slice to copy to (of length {}) does not match buffer length of {}",
|
||||
|
@ -506,7 +506,7 @@ impl<T: Element> PyBuffer<T> {
|
|||
/// If the buffer is multi-dimensional, the elements are written in C-style order.
|
||||
///
|
||||
/// Fails if the buffer format is not compatible with type `T`.
|
||||
pub fn to_vec(&self, py: Python) -> PyResult<Vec<T>> {
|
||||
pub fn to_vec(&self, py: Python<'_>) -> PyResult<Vec<T>> {
|
||||
self.to_vec_impl(py, b'C')
|
||||
}
|
||||
|
||||
|
@ -514,11 +514,11 @@ impl<T: Element> PyBuffer<T> {
|
|||
/// If the buffer is multi-dimensional, the elements are written in Fortran-style order.
|
||||
///
|
||||
/// Fails if the buffer format is not compatible with type `T`.
|
||||
pub fn to_fortran_vec(&self, py: Python) -> PyResult<Vec<T>> {
|
||||
pub fn to_fortran_vec(&self, py: Python<'_>) -> PyResult<Vec<T>> {
|
||||
self.to_vec_impl(py, b'F')
|
||||
}
|
||||
|
||||
fn to_vec_impl(&self, py: Python, fort: u8) -> PyResult<Vec<T>> {
|
||||
fn to_vec_impl(&self, py: Python<'_>, fort: u8) -> PyResult<Vec<T>> {
|
||||
let item_count = self.item_count();
|
||||
let mut vec: Vec<T> = Vec::with_capacity(item_count);
|
||||
unsafe {
|
||||
|
@ -554,7 +554,7 @@ impl<T: Element> PyBuffer<T> {
|
|||
/// To check whether the buffer format is compatible before calling this method,
|
||||
/// use `<T as buffer::Element>::is_compatible_format(buf.format())`.
|
||||
/// Alternatively, `match buffer::ElementType::from_format(buf.format())`.
|
||||
pub fn copy_from_slice(&self, py: Python, source: &[T]) -> PyResult<()> {
|
||||
pub fn copy_from_slice(&self, py: Python<'_>, source: &[T]) -> PyResult<()> {
|
||||
self.copy_from_slice_impl(py, source, b'C')
|
||||
}
|
||||
|
||||
|
@ -568,11 +568,11 @@ impl<T: Element> PyBuffer<T> {
|
|||
/// To check whether the buffer format is compatible before calling this method,
|
||||
/// use `<T as buffer::Element>::is_compatible_format(buf.format())`.
|
||||
/// Alternatively, `match buffer::ElementType::from_format(buf.format())`.
|
||||
pub fn copy_from_fortran_slice(&self, py: Python, source: &[T]) -> PyResult<()> {
|
||||
pub fn copy_from_fortran_slice(&self, py: Python<'_>, source: &[T]) -> PyResult<()> {
|
||||
self.copy_from_slice_impl(py, source, b'F')
|
||||
}
|
||||
|
||||
fn copy_from_slice_impl(&self, py: Python, source: &[T], fort: u8) -> PyResult<()> {
|
||||
fn copy_from_slice_impl(&self, py: Python<'_>, source: &[T], fort: u8) -> PyResult<()> {
|
||||
if self.readonly() {
|
||||
return Err(PyBufferError::new_err("cannot write to read-only buffer"));
|
||||
} else if mem::size_of_val(source) != self.len_bytes() {
|
||||
|
@ -607,7 +607,7 @@ impl<T: Element> PyBuffer<T> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn release(self, _py: Python) {
|
||||
pub fn release(self, _py: Python<'_>) {
|
||||
// First move self into a ManuallyDrop, so that PyBuffer::drop will
|
||||
// never be called. (It would acquire the GIL and call PyBuffer_Release
|
||||
// again.)
|
||||
|
|
|
@ -37,7 +37,7 @@ impl PyCallbackOutput for () {
|
|||
|
||||
/// Convert the result of callback function into the appropriate return value.
|
||||
pub trait IntoPyCallbackOutput<Target> {
|
||||
fn convert(self, py: Python) -> PyResult<Target>;
|
||||
fn convert(self, py: Python<'_>) -> PyResult<Target>;
|
||||
}
|
||||
|
||||
impl<T, E, U> IntoPyCallbackOutput<U> for Result<T, E>
|
||||
|
@ -46,7 +46,7 @@ where
|
|||
E: Into<PyErr>,
|
||||
{
|
||||
#[inline]
|
||||
fn convert(self, py: Python) -> PyResult<U> {
|
||||
fn convert(self, py: Python<'_>) -> PyResult<U> {
|
||||
self.map_err(Into::into).and_then(|t| t.convert(py))
|
||||
}
|
||||
}
|
||||
|
@ -56,42 +56,42 @@ where
|
|||
T: IntoPy<PyObject>,
|
||||
{
|
||||
#[inline]
|
||||
fn convert(self, py: Python) -> PyResult<*mut ffi::PyObject> {
|
||||
fn convert(self, py: Python<'_>) -> PyResult<*mut ffi::PyObject> {
|
||||
Ok(self.into_py(py).into_ptr())
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPyCallbackOutput<Self> for *mut ffi::PyObject {
|
||||
#[inline]
|
||||
fn convert(self, _: Python) -> PyResult<Self> {
|
||||
fn convert(self, _: Python<'_>) -> PyResult<Self> {
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPyCallbackOutput<std::os::raw::c_int> for () {
|
||||
#[inline]
|
||||
fn convert(self, _: Python) -> PyResult<std::os::raw::c_int> {
|
||||
fn convert(self, _: Python<'_>) -> PyResult<std::os::raw::c_int> {
|
||||
Ok(0)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPyCallbackOutput<std::os::raw::c_int> for bool {
|
||||
#[inline]
|
||||
fn convert(self, _: Python) -> PyResult<std::os::raw::c_int> {
|
||||
fn convert(self, _: Python<'_>) -> PyResult<std::os::raw::c_int> {
|
||||
Ok(self as c_int)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPyCallbackOutput<()> for () {
|
||||
#[inline]
|
||||
fn convert(self, _: Python) -> PyResult<()> {
|
||||
fn convert(self, _: Python<'_>) -> PyResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPyCallbackOutput<ffi::Py_ssize_t> for usize {
|
||||
#[inline]
|
||||
fn convert(self, _py: Python) -> PyResult<ffi::Py_ssize_t> {
|
||||
fn convert(self, _py: Python<'_>) -> PyResult<ffi::Py_ssize_t> {
|
||||
if self <= (isize::MAX as usize) {
|
||||
Ok(self as isize)
|
||||
} else {
|
||||
|
@ -104,14 +104,14 @@ impl IntoPyCallbackOutput<ffi::Py_ssize_t> for usize {
|
|||
|
||||
impl IntoPyCallbackOutput<bool> for bool {
|
||||
#[inline]
|
||||
fn convert(self, _: Python) -> PyResult<bool> {
|
||||
fn convert(self, _: Python<'_>) -> PyResult<bool> {
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPyCallbackOutput<usize> for usize {
|
||||
#[inline]
|
||||
fn convert(self, _: Python) -> PyResult<usize> {
|
||||
fn convert(self, _: Python<'_>) -> PyResult<usize> {
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
@ -121,7 +121,7 @@ where
|
|||
T: IntoPy<PyObject>,
|
||||
{
|
||||
#[inline]
|
||||
fn convert(self, py: Python) -> PyResult<PyObject> {
|
||||
fn convert(self, py: Python<'_>) -> PyResult<PyObject> {
|
||||
Ok(self.into_py(py))
|
||||
}
|
||||
}
|
||||
|
@ -155,7 +155,7 @@ pub struct HashCallbackOutput(Py_hash_t);
|
|||
|
||||
impl IntoPyCallbackOutput<Py_hash_t> for HashCallbackOutput {
|
||||
#[inline]
|
||||
fn convert(self, _py: Python) -> PyResult<Py_hash_t> {
|
||||
fn convert(self, _py: Python<'_>) -> PyResult<Py_hash_t> {
|
||||
let hash = self.0;
|
||||
if hash == -1 {
|
||||
Ok(-2)
|
||||
|
@ -170,14 +170,14 @@ where
|
|||
T: WrappingCastTo<Py_hash_t>,
|
||||
{
|
||||
#[inline]
|
||||
fn convert(self, _py: Python) -> PyResult<HashCallbackOutput> {
|
||||
fn convert(self, _py: Python<'_>) -> PyResult<HashCallbackOutput> {
|
||||
Ok(HashCallbackOutput(self.wrapping_cast()))
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[inline]
|
||||
pub fn convert<T, U>(py: Python, value: T) -> PyResult<U>
|
||||
pub fn convert<T, U>(py: Python<'_>, value: T) -> PyResult<U>
|
||||
where
|
||||
T: IntoPyCallbackOutput<U>,
|
||||
{
|
||||
|
@ -237,7 +237,7 @@ macro_rules! callback_body {
|
|||
#[inline]
|
||||
pub unsafe fn handle_panic<F, R>(body: F) -> R
|
||||
where
|
||||
F: FnOnce(Python) -> PyResult<R> + UnwindSafe,
|
||||
F: for<'py> FnOnce(Python<'py>) -> PyResult<R> + UnwindSafe,
|
||||
R: PyCallbackOutput,
|
||||
{
|
||||
let pool = GILPool::new();
|
||||
|
@ -250,7 +250,7 @@ where
|
|||
#[doc(hidden)]
|
||||
#[inline]
|
||||
pub fn panic_result_into_callback_output<R>(
|
||||
py: Python,
|
||||
py: Python<'_>,
|
||||
panic_result: Result<PyResult<R>, Box<dyn Any + Send + 'static>>,
|
||||
) -> R
|
||||
where
|
||||
|
|
|
@ -18,11 +18,15 @@ use std::os::raw::c_int;
|
|||
pub trait PyBufferProtocol<'p>: PyClass {
|
||||
// No default implementations so that implementors of this trait provide both methods.
|
||||
|
||||
fn bf_getbuffer(slf: PyRefMut<Self>, view: *mut ffi::Py_buffer, flags: c_int) -> Self::Result
|
||||
fn bf_getbuffer(
|
||||
slf: PyRefMut<'_, Self>,
|
||||
view: *mut ffi::Py_buffer,
|
||||
flags: c_int,
|
||||
) -> Self::Result
|
||||
where
|
||||
Self: PyBufferGetBufferProtocol<'p>;
|
||||
|
||||
fn bf_releasebuffer(slf: PyRefMut<Self>, view: *mut ffi::Py_buffer) -> Self::Result
|
||||
fn bf_releasebuffer(slf: PyRefMut<'_, Self>, view: *mut ffi::Py_buffer) -> Self::Result
|
||||
where
|
||||
Self: PyBufferReleaseBufferProtocol<'p>;
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ pub use crate::impl_::pymethods::{PyTraverseError, PyVisit};
|
|||
/// GC support
|
||||
#[deprecated(since = "0.16.0", note = "prefer `#[pymethods]` to `#[pyproto]`")]
|
||||
pub trait PyGCProtocol<'p>: PyClass {
|
||||
fn __traverse__(&'p self, visit: PyVisit) -> Result<(), PyTraverseError>;
|
||||
fn __traverse__(&'p self, visit: PyVisit<'_>) -> Result<(), PyTraverseError>;
|
||||
fn __clear__(&'p mut self);
|
||||
}
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@ use crate::{PyClass, PyObject};
|
|||
/// the integers 1 to 5, before raising `StopIteration("Ended")`.
|
||||
///
|
||||
/// ```rust
|
||||
/// # #![allow(deprecated)]
|
||||
/// # #![allow(deprecated, elided_lifetimes_in_paths)]
|
||||
/// use pyo3::class::iter::IterNextOutput;
|
||||
/// use pyo3::prelude::*;
|
||||
/// use pyo3::PyIterProtocol;
|
||||
|
|
|
@ -71,7 +71,7 @@ where
|
|||
/// Conversion trait that allows various objects to be converted into `PyObject`.
|
||||
pub trait ToPyObject {
|
||||
/// Converts self into a Python object.
|
||||
fn to_object(&self, py: Python) -> PyObject;
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject;
|
||||
}
|
||||
|
||||
/// This trait has two implementations: The slow one is implemented for
|
||||
|
@ -84,7 +84,7 @@ pub trait ToBorrowedObject: ToPyObject {
|
|||
///
|
||||
/// May be more efficient than `to_object` because it does not need
|
||||
/// to touch any reference counts when the input object already is a Python object.
|
||||
fn with_borrowed_ptr<F, R>(&self, py: Python, f: F) -> R
|
||||
fn with_borrowed_ptr<F, R>(&self, py: Python<'_>, f: F) -> R
|
||||
where
|
||||
F: FnOnce(*mut ffi::PyObject) -> R,
|
||||
{
|
||||
|
@ -134,7 +134,7 @@ impl<T> ToBorrowedObject for T where T: ToPyObject {}
|
|||
/// }
|
||||
///
|
||||
/// impl IntoPy<PyObject> for Number {
|
||||
/// fn into_py(self, py: Python) -> PyObject {
|
||||
/// fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
/// // delegates to i32's IntoPy implementation.
|
||||
/// self.value.into_py(py)
|
||||
/// }
|
||||
|
@ -156,7 +156,7 @@ impl<T> ToBorrowedObject for T where T: ToPyObject {}
|
|||
/// }
|
||||
///
|
||||
/// impl IntoPy<PyObject> for Value {
|
||||
/// fn into_py(self, py: Python) -> PyObject {
|
||||
/// fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
/// match self {
|
||||
/// Self::Integer(val) => val.into_py(py),
|
||||
/// Self::String(val) => val.into_py(py),
|
||||
|
@ -181,7 +181,7 @@ impl<T> ToBorrowedObject for T where T: ToPyObject {}
|
|||
#[cfg_attr(docsrs, doc(alias = "IntoPyCallbackOutput"))]
|
||||
pub trait IntoPy<T>: Sized {
|
||||
/// Performs the conversion.
|
||||
fn into_py(self, py: Python) -> T;
|
||||
fn into_py(self, py: Python<'_>) -> T;
|
||||
}
|
||||
|
||||
/// `FromPyObject` is implemented by various types that can be extracted from
|
||||
|
@ -218,7 +218,7 @@ pub trait FromPyObject<'source>: Sized {
|
|||
/// `T: ToPyObject` is expected.
|
||||
impl<T: ?Sized + ToPyObject> ToPyObject for &'_ T {
|
||||
#[inline]
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
<T as ToPyObject>::to_object(*self, py)
|
||||
}
|
||||
}
|
||||
|
@ -229,7 +229,7 @@ impl<T> ToPyObject for Option<T>
|
|||
where
|
||||
T: ToPyObject,
|
||||
{
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
self.as_ref()
|
||||
.map_or_else(|| py.None(), |val| val.to_object(py))
|
||||
}
|
||||
|
@ -239,20 +239,20 @@ impl<T> IntoPy<PyObject> for Option<T>
|
|||
where
|
||||
T: IntoPy<PyObject>,
|
||||
{
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
self.map_or_else(|| py.None(), |val| val.into_py(py))
|
||||
}
|
||||
}
|
||||
|
||||
/// `()` is converted to Python `None`.
|
||||
impl ToPyObject for () {
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
py.None()
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPy<PyObject> for () {
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
py.None()
|
||||
}
|
||||
}
|
||||
|
@ -262,7 +262,7 @@ where
|
|||
T: AsPyPointer,
|
||||
{
|
||||
#[inline]
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
unsafe { PyObject::from_borrowed_ptr(py, self.as_ptr()) }
|
||||
}
|
||||
}
|
||||
|
@ -343,10 +343,10 @@ pub trait PyTryFrom<'v>: Sized + PyNativeType {
|
|||
/// This trait is similar to `std::convert::TryInto`
|
||||
pub trait PyTryInto<T>: Sized {
|
||||
/// Cast from PyObject to a concrete Python object type.
|
||||
fn try_into(&self) -> Result<&T, PyDowncastError>;
|
||||
fn try_into(&self) -> Result<&T, PyDowncastError<'_>>;
|
||||
|
||||
/// Cast from PyObject to a concrete Python object type. With exact type check.
|
||||
fn try_into_exact(&self) -> Result<&T, PyDowncastError>;
|
||||
fn try_into_exact(&self) -> Result<&T, PyDowncastError<'_>>;
|
||||
}
|
||||
|
||||
// TryFrom implies TryInto
|
||||
|
@ -354,10 +354,10 @@ impl<U> PyTryInto<U> for PyAny
|
|||
where
|
||||
U: for<'v> PyTryFrom<'v>,
|
||||
{
|
||||
fn try_into(&self) -> Result<&U, PyDowncastError> {
|
||||
U::try_from(self)
|
||||
fn try_into(&self) -> Result<&U, PyDowncastError<'_>> {
|
||||
<U as PyTryFrom<'_>>::try_from(self)
|
||||
}
|
||||
fn try_into_exact(&self) -> Result<&U, PyDowncastError> {
|
||||
fn try_into_exact(&self) -> Result<&U, PyDowncastError<'_>> {
|
||||
U::try_from_exact(self)
|
||||
}
|
||||
}
|
||||
|
@ -426,7 +426,7 @@ where
|
|||
|
||||
/// Converts `()` to an empty Python tuple.
|
||||
impl IntoPy<Py<PyTuple>> for () {
|
||||
fn into_py(self, py: Python) -> Py<PyTuple> {
|
||||
fn into_py(self, py: Python<'_>) -> Py<PyTuple> {
|
||||
PyTuple::empty(py).into()
|
||||
}
|
||||
}
|
||||
|
@ -534,11 +534,11 @@ mod tests {
|
|||
let list: &PyAny = vec![3, 6, 5, 4, 7].to_object(py).into_ref(py);
|
||||
let dict: &PyAny = vec![("reverse", true)].into_py_dict(py).as_ref();
|
||||
|
||||
assert!(PyList::try_from(list).is_ok());
|
||||
assert!(PyDict::try_from(dict).is_ok());
|
||||
assert!(<PyList as PyTryFrom<'_>>::try_from(list).is_ok());
|
||||
assert!(<PyDict as PyTryFrom<'_>>::try_from(dict).is_ok());
|
||||
|
||||
assert!(PyAny::try_from(list).is_ok());
|
||||
assert!(PyAny::try_from(dict).is_ok());
|
||||
assert!(<PyAny as PyTryFrom<'_>>::try_from(list).is_ok());
|
||||
assert!(<PyAny as PyTryFrom<'_>>::try_from(dict).is_ok());
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ mod min_const_generics {
|
|||
where
|
||||
T: ToPyObject,
|
||||
{
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
self.as_ref().to_object(py)
|
||||
}
|
||||
}
|
||||
|
@ -149,7 +149,7 @@ mod array_impls {
|
|||
where
|
||||
T: ToPyObject
|
||||
{
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
self.as_ref().to_object(py)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ where
|
|||
V: ToPyObject,
|
||||
H: hash::BuildHasher,
|
||||
{
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
IntoPyDict::into_py_dict(self, py).into()
|
||||
}
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ where
|
|||
V: IntoPy<PyObject>,
|
||||
H: hash::BuildHasher,
|
||||
{
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
let iter = self
|
||||
.into_iter()
|
||||
.map(|(k, v)| (k.into_py(py), v.into_py(py)));
|
||||
|
@ -72,7 +72,7 @@ impl<T> ToPyObject for hashbrown::HashSet<T>
|
|||
where
|
||||
T: hash::Hash + Eq + ToPyObject,
|
||||
{
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
let set = PySet::new::<T>(py, &[]).expect("Failed to construct empty set");
|
||||
{
|
||||
for val in self {
|
||||
|
@ -88,7 +88,7 @@ where
|
|||
K: IntoPy<PyObject> + Eq + hash::Hash,
|
||||
S: hash::BuildHasher + Default,
|
||||
{
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
let set = PySet::empty(py).expect("Failed to construct empty set");
|
||||
{
|
||||
for val in self {
|
||||
|
|
|
@ -76,7 +76,7 @@
|
|||
//! }
|
||||
//!
|
||||
//! #[pymodule]
|
||||
//! fn my_module(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
//! fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
//! m.add_function(wrap_pyfunction!(calculate_statistics, m)?)?;
|
||||
//! Ok(())
|
||||
//! }
|
||||
|
@ -102,7 +102,7 @@ where
|
|||
V: ToPyObject,
|
||||
H: hash::BuildHasher,
|
||||
{
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
IntoPyDict::into_py_dict(self, py).into()
|
||||
}
|
||||
}
|
||||
|
@ -113,7 +113,7 @@ where
|
|||
V: IntoPy<PyObject>,
|
||||
H: hash::BuildHasher,
|
||||
{
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
let iter = self
|
||||
.into_iter()
|
||||
.map(|(k, v)| (k.into_py(py), v.into_py(py)));
|
||||
|
|
|
@ -40,7 +40,7 @@
|
|||
//! }
|
||||
//!
|
||||
//! #[pymodule]
|
||||
//! fn my_module(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
//! fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
//! m.add_function(wrap_pyfunction!(add_one, m)?)?;
|
||||
//! Ok(())
|
||||
//! }
|
||||
|
@ -82,7 +82,7 @@ macro_rules! bigint_conversion {
|
|||
($rust_ty: ty, $is_signed: expr, $to_bytes: path, $from_bytes: path) => {
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "num-bigint")))]
|
||||
impl ToPyObject for $rust_ty {
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
unsafe {
|
||||
let bytes = $to_bytes(self);
|
||||
let obj = ffi::_PyLong_FromByteArray(
|
||||
|
@ -98,7 +98,7 @@ macro_rules! bigint_conversion {
|
|||
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "num-bigint")))]
|
||||
impl IntoPy<PyObject> for $rust_ty {
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
self.to_object(py)
|
||||
}
|
||||
}
|
||||
|
@ -146,7 +146,7 @@ mod tests {
|
|||
use crate::types::{PyDict, PyModule};
|
||||
use indoc::indoc;
|
||||
|
||||
fn python_fib(py: Python) -> &PyModule {
|
||||
fn python_fib(py: Python<'_>) -> &PyModule {
|
||||
let fib_code = indoc!(
|
||||
r#"
|
||||
def fib(n):
|
||||
|
@ -224,7 +224,7 @@ mod tests {
|
|||
})
|
||||
}
|
||||
|
||||
fn python_index_class(py: Python) -> &PyModule {
|
||||
fn python_index_class(py: Python<'_>) -> &PyModule {
|
||||
let index_code = indoc!(
|
||||
r#"
|
||||
class C:
|
||||
|
|
|
@ -49,7 +49,7 @@
|
|||
//! }
|
||||
//!
|
||||
//! #[pymodule]
|
||||
//! fn my_module(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
//! fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
//! m.add_function(wrap_pyfunction!(get_eigenvalues, m)?)?;
|
||||
//! Ok(())
|
||||
//! }
|
||||
|
@ -107,7 +107,7 @@ use std::os::raw::c_double;
|
|||
|
||||
impl PyComplex {
|
||||
/// Creates a new Python `PyComplex` object from `num_complex`'s [`Complex`].
|
||||
pub fn from_complex<F: Into<c_double>>(py: Python, complex: Complex<F>) -> &PyComplex {
|
||||
pub fn from_complex<F: Into<c_double>>(py: Python<'_>, complex: Complex<F>) -> &PyComplex {
|
||||
unsafe {
|
||||
let ptr = ffi::PyComplex_FromDoubles(complex.re.into(), complex.im.into());
|
||||
py.from_owned_ptr(ptr)
|
||||
|
@ -120,14 +120,14 @@ macro_rules! complex_conversion {
|
|||
#[cfg_attr(docsrs, doc(cfg(feature = "num-complex")))]
|
||||
impl ToPyObject for Complex<$float> {
|
||||
#[inline]
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
crate::IntoPy::<PyObject>::into_py(self.to_owned(), py)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "num-complex")))]
|
||||
impl crate::IntoPy<PyObject> for Complex<$float> {
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
unsafe {
|
||||
let raw_obj =
|
||||
ffi::PyComplex_FromDoubles(self.re as c_double, self.im as c_double);
|
||||
|
|
|
@ -11,7 +11,7 @@ use std::ffi::{OsStr, OsString};
|
|||
use std::os::raw::c_char;
|
||||
|
||||
impl ToPyObject for OsStr {
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
// If the string is UTF-8, take the quick and easy shortcut
|
||||
if let Some(valid_utf8_path) = self.to_str() {
|
||||
return valid_utf8_path.to_object(py);
|
||||
|
@ -112,40 +112,40 @@ impl FromPyObject<'_> for OsString {
|
|||
|
||||
impl IntoPy<PyObject> for &'_ OsStr {
|
||||
#[inline]
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
self.to_object(py)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPyObject for Cow<'_, OsStr> {
|
||||
#[inline]
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
(self as &OsStr).to_object(py)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPy<PyObject> for Cow<'_, OsStr> {
|
||||
#[inline]
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
self.to_object(py)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPyObject for OsString {
|
||||
#[inline]
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
(self as &OsStr).to_object(py)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPy<PyObject> for OsString {
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
self.to_object(py)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoPy<PyObject> for &'a OsString {
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
self.to_object(py)
|
||||
}
|
||||
}
|
||||
|
@ -182,7 +182,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_topyobject_roundtrip() {
|
||||
Python::with_gil(|py| {
|
||||
fn test_roundtrip<T: ToPyObject + AsRef<OsStr> + Debug>(py: Python, obj: T) {
|
||||
fn test_roundtrip<T: ToPyObject + AsRef<OsStr> + Debug>(py: Python<'_>, obj: T) {
|
||||
let pyobject = obj.to_object(py);
|
||||
let pystring: &PyString = pyobject.extract(py).unwrap();
|
||||
assert_eq!(pystring.to_string_lossy(), obj.as_ref().to_string_lossy());
|
||||
|
@ -201,7 +201,7 @@ mod tests {
|
|||
fn test_intopy_roundtrip() {
|
||||
Python::with_gil(|py| {
|
||||
fn test_roundtrip<T: IntoPy<PyObject> + AsRef<OsStr> + Debug + Clone>(
|
||||
py: Python,
|
||||
py: Python<'_>,
|
||||
obj: T,
|
||||
) {
|
||||
let pyobject = obj.clone().into_py(py);
|
||||
|
|
|
@ -5,7 +5,7 @@ use std::ffi::OsString;
|
|||
use std::path::{Path, PathBuf};
|
||||
|
||||
impl ToPyObject for Path {
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
self.as_os_str().to_object(py)
|
||||
}
|
||||
}
|
||||
|
@ -34,40 +34,40 @@ impl FromPyObject<'_> for PathBuf {
|
|||
|
||||
impl<'a> IntoPy<PyObject> for &'a Path {
|
||||
#[inline]
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
self.as_os_str().to_object(py)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ToPyObject for Cow<'a, Path> {
|
||||
#[inline]
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
self.as_os_str().to_object(py)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoPy<PyObject> for Cow<'a, Path> {
|
||||
#[inline]
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
self.to_object(py)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPyObject for PathBuf {
|
||||
#[inline]
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
self.as_os_str().to_object(py)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPy<PyObject> for PathBuf {
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
self.into_os_string().to_object(py)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoPy<PyObject> for &'a PathBuf {
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
self.as_os_str().to_object(py)
|
||||
}
|
||||
}
|
||||
|
@ -103,7 +103,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_topyobject_roundtrip() {
|
||||
Python::with_gil(|py| {
|
||||
fn test_roundtrip<T: ToPyObject + AsRef<Path> + Debug>(py: Python, obj: T) {
|
||||
fn test_roundtrip<T: ToPyObject + AsRef<Path> + Debug>(py: Python<'_>, obj: T) {
|
||||
let pyobject = obj.to_object(py);
|
||||
let pystring: &PyString = pyobject.extract(py).unwrap();
|
||||
assert_eq!(pystring.to_string_lossy(), obj.as_ref().to_string_lossy());
|
||||
|
@ -122,7 +122,7 @@ mod tests {
|
|||
fn test_intopy_roundtrip() {
|
||||
Python::with_gil(|py| {
|
||||
fn test_roundtrip<T: IntoPy<PyObject> + AsRef<Path> + Debug + Clone>(
|
||||
py: Python,
|
||||
py: Python<'_>,
|
||||
obj: T,
|
||||
) {
|
||||
let pyobject = obj.clone().into_py(py);
|
||||
|
|
|
@ -15,12 +15,12 @@ pub(crate) struct PyErrStateNormalized {
|
|||
|
||||
pub(crate) enum PyErrState {
|
||||
LazyTypeAndValue {
|
||||
ptype: fn(Python) -> &PyType,
|
||||
pvalue: Box<dyn FnOnce(Python) -> PyObject + Send + Sync>,
|
||||
ptype: for<'py> fn(Python<'py>) -> &PyType,
|
||||
pvalue: Box<dyn for<'py> FnOnce(Python<'py>) -> PyObject + Send + Sync>,
|
||||
},
|
||||
LazyValue {
|
||||
ptype: Py<PyType>,
|
||||
pvalue: Box<dyn FnOnce(Python) -> PyObject + Send + Sync>,
|
||||
pvalue: Box<dyn for<'py> FnOnce(Python<'py>) -> PyObject + Send + Sync>,
|
||||
},
|
||||
FfiTuple {
|
||||
ptype: PyObject,
|
||||
|
@ -33,28 +33,28 @@ pub(crate) enum PyErrState {
|
|||
/// Helper conversion trait that allows to use custom arguments for lazy exception construction.
|
||||
pub trait PyErrArguments: Send + Sync {
|
||||
/// Arguments for exception
|
||||
fn arguments(self, py: Python) -> PyObject;
|
||||
fn arguments(self, py: Python<'_>) -> PyObject;
|
||||
}
|
||||
|
||||
impl<T> PyErrArguments for T
|
||||
where
|
||||
T: IntoPy<PyObject> + Send + Sync,
|
||||
{
|
||||
fn arguments(self, py: Python) -> PyObject {
|
||||
fn arguments(self, py: Python<'_>) -> PyObject {
|
||||
self.into_py(py)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn boxed_args(
|
||||
args: impl PyErrArguments + 'static,
|
||||
) -> Box<dyn FnOnce(Python) -> PyObject + Send + Sync> {
|
||||
) -> Box<dyn for<'py> FnOnce(Python<'py>) -> PyObject + Send + Sync> {
|
||||
Box::new(|py| args.arguments(py))
|
||||
}
|
||||
|
||||
impl PyErrState {
|
||||
pub(crate) fn into_ffi_tuple(
|
||||
self,
|
||||
py: Python,
|
||||
py: Python<'_>,
|
||||
) -> (*mut ffi::PyObject, *mut ffi::PyObject, *mut ffi::PyObject) {
|
||||
match self {
|
||||
PyErrState::LazyTypeAndValue { ptype, pvalue } => {
|
||||
|
@ -88,7 +88,7 @@ impl PyErrState {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn exceptions_must_derive_from_base_exception(py: Python) -> Self {
|
||||
pub(crate) fn exceptions_must_derive_from_base_exception(py: Python<'_>) -> Self {
|
||||
PyErrState::LazyValue {
|
||||
ptype: PyTypeError::type_object(py).into(),
|
||||
pvalue: boxed_args("exceptions must derive from BaseException"),
|
||||
|
|
|
@ -28,7 +28,7 @@ impl std::convert::From<io::Error> for PyErr {
|
|||
}
|
||||
|
||||
impl PyErrArguments for io::Error {
|
||||
fn arguments(self, py: Python) -> PyObject {
|
||||
fn arguments(self, py: Python<'_>) -> PyObject {
|
||||
self.to_string().into_py(py)
|
||||
}
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ impl<W: 'static + Send + Sync + std::fmt::Debug> std::convert::From<std::io::Int
|
|||
}
|
||||
|
||||
impl<W: Send + Sync + std::fmt::Debug> PyErrArguments for std::io::IntoInnerError<W> {
|
||||
fn arguments(self, py: Python) -> PyObject {
|
||||
fn arguments(self, py: Python<'_>) -> PyObject {
|
||||
self.to_string().into_py(py)
|
||||
}
|
||||
}
|
||||
|
@ -56,7 +56,7 @@ impl std::convert::From<std::convert::Infallible> for PyErr {
|
|||
macro_rules! impl_to_pyerr {
|
||||
($err: ty, $pyexc: ty) => {
|
||||
impl PyErrArguments for $err {
|
||||
fn arguments(self, py: Python) -> PyObject {
|
||||
fn arguments(self, py: Python<'_>) -> PyObject {
|
||||
self.to_string().into_py(py)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -214,7 +214,7 @@ impl PyErr {
|
|||
}
|
||||
|
||||
/// Consumes self to take ownership of the exception value contained in this error.
|
||||
pub fn into_value(self, py: Python) -> Py<PyBaseException> {
|
||||
pub fn into_value(self, py: Python<'_>) -> Py<PyBaseException> {
|
||||
// NB technically this causes one reference count increase and decrease in quick succession
|
||||
// on pvalue, but it's probably not worth optimizing this right now for the additional code
|
||||
// complexity.
|
||||
|
@ -243,7 +243,7 @@ impl PyErr {
|
|||
|
||||
/// Gets whether an error is present in the Python interpreter's global state.
|
||||
#[inline]
|
||||
pub fn occurred(_: Python) -> bool {
|
||||
pub fn occurred(_: Python<'_>) -> bool {
|
||||
unsafe { !ffi::PyErr_Occurred().is_null() }
|
||||
}
|
||||
|
||||
|
@ -256,7 +256,7 @@ impl PyErr {
|
|||
/// Use this function when it is not known if an error should be present. If the error is
|
||||
/// expected to have been set, for example from [`PyErr::occurred`] or by an error return value
|
||||
/// from a C FFI function, use [`PyErr::fetch`].
|
||||
pub fn take(py: Python) -> Option<PyErr> {
|
||||
pub fn take(py: Python<'_>) -> Option<PyErr> {
|
||||
let (ptype, pvalue, ptraceback) = unsafe {
|
||||
let mut ptype: *mut ffi::PyObject = std::ptr::null_mut();
|
||||
let mut pvalue: *mut ffi::PyObject = std::ptr::null_mut();
|
||||
|
@ -327,7 +327,7 @@ impl PyErr {
|
|||
/// [PyErr::occurred] or by an error return value from a C FFI function.
|
||||
#[cfg_attr(all(debug_assertions, track_caller), track_caller)]
|
||||
#[inline]
|
||||
pub fn fetch(py: Python) -> PyErr {
|
||||
pub fn fetch(py: Python<'_>) -> PyErr {
|
||||
const FAILED_TO_FETCH: &str = "attempted to fetch exception but none was set";
|
||||
match PyErr::take(py) {
|
||||
Some(err) => err,
|
||||
|
@ -353,7 +353,7 @@ impl PyErr {
|
|||
///
|
||||
/// This function will panic if `name` or `doc` cannot be converted to [`CString`]s.
|
||||
pub fn new_type(
|
||||
py: Python,
|
||||
py: Python<'_>,
|
||||
name: &str,
|
||||
doc: Option<&str>,
|
||||
base: Option<&PyType>,
|
||||
|
@ -393,14 +393,14 @@ impl PyErr {
|
|||
}
|
||||
|
||||
/// Prints a standard traceback to `sys.stderr`.
|
||||
pub fn print(&self, py: Python) {
|
||||
pub fn print(&self, py: Python<'_>) {
|
||||
self.clone_ref(py).restore(py);
|
||||
unsafe { ffi::PyErr_PrintEx(0) }
|
||||
}
|
||||
|
||||
/// Prints a standard traceback to `sys.stderr`, and sets
|
||||
/// `sys.last_{type,value,traceback}` attributes to this exception's data.
|
||||
pub fn print_and_set_sys_last_vars(&self, py: Python) {
|
||||
pub fn print_and_set_sys_last_vars(&self, py: Python<'_>) {
|
||||
self.clone_ref(py).restore(py);
|
||||
unsafe { ffi::PyErr_PrintEx(1) }
|
||||
}
|
||||
|
@ -409,7 +409,7 @@ impl PyErr {
|
|||
///
|
||||
/// If `exc` is a class object, this also returns `true` when `self` is an instance of a subclass.
|
||||
/// 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
|
||||
pub fn matches<T>(&self, py: Python<'_>, exc: T) -> bool
|
||||
where
|
||||
T: ToBorrowedObject,
|
||||
{
|
||||
|
@ -420,13 +420,13 @@ impl PyErr {
|
|||
|
||||
/// Returns true if the current exception is instance of `T`.
|
||||
#[inline]
|
||||
pub fn is_instance(&self, py: Python, typ: &PyType) -> bool {
|
||||
pub fn is_instance(&self, py: Python<'_>, typ: &PyType) -> bool {
|
||||
unsafe { ffi::PyErr_GivenExceptionMatches(self.type_ptr(py), typ.as_ptr()) != 0 }
|
||||
}
|
||||
|
||||
/// Returns true if the current exception is instance of `T`.
|
||||
#[inline]
|
||||
pub fn is_instance_of<T>(&self, py: Python) -> bool
|
||||
pub fn is_instance_of<T>(&self, py: Python<'_>) -> bool
|
||||
where
|
||||
T: PyTypeObject,
|
||||
{
|
||||
|
@ -436,7 +436,7 @@ impl PyErr {
|
|||
/// Writes the error back to the Python interpreter's global state.
|
||||
/// This is the opposite of `PyErr::fetch()`.
|
||||
#[inline]
|
||||
pub fn restore(self, py: Python) {
|
||||
pub fn restore(self, py: Python<'_>) {
|
||||
let (ptype, pvalue, ptraceback) = self
|
||||
.state
|
||||
.into_inner()
|
||||
|
@ -447,7 +447,7 @@ impl PyErr {
|
|||
|
||||
/// Issues a warning message.
|
||||
/// May return a `PyErr` if warnings-as-errors is enabled.
|
||||
pub fn warn(py: Python, category: &PyAny, message: &str, stacklevel: i32) -> PyResult<()> {
|
||||
pub fn warn(py: Python<'_>, category: &PyAny, message: &str, stacklevel: i32) -> PyResult<()> {
|
||||
let message = CString::new(message)?;
|
||||
unsafe {
|
||||
error_on_minusone(
|
||||
|
@ -478,20 +478,20 @@ impl PyErr {
|
|||
/// });
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn clone_ref(&self, py: Python) -> PyErr {
|
||||
pub fn clone_ref(&self, py: Python<'_>) -> PyErr {
|
||||
PyErr::from_state(PyErrState::Normalized(self.normalized(py).clone()))
|
||||
}
|
||||
|
||||
/// Return the cause (either an exception instance, or None, set by `raise ... from ...`)
|
||||
/// associated with the exception, as accessible from Python through `__cause__`.
|
||||
pub fn cause(&self, py: Python) -> Option<PyErr> {
|
||||
pub fn cause(&self, py: Python<'_>) -> Option<PyErr> {
|
||||
let ptr = unsafe { ffi::PyException_GetCause(self.value(py).as_ptr()) };
|
||||
let obj = unsafe { py.from_owned_ptr_or_opt::<PyAny>(ptr) };
|
||||
obj.map(Self::from_value)
|
||||
}
|
||||
|
||||
/// Set the cause associated with the exception, pass `None` to clear it.
|
||||
pub fn set_cause(&self, py: Python, cause: Option<Self>) {
|
||||
pub fn set_cause(&self, py: Python<'_>, cause: Option<Self>) {
|
||||
unsafe {
|
||||
// PyException_SetCause _steals_ a reference to cause, so must use .into_ptr()
|
||||
ffi::PyException_SetCause(
|
||||
|
@ -509,7 +509,7 @@ impl PyErr {
|
|||
}
|
||||
|
||||
/// Returns borrowed reference to this Err's type
|
||||
fn type_ptr(&self, py: Python) -> *mut ffi::PyObject {
|
||||
fn type_ptr(&self, py: Python<'_>) -> *mut ffi::PyObject {
|
||||
match unsafe { &*self.state.get() } {
|
||||
// In lazy type case, normalize before returning ptype in case the type is not a valid
|
||||
// exception type.
|
||||
|
@ -522,7 +522,7 @@ impl PyErr {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
fn normalized(&self, py: Python) -> &PyErrStateNormalized {
|
||||
fn normalized(&self, py: Python<'_>) -> &PyErrStateNormalized {
|
||||
if let Some(PyErrState::Normalized(n)) = unsafe {
|
||||
// Safety: self.state will never be written again once normalized.
|
||||
&*self.state.get()
|
||||
|
@ -534,7 +534,7 @@ impl PyErr {
|
|||
}
|
||||
|
||||
#[cold]
|
||||
fn make_normalized(&self, py: Python) -> &PyErrStateNormalized {
|
||||
fn make_normalized(&self, py: Python<'_>) -> &PyErrStateNormalized {
|
||||
// This process is safe because:
|
||||
// - Access is guaranteed not to be concurrent thanks to `Python` GIL token
|
||||
// - Write happens only once, and then never will change again.
|
||||
|
@ -611,13 +611,13 @@ impl PyErr {
|
|||
since = "0.16.0",
|
||||
note = "Use err.into_value(py) instead of err.into_instance(py)"
|
||||
)]
|
||||
pub fn into_instance(self, py: Python) -> Py<PyBaseException> {
|
||||
pub fn into_instance(self, py: Python<'_>) -> Py<PyBaseException> {
|
||||
self.into_value(py)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for PyErr {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||
Python::with_gil(|py| {
|
||||
f.debug_struct("PyErr")
|
||||
.field("type", self.get_type(py))
|
||||
|
@ -629,7 +629,7 @@ impl std::fmt::Debug for PyErr {
|
|||
}
|
||||
|
||||
impl std::fmt::Display for PyErr {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
Python::with_gil(|py| {
|
||||
let value = self.value(py);
|
||||
let type_name = value.get_type().name().map_err(|_| std::fmt::Error)?;
|
||||
|
@ -646,26 +646,26 @@ impl std::fmt::Display for PyErr {
|
|||
impl std::error::Error for PyErr {}
|
||||
|
||||
impl IntoPy<PyObject> for PyErr {
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
self.into_value(py).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPyObject for PyErr {
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
self.clone_ref(py).into_py(py)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoPy<PyObject> for &'a PyErr {
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
self.clone_ref(py).into_py(py)
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert `PyDowncastError` to Python `TypeError`.
|
||||
impl<'a> std::convert::From<PyDowncastError<'a>> for PyErr {
|
||||
fn from(err: PyDowncastError) -> PyErr {
|
||||
fn from(err: PyDowncastError<'_>) -> PyErr {
|
||||
exceptions::PyTypeError::new_err(err.to_string())
|
||||
}
|
||||
}
|
||||
|
@ -673,7 +673,7 @@ impl<'a> std::convert::From<PyDowncastError<'a>> for PyErr {
|
|||
impl<'a> std::error::Error for PyDowncastError<'a> {}
|
||||
|
||||
impl<'a> std::fmt::Display for PyDowncastError<'a> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||
write!(
|
||||
f,
|
||||
"'{}' object cannot be converted to '{}'",
|
||||
|
@ -683,7 +683,7 @@ impl<'a> std::fmt::Display for PyDowncastError<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn panic_after_error(_py: Python) -> ! {
|
||||
pub fn panic_after_error(_py: Python<'_>) -> ! {
|
||||
unsafe {
|
||||
ffi::PyErr_Print();
|
||||
}
|
||||
|
@ -692,7 +692,7 @@ pub fn panic_after_error(_py: Python) -> ! {
|
|||
|
||||
/// Returns Ok if the error code is not -1.
|
||||
#[inline]
|
||||
pub fn error_on_minusone(py: Python, result: c_int) -> PyResult<()> {
|
||||
pub fn error_on_minusone(py: Python<'_>, result: c_int) -> PyResult<()> {
|
||||
if result != -1 {
|
||||
Ok(())
|
||||
} else {
|
||||
|
@ -701,7 +701,7 @@ pub fn error_on_minusone(py: Python, result: c_int) -> PyResult<()> {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
fn exceptions_must_derive_from_base_exception(py: Python) -> PyErr {
|
||||
fn exceptions_must_derive_from_base_exception(py: Python<'_>) -> PyErr {
|
||||
PyErr::from_state(PyErrState::exceptions_must_derive_from_base_exception(py))
|
||||
}
|
||||
|
||||
|
|
|
@ -98,7 +98,7 @@ macro_rules! import_exception {
|
|||
);
|
||||
|
||||
impl $name {
|
||||
fn type_object_raw(py: $crate::Python) -> *mut $crate::ffi::PyTypeObject {
|
||||
fn type_object_raw(py: $crate::Python<'_>) -> *mut $crate::ffi::PyTypeObject {
|
||||
use $crate::once_cell::GILOnceCell;
|
||||
use $crate::AsPyPointer;
|
||||
static TYPE_OBJECT: GILOnceCell<$crate::Py<$crate::types::PyType>> =
|
||||
|
@ -151,7 +151,7 @@ macro_rules! import_exception {
|
|||
/// }
|
||||
///
|
||||
/// #[pymodule]
|
||||
/// fn my_module(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
/// fn my_module(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
/// m.add("MyError", py.get_type::<MyError>())?;
|
||||
/// m.add_function(wrap_pyfunction!(raise_myerror, py)?)?;
|
||||
/// Ok(())
|
||||
|
@ -231,7 +231,7 @@ macro_rules! create_exception_type_object {
|
|||
);
|
||||
|
||||
impl $name {
|
||||
fn type_object_raw(py: $crate::Python) -> *mut $crate::ffi::PyTypeObject {
|
||||
fn type_object_raw(py: $crate::Python<'_>) -> *mut $crate::ffi::PyTypeObject {
|
||||
use $crate::once_cell::GILOnceCell;
|
||||
use $crate::AsPyPointer;
|
||||
static TYPE_OBJECT: GILOnceCell<$crate::Py<$crate::types::PyType>> =
|
||||
|
|
12
src/gil.rs
12
src/gil.rs
|
@ -164,7 +164,7 @@ pub struct GILGuard {
|
|||
impl GILGuard {
|
||||
/// Retrieves the marker type that proves that the GIL was acquired.
|
||||
#[inline]
|
||||
pub fn python(&self) -> Python {
|
||||
pub fn python(&self) -> Python<'_> {
|
||||
unsafe { Python::assume_gil_acquired() }
|
||||
}
|
||||
|
||||
|
@ -298,7 +298,7 @@ impl ReferencePool {
|
|||
self.dirty.store(true, atomic::Ordering::Release);
|
||||
}
|
||||
|
||||
fn update_counts(&self, _py: Python) {
|
||||
fn update_counts(&self, _py: Python<'_>) {
|
||||
let prev = self.dirty.swap(false, atomic::Ordering::Acquire);
|
||||
if !prev {
|
||||
return;
|
||||
|
@ -360,7 +360,7 @@ impl GILPool {
|
|||
|
||||
/// Gets the Python token associated with this [`GILPool`].
|
||||
#[inline]
|
||||
pub fn python(&self) -> Python {
|
||||
pub fn python(&self) -> Python<'_> {
|
||||
unsafe { Python::assume_gil_acquired() }
|
||||
}
|
||||
}
|
||||
|
@ -424,7 +424,7 @@ pub unsafe fn register_decref(obj: NonNull<ffi::PyObject>) {
|
|||
///
|
||||
/// # Safety
|
||||
/// The object must be an owned Python reference.
|
||||
pub unsafe fn register_owned(_py: Python, obj: NonNull<ffi::PyObject>) {
|
||||
pub unsafe fn register_owned(_py: Python<'_>, obj: NonNull<ffi::PyObject>) {
|
||||
debug_assert!(gil_is_acquired());
|
||||
// Ignores the error in case this function called from `atexit`.
|
||||
let _ = OWNED_OBJECTS.try_with(|holder| holder.borrow_mut().push(obj));
|
||||
|
@ -483,7 +483,7 @@ impl EnsureGIL {
|
|||
/// Thus this method could be used to get access to a GIL token while the GIL is not held.
|
||||
/// Care should be taken to only use the returned Python in contexts where it is certain the
|
||||
/// GIL continues to be held.
|
||||
pub unsafe fn python(&self) -> Python {
|
||||
pub unsafe fn python(&self) -> Python<'_> {
|
||||
match &self.0 {
|
||||
Some(gil) => gil.python(),
|
||||
None => Python::assume_gil_acquired(),
|
||||
|
@ -497,7 +497,7 @@ mod tests {
|
|||
use crate::{ffi, gil, AsPyPointer, IntoPyPointer, PyObject, Python, ToPyObject};
|
||||
use std::ptr::NonNull;
|
||||
|
||||
fn get_object(py: Python) -> PyObject {
|
||||
fn get_object(py: Python<'_>) -> PyObject {
|
||||
// Convenience function for getting a single unique object, using `new_pool` so as to leave
|
||||
// the original pool state unchanged.
|
||||
let pool = unsafe { py.new_pool() };
|
||||
|
|
|
@ -101,7 +101,7 @@ pub fn from_py_with_with_default<'py, T>(
|
|||
/// single string.)
|
||||
#[doc(hidden)]
|
||||
#[cold]
|
||||
pub fn argument_extraction_error(py: Python, arg_name: &str, error: PyErr) -> PyErr {
|
||||
pub fn argument_extraction_error(py: Python<'_>, arg_name: &str, error: PyErr) -> PyErr {
|
||||
if error.get_type(py).is(PyTypeError::type_object(py)) {
|
||||
let remapped_error =
|
||||
PyTypeError::new_err(format!("argument '{}': {}", arg_name, error.value(py)));
|
||||
|
|
|
@ -2,7 +2,7 @@ use crate::{exceptions::PyTypeError, PyErr, Python};
|
|||
|
||||
#[cold]
|
||||
pub fn failed_to_extract_enum(
|
||||
py: Python,
|
||||
py: Python<'_>,
|
||||
type_name: &str,
|
||||
variant_names: &[&str],
|
||||
error_names: &[&str],
|
||||
|
|
|
@ -32,7 +32,7 @@ pub trait PyClassDict {
|
|||
fn new() -> Self;
|
||||
/// Empties the dictionary of its key-value pairs.
|
||||
#[inline]
|
||||
fn clear_dict(&mut self, _py: Python) {}
|
||||
fn clear_dict(&mut self, _py: Python<'_>) {}
|
||||
private_decl! {}
|
||||
}
|
||||
|
||||
|
@ -46,7 +46,7 @@ pub trait PyClassWeakRef {
|
|||
/// - `_obj` must be a pointer to the pyclass instance which contains `self`.
|
||||
/// - The GIL must be held.
|
||||
#[inline]
|
||||
unsafe fn clear_weakrefs(&mut self, _obj: *mut ffi::PyObject, _py: Python) {}
|
||||
unsafe fn clear_weakrefs(&mut self, _obj: *mut ffi::PyObject, _py: Python<'_>) {}
|
||||
private_decl! {}
|
||||
}
|
||||
|
||||
|
@ -82,7 +82,7 @@ impl PyClassDict for PyClassDictSlot {
|
|||
Self(std::ptr::null_mut())
|
||||
}
|
||||
#[inline]
|
||||
fn clear_dict(&mut self, _py: Python) {
|
||||
fn clear_dict(&mut self, _py: Python<'_>) {
|
||||
if !self.0.is_null() {
|
||||
unsafe { ffi::PyDict_Clear(self.0) }
|
||||
}
|
||||
|
@ -102,7 +102,7 @@ impl PyClassWeakRef for PyClassWeakRefSlot {
|
|||
Self(std::ptr::null_mut())
|
||||
}
|
||||
#[inline]
|
||||
unsafe fn clear_weakrefs(&mut self, obj: *mut ffi::PyObject, _py: Python) {
|
||||
unsafe fn clear_weakrefs(&mut self, obj: *mut ffi::PyObject, _py: Python<'_>) {
|
||||
if !self.0.is_null() {
|
||||
ffi::PyObject_ClearWeakRefs(obj)
|
||||
}
|
||||
|
@ -205,7 +205,7 @@ slot_fragment_trait! {
|
|||
#[inline]
|
||||
unsafe fn __getattribute__(
|
||||
self,
|
||||
py: Python,
|
||||
py: Python<'_>,
|
||||
slf: *mut ffi::PyObject,
|
||||
attr: *mut ffi::PyObject,
|
||||
) -> PyResult<*mut ffi::PyObject> {
|
||||
|
@ -225,7 +225,7 @@ slot_fragment_trait! {
|
|||
#[inline]
|
||||
unsafe fn __getattr__(
|
||||
self,
|
||||
py: Python,
|
||||
py: Python<'_>,
|
||||
_slf: *mut ffi::PyObject,
|
||||
attr: *mut ffi::PyObject,
|
||||
) -> PyResult<*mut ffi::PyObject> {
|
||||
|
@ -299,7 +299,7 @@ macro_rules! define_pyclass_setattr_slot {
|
|||
#[inline]
|
||||
unsafe fn $set(
|
||||
self,
|
||||
_py: Python,
|
||||
_py: Python<'_>,
|
||||
_slf: *mut ffi::PyObject,
|
||||
_attr: *mut ffi::PyObject,
|
||||
_value: NonNull<ffi::PyObject>,
|
||||
|
@ -315,7 +315,7 @@ macro_rules! define_pyclass_setattr_slot {
|
|||
#[inline]
|
||||
unsafe fn $del(
|
||||
self,
|
||||
_py: Python,
|
||||
_py: Python<'_>,
|
||||
_slf: *mut ffi::PyObject,
|
||||
_attr: *mut ffi::PyObject,
|
||||
) -> PyResult<()> {
|
||||
|
@ -416,7 +416,7 @@ macro_rules! define_pyclass_binary_operator_slot {
|
|||
#[inline]
|
||||
unsafe fn $lhs(
|
||||
self,
|
||||
_py: Python,
|
||||
_py: Python<'_>,
|
||||
_slf: *mut ffi::PyObject,
|
||||
_other: *mut ffi::PyObject,
|
||||
) -> PyResult<*mut ffi::PyObject> {
|
||||
|
@ -431,7 +431,7 @@ macro_rules! define_pyclass_binary_operator_slot {
|
|||
#[inline]
|
||||
unsafe fn $rhs(
|
||||
self,
|
||||
_py: Python,
|
||||
_py: Python<'_>,
|
||||
_slf: *mut ffi::PyObject,
|
||||
_other: *mut ffi::PyObject,
|
||||
) -> PyResult<*mut ffi::PyObject> {
|
||||
|
@ -611,7 +611,7 @@ slot_fragment_trait! {
|
|||
#[inline]
|
||||
unsafe fn __pow__(
|
||||
self,
|
||||
_py: Python,
|
||||
_py: Python<'_>,
|
||||
_slf: *mut ffi::PyObject,
|
||||
_other: *mut ffi::PyObject,
|
||||
_mod: *mut ffi::PyObject,
|
||||
|
@ -627,7 +627,7 @@ slot_fragment_trait! {
|
|||
#[inline]
|
||||
unsafe fn __rpow__(
|
||||
self,
|
||||
_py: Python,
|
||||
_py: Python<'_>,
|
||||
_slf: *mut ffi::PyObject,
|
||||
_other: *mut ffi::PyObject,
|
||||
_mod: *mut ffi::PyObject,
|
||||
|
@ -675,7 +675,7 @@ pub use generate_pyclass_pow_slot;
|
|||
/// Do not implement this trait manually. Instead, use `#[pyclass(freelist = N)]`
|
||||
/// on a Rust struct to implement it.
|
||||
pub trait PyClassWithFreeList: PyClass {
|
||||
fn get_free_list(py: Python) -> &mut FreeList<*mut ffi::PyObject>;
|
||||
fn get_free_list(py: Python<'_>) -> &mut FreeList<*mut ffi::PyObject>;
|
||||
}
|
||||
|
||||
/// Implementation of tp_alloc for `freelist` classes.
|
||||
|
@ -737,7 +737,7 @@ pub unsafe extern "C" fn free_with_freelist<T: PyClassWithFreeList>(obj: *mut c_
|
|||
/// Workaround for Python issue 35810; no longer necessary in Python 3.8
|
||||
#[inline]
|
||||
#[cfg(not(Py_3_8))]
|
||||
unsafe fn bpo_35810_workaround(_py: Python, ty: *mut ffi::PyTypeObject) {
|
||||
unsafe fn bpo_35810_workaround(_py: Python<'_>, ty: *mut ffi::PyTypeObject) {
|
||||
#[cfg(Py_LIMITED_API)]
|
||||
{
|
||||
// Must check version at runtime for abi3 wheels - they could run against a higher version
|
||||
|
|
|
@ -15,7 +15,7 @@ pub struct ModuleDef {
|
|||
}
|
||||
|
||||
/// Wrapper to enable initializer to be used in const fns.
|
||||
pub struct ModuleInitializer(pub fn(Python, &PyModule) -> PyResult<()>);
|
||||
pub struct ModuleInitializer(pub for<'py> fn(Python<'py>, &PyModule) -> PyResult<()>);
|
||||
|
||||
unsafe impl Sync for ModuleDef {}
|
||||
|
||||
|
@ -53,7 +53,7 @@ impl ModuleDef {
|
|||
}
|
||||
}
|
||||
/// Builds a module using user given initializer. Used for [`#[pymodule]`][crate::pymodule].
|
||||
pub fn make_module(&'static self, py: Python) -> PyResult<PyObject> {
|
||||
pub fn make_module(&'static self, py: Python<'_>) -> PyResult<PyObject> {
|
||||
let module = unsafe {
|
||||
Py::<PyModule>::from_owned_ptr_or_err(py, ffi::PyModule_Create(self.ffi_def.get()))?
|
||||
};
|
||||
|
@ -154,7 +154,7 @@ mod tests {
|
|||
static INIT_CALLED: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
#[allow(clippy::unnecessary_wraps)]
|
||||
fn init(_: Python, _: &PyModule) -> PyResult<()> {
|
||||
fn init(_: Python<'_>, _: &PyModule) -> PyResult<()> {
|
||||
INIT_CALLED.store(true, Ordering::SeqCst);
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ use std::ptr::NonNull;
|
|||
pub unsafe trait PyNativeType: Sized {
|
||||
/// 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() }
|
||||
}
|
||||
/// Cast `&PyAny` to `&Self` without no type checking.
|
||||
|
@ -251,7 +251,7 @@ where
|
|||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn new(py: Python, value: impl Into<PyClassInitializer<T>>) -> PyResult<Py<T>> {
|
||||
pub fn new(py: Python<'_>, value: impl Into<PyClassInitializer<T>>) -> PyResult<Py<T>> {
|
||||
let initializer = value.into();
|
||||
let obj = initializer.create_cell(py)?;
|
||||
let ob = unsafe { Py::from_owned_ptr(py, obj as _) };
|
||||
|
@ -351,7 +351,7 @@ where
|
|||
/// obj.into_ref(py)
|
||||
/// }
|
||||
/// ```
|
||||
pub fn into_ref(self, py: Python) -> &T::AsRefTarget {
|
||||
pub fn into_ref(self, py: Python<'_>) -> &T::AsRefTarget {
|
||||
unsafe { py.from_owned_ptr(self.into_ptr()) }
|
||||
}
|
||||
}
|
||||
|
@ -474,7 +474,7 @@ impl<T> Py<T> {
|
|||
|
||||
/// Gets the reference count of the `ffi::PyObject` pointer.
|
||||
#[inline]
|
||||
pub fn get_refcnt(&self, _py: Python) -> isize {
|
||||
pub fn get_refcnt(&self, _py: Python<'_>) -> isize {
|
||||
unsafe { ffi::Py_REFCNT(self.0.as_ptr()) }
|
||||
}
|
||||
|
||||
|
@ -502,21 +502,21 @@ impl<T> Py<T> {
|
|||
/// # }
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn clone_ref(&self, py: Python) -> Py<T> {
|
||||
pub fn clone_ref(&self, py: Python<'_>) -> Py<T> {
|
||||
unsafe { Py::from_borrowed_ptr(py, self.0.as_ptr()) }
|
||||
}
|
||||
|
||||
/// Returns whether the object is considered to be None.
|
||||
///
|
||||
/// This is equivalent to the Python expression `self is None`.
|
||||
pub fn is_none(&self, _py: Python) -> bool {
|
||||
pub fn is_none(&self, _py: Python<'_>) -> bool {
|
||||
unsafe { ffi::Py_None() == self.as_ptr() }
|
||||
}
|
||||
|
||||
/// Returns whether the object is considered to be true.
|
||||
///
|
||||
/// This is equivalent to the Python expression `bool(self)`.
|
||||
pub fn is_true(&self, py: Python) -> PyResult<bool> {
|
||||
pub fn is_true(&self, py: Python<'_>) -> PyResult<bool> {
|
||||
let v = unsafe { ffi::PyObject_IsTrue(self.as_ptr()) };
|
||||
err::error_on_minusone(py, v)?;
|
||||
Ok(v != 0)
|
||||
|
@ -535,7 +535,7 @@ impl<T> Py<T> {
|
|||
/// Retrieves an attribute value.
|
||||
///
|
||||
/// This is equivalent to the Python expression `self.attr_name`.
|
||||
pub fn getattr<N>(&self, py: Python, attr_name: N) -> PyResult<PyObject>
|
||||
pub fn getattr<N>(&self, py: Python<'_>, attr_name: N) -> PyResult<PyObject>
|
||||
where
|
||||
N: ToPyObject,
|
||||
{
|
||||
|
@ -547,7 +547,7 @@ impl<T> Py<T> {
|
|||
/// Sets an attribute value.
|
||||
///
|
||||
/// This is equivalent to the Python expression `self.attr_name = value`.
|
||||
pub fn setattr<N, V>(&self, py: Python, attr_name: N, value: V) -> PyResult<()>
|
||||
pub fn setattr<N, V>(&self, py: Python<'_>, attr_name: N, value: V) -> PyResult<()>
|
||||
where
|
||||
N: ToPyObject,
|
||||
V: ToPyObject,
|
||||
|
@ -564,7 +564,7 @@ impl<T> Py<T> {
|
|||
/// This is equivalent to the Python expression `self(*args, **kwargs)`.
|
||||
pub fn call(
|
||||
&self,
|
||||
py: Python,
|
||||
py: Python<'_>,
|
||||
args: impl IntoPy<Py<PyTuple>>,
|
||||
kwargs: Option<&PyDict>,
|
||||
) -> PyResult<PyObject> {
|
||||
|
@ -583,14 +583,14 @@ impl<T> Py<T> {
|
|||
/// Calls the object with only positional arguments.
|
||||
///
|
||||
/// This is equivalent to the Python expression `self(*args)`.
|
||||
pub fn call1(&self, py: Python, args: impl IntoPy<Py<PyTuple>>) -> PyResult<PyObject> {
|
||||
pub fn call1(&self, py: Python<'_>, args: impl IntoPy<Py<PyTuple>>) -> PyResult<PyObject> {
|
||||
self.call(py, args, None)
|
||||
}
|
||||
|
||||
/// Calls the object without arguments.
|
||||
///
|
||||
/// This is equivalent to the Python expression `self()`.
|
||||
pub fn call0(&self, py: Python) -> PyResult<PyObject> {
|
||||
pub fn call0(&self, py: Python<'_>) -> PyResult<PyObject> {
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(all(Py_3_9, not(PyPy)))] {
|
||||
// Optimized path on python 3.9+
|
||||
|
@ -608,7 +608,7 @@ impl<T> Py<T> {
|
|||
/// This is equivalent to the Python expression `self.name(*args, **kwargs)`.
|
||||
pub fn call_method(
|
||||
&self,
|
||||
py: Python,
|
||||
py: Python<'_>,
|
||||
name: &str,
|
||||
args: impl IntoPy<Py<PyTuple>>,
|
||||
kwargs: Option<&PyDict>,
|
||||
|
@ -633,7 +633,7 @@ impl<T> Py<T> {
|
|||
/// This is equivalent to the Python expression `self.name(*args)`.
|
||||
pub fn call_method1(
|
||||
&self,
|
||||
py: Python,
|
||||
py: Python<'_>,
|
||||
name: &str,
|
||||
args: impl IntoPy<Py<PyTuple>>,
|
||||
) -> PyResult<PyObject> {
|
||||
|
@ -643,7 +643,7 @@ impl<T> Py<T> {
|
|||
/// Calls a method on the object with no arguments.
|
||||
///
|
||||
/// This is equivalent to the Python expression `self.name()`.
|
||||
pub fn call_method0(&self, py: Python, name: &str) -> PyResult<PyObject> {
|
||||
pub fn call_method0(&self, py: Python<'_>, name: &str) -> PyResult<PyObject> {
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(all(Py_3_9, not(any(Py_LIMITED_API, PyPy))))] {
|
||||
// Optimized path on python 3.9+
|
||||
|
@ -668,7 +668,7 @@ impl<T> Py<T> {
|
|||
/// # Panics
|
||||
/// Panics if `ptr` is null.
|
||||
#[inline]
|
||||
pub unsafe fn from_owned_ptr(py: Python, ptr: *mut ffi::PyObject) -> Py<T> {
|
||||
pub unsafe fn from_owned_ptr(py: Python<'_>, ptr: *mut ffi::PyObject) -> Py<T> {
|
||||
match NonNull::new(ptr) {
|
||||
Some(nonnull_ptr) => Py(nonnull_ptr, PhantomData),
|
||||
None => crate::err::panic_after_error(py),
|
||||
|
@ -682,7 +682,10 @@ impl<T> Py<T> {
|
|||
/// # Safety
|
||||
/// If non-null, `ptr` must be a pointer to a Python object of type T.
|
||||
#[inline]
|
||||
pub unsafe fn from_owned_ptr_or_err(py: Python, ptr: *mut ffi::PyObject) -> PyResult<Py<T>> {
|
||||
pub unsafe fn from_owned_ptr_or_err(
|
||||
py: Python<'_>,
|
||||
ptr: *mut ffi::PyObject,
|
||||
) -> PyResult<Py<T>> {
|
||||
match NonNull::new(ptr) {
|
||||
Some(nonnull_ptr) => Ok(Py(nonnull_ptr, PhantomData)),
|
||||
None => Err(PyErr::fetch(py)),
|
||||
|
@ -696,7 +699,7 @@ impl<T> Py<T> {
|
|||
/// # Safety
|
||||
/// If non-null, `ptr` must be a pointer to a Python object of type T.
|
||||
#[inline]
|
||||
pub unsafe fn from_owned_ptr_or_opt(_py: Python, ptr: *mut ffi::PyObject) -> Option<Self> {
|
||||
pub unsafe fn from_owned_ptr_or_opt(_py: Python<'_>, ptr: *mut ffi::PyObject) -> Option<Self> {
|
||||
NonNull::new(ptr).map(|nonnull_ptr| Py(nonnull_ptr, PhantomData))
|
||||
}
|
||||
|
||||
|
@ -708,7 +711,7 @@ impl<T> Py<T> {
|
|||
/// # Panics
|
||||
/// Panics if `ptr` is null.
|
||||
#[inline]
|
||||
pub unsafe fn from_borrowed_ptr(py: Python, ptr: *mut ffi::PyObject) -> Py<T> {
|
||||
pub unsafe fn from_borrowed_ptr(py: Python<'_>, ptr: *mut ffi::PyObject) -> Py<T> {
|
||||
match Self::from_borrowed_ptr_or_opt(py, ptr) {
|
||||
Some(slf) => slf,
|
||||
None => crate::err::panic_after_error(py),
|
||||
|
@ -722,7 +725,10 @@ impl<T> Py<T> {
|
|||
/// # Safety
|
||||
/// `ptr` must be a pointer to a Python object of type T.
|
||||
#[inline]
|
||||
pub unsafe fn from_borrowed_ptr_or_err(py: Python, ptr: *mut ffi::PyObject) -> PyResult<Self> {
|
||||
pub unsafe fn from_borrowed_ptr_or_err(
|
||||
py: Python<'_>,
|
||||
ptr: *mut ffi::PyObject,
|
||||
) -> PyResult<Self> {
|
||||
Self::from_borrowed_ptr_or_opt(py, ptr).ok_or_else(|| PyErr::fetch(py))
|
||||
}
|
||||
|
||||
|
@ -733,7 +739,10 @@ impl<T> Py<T> {
|
|||
/// # Safety
|
||||
/// `ptr` must be a pointer to a Python object of type T.
|
||||
#[inline]
|
||||
pub unsafe fn from_borrowed_ptr_or_opt(_py: Python, ptr: *mut ffi::PyObject) -> Option<Self> {
|
||||
pub unsafe fn from_borrowed_ptr_or_opt(
|
||||
_py: Python<'_>,
|
||||
ptr: *mut ffi::PyObject,
|
||||
) -> Option<Self> {
|
||||
NonNull::new(ptr).map(|nonnull_ptr| {
|
||||
ffi::Py_INCREF(ptr);
|
||||
Py(nonnull_ptr, PhantomData)
|
||||
|
@ -760,7 +769,7 @@ impl<T> Py<T> {
|
|||
|
||||
impl<T> ToPyObject for Py<T> {
|
||||
/// Converts `Py` instance -> PyObject.
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
unsafe { PyObject::from_borrowed_ptr(py, self.as_ptr()) }
|
||||
}
|
||||
}
|
||||
|
@ -769,7 +778,7 @@ impl<T> IntoPy<PyObject> for Py<T> {
|
|||
/// Converts a `Py` instance to `PyObject`.
|
||||
/// Consumes `self` without calling `Py_DECREF()`.
|
||||
#[inline]
|
||||
fn into_py(self, _py: Python) -> PyObject {
|
||||
fn into_py(self, _py: Python<'_>) -> PyObject {
|
||||
unsafe { PyObject::from_non_null(self.into_non_null()) }
|
||||
}
|
||||
}
|
||||
|
@ -811,7 +820,7 @@ where
|
|||
}
|
||||
|
||||
// `&PyCell<T>` can be converted to `Py<T>`
|
||||
impl<'a, T> std::convert::From<&PyCell<T>> for Py<T>
|
||||
impl<T> std::convert::From<&PyCell<T>> for Py<T>
|
||||
where
|
||||
T: PyClass,
|
||||
{
|
||||
|
@ -890,13 +899,13 @@ where
|
|||
T: PyTypeInfo,
|
||||
T::AsRefTarget: std::fmt::Display,
|
||||
{
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
Python::with_gil(|py| std::fmt::Display::fmt(self.as_ref(py), f))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> std::fmt::Debug for Py<T> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_tuple("Py").field(&self.0.as_ptr()).finish()
|
||||
}
|
||||
}
|
||||
|
@ -914,11 +923,11 @@ impl PyObject {
|
|||
///
|
||||
/// This can cast only to native Python types, not types implemented in Rust. For a more
|
||||
/// flexible alternative, see [`Py::extract`](struct.Py.html#method.extract).
|
||||
pub fn cast_as<'p, D>(&'p self, py: Python<'p>) -> Result<&'p D, PyDowncastError>
|
||||
pub fn cast_as<'p, D>(&'p self, py: Python<'p>) -> Result<&'p D, PyDowncastError<'_>>
|
||||
where
|
||||
D: PyTryFrom<'p>,
|
||||
{
|
||||
D::try_from(unsafe { py.from_borrowed_ptr::<PyAny>(self.as_ptr()) })
|
||||
<D as PyTryFrom<'_>>::try_from(unsafe { py.from_borrowed_ptr::<PyAny>(self.as_ptr()) })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
14
src/lib.rs
14
src/lib.rs
|
@ -10,7 +10,15 @@
|
|||
)]
|
||||
// Deny some lints in doctests.
|
||||
// Use `#[allow(...)]` locally to override.
|
||||
#![doc(test(attr(deny(warnings), allow(unused_variables, unused_assignments))))]
|
||||
#![doc(test(attr(
|
||||
deny(
|
||||
elided_lifetimes_in_paths,
|
||||
unused_lifetimes,
|
||||
rust_2021_prelude_collisions,
|
||||
warnings
|
||||
),
|
||||
allow(unused_variables, unused_assignments)
|
||||
)))]
|
||||
#![cfg_attr(coverage, feature(no_coverage))] // used in src/test_hygiene.rs
|
||||
|
||||
//! Rust bindings to the Python interpreter.
|
||||
|
@ -160,7 +168,7 @@
|
|||
//!
|
||||
//! /// A Python module implemented in Rust.
|
||||
//! #[pymodule]
|
||||
//! fn string_sum(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
//! fn string_sum(py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
//! m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
|
||||
//!
|
||||
//! Ok(())
|
||||
|
@ -226,7 +234,7 @@
|
|||
//! let code = "os.getenv('USER') or os.getenv('USERNAME') or 'Unknown'";
|
||||
//! let user: String = py.eval(code, None, Some(&locals))?.extract()?;
|
||||
//!
|
||||
//! println!("Hello {}, I'm Python {}", user, version);
|
||||
//! println!("Hello {}, I'm Python<'_> {}", user, version);
|
||||
//! Ok(())
|
||||
//! })
|
||||
//! }
|
||||
|
|
|
@ -779,7 +779,7 @@ impl<'py> Python<'py> {
|
|||
///
|
||||
/// # fn main(){
|
||||
/// #[pyfunction]
|
||||
/// fn loop_forever(py: Python) -> PyResult<()> {
|
||||
/// fn loop_forever(py: Python<'_>) -> PyResult<()> {
|
||||
/// loop {
|
||||
/// // As this loop is infinite it should check for signals every once in a while.
|
||||
/// // Using `?` causes any `PyErr` (potentially containing `KeyboardInterrupt`)
|
||||
|
|
|
@ -72,7 +72,7 @@ mod tests {
|
|||
});
|
||||
}
|
||||
|
||||
fn equal(_py: Python, a: &impl AsPyPointer, b: &impl AsPyPointer) -> bool {
|
||||
fn equal(_py: Python<'_>, a: &impl AsPyPointer, b: &impl AsPyPointer) -> bool {
|
||||
unsafe { ffi::PyObject_RichCompareBool(a.as_ptr(), b.as_ptr(), ffi::Py_EQ) != 0 }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ use std::cell::UnsafeCell;
|
|||
///
|
||||
/// static LIST_CELL: GILOnceCell<Py<PyList>> = GILOnceCell::new();
|
||||
///
|
||||
/// pub fn get_shared_list(py: Python) -> &PyList {
|
||||
/// pub fn get_shared_list(py: Python<'_>) -> &PyList {
|
||||
/// LIST_CELL
|
||||
/// .get_or_init(py, || PyList::empty(py).into())
|
||||
/// .as_ref(py)
|
||||
|
@ -42,7 +42,7 @@ impl<T> GILOnceCell<T> {
|
|||
}
|
||||
|
||||
/// Get a reference to the contained value, or `None` if the cell has not yet been written.
|
||||
pub fn get(&self, _py: Python) -> Option<&T> {
|
||||
pub fn get(&self, _py: Python<'_>) -> Option<&T> {
|
||||
// Safe because if the cell has not yet been written, None is returned.
|
||||
unsafe { &*self.0.get() }.as_ref()
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ impl<T> GILOnceCell<T> {
|
|||
/// exactly once, even if multiple threads attempt to call `get_or_init`
|
||||
/// 4) if f() can panic but still does not release the GIL, it may be called multiple times,
|
||||
/// but it is guaranteed that f() will never be called concurrently
|
||||
pub fn get_or_init<F>(&self, py: Python, f: F) -> &T
|
||||
pub fn get_or_init<F>(&self, py: Python<'_>, f: F) -> &T
|
||||
where
|
||||
F: FnOnce() -> T,
|
||||
{
|
||||
|
@ -90,7 +90,7 @@ impl<T> GILOnceCell<T> {
|
|||
///
|
||||
/// If the cell has already been written, `Err(value)` will be returned containing the new
|
||||
/// value which was not written.
|
||||
pub fn set(&self, _py: Python, value: T) -> Result<(), T> {
|
||||
pub fn set(&self, _py: Python<'_>, value: T) -> Result<(), T> {
|
||||
// Safe because GIL is held, so no other thread can be writing to this cell concurrently.
|
||||
let inner = unsafe { &mut *self.0.get() };
|
||||
if inner.is_some() {
|
||||
|
|
|
@ -259,7 +259,7 @@ impl<T: PyClass> PyCell<T> {
|
|||
///
|
||||
/// In cases where the value in the cell does not need to be accessed immediately after
|
||||
/// creation, consider [`Py::new`](crate::Py::new) as a more efficient alternative.
|
||||
pub fn new(py: Python, value: impl Into<PyClassInitializer<T>>) -> PyResult<&Self> {
|
||||
pub fn new(py: Python<'_>, value: impl Into<PyClassInitializer<T>>) -> PyResult<&Self> {
|
||||
unsafe {
|
||||
let initializer = value.into();
|
||||
let self_ = initializer.create_cell(py)?;
|
||||
|
@ -567,7 +567,7 @@ impl<T: PyClass + fmt::Debug> fmt::Debug for PyCell<T> {
|
|||
/// (Child { name: "Caterpillar" }, Parent { basename: "Butterfly" })
|
||||
/// }
|
||||
///
|
||||
/// fn format(slf: PyRef<Self>) -> String {
|
||||
/// fn format(slf: PyRef<'_, Self>) -> String {
|
||||
/// // We can get *mut ffi::PyObject from PyRef
|
||||
/// use pyo3::AsPyPointer;
|
||||
/// let refcnt = unsafe { pyo3::ffi::Py_REFCNT(slf.as_ptr()) };
|
||||
|
@ -589,7 +589,7 @@ pub struct PyRef<'p, T: PyClass> {
|
|||
|
||||
impl<'p, T: PyClass> PyRef<'p, T> {
|
||||
/// Returns a `Python` token that is bound to the lifetime of the `PyRef`.
|
||||
pub fn py(&self) -> Python {
|
||||
pub fn py(&self) -> Python<'_> {
|
||||
unsafe { Python::assume_gil_acquired() }
|
||||
}
|
||||
}
|
||||
|
@ -643,7 +643,7 @@ where
|
|||
/// .add_subclass(Base2 { name2: "base2" })
|
||||
/// .add_subclass(Self { name3: "sub" })
|
||||
/// }
|
||||
/// fn name(slf: PyRef<Self>) -> String {
|
||||
/// fn name(slf: PyRef<'_, Self>) -> String {
|
||||
/// let subname = slf.name3;
|
||||
/// let super_ = slf.into_super();
|
||||
/// format!("{} {} {}", super_.as_ref().name1, super_.name2, subname)
|
||||
|
@ -713,7 +713,7 @@ pub struct PyRefMut<'p, T: PyClass> {
|
|||
|
||||
impl<'p, T: PyClass> PyRefMut<'p, T> {
|
||||
/// Returns a `Python` token that is bound to the lifetime of the `PyRefMut`.
|
||||
pub fn py(&self) -> Python {
|
||||
pub fn py(&self) -> Python<'_> {
|
||||
unsafe { Python::assume_gil_acquired() }
|
||||
}
|
||||
}
|
||||
|
@ -778,7 +778,7 @@ impl<'p, T: PyClass> Drop for PyRefMut<'p, T> {
|
|||
}
|
||||
|
||||
impl<T: PyClass> IntoPy<PyObject> for PyRefMut<'_, T> {
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
unsafe { PyObject::from_borrowed_ptr(py, self.inner.as_ptr()) }
|
||||
}
|
||||
}
|
||||
|
@ -875,7 +875,7 @@ pub trait PyCellLayout<T>: PyLayout<T> {
|
|||
/// # Safety
|
||||
/// - slf must be a valid pointer to an instance of a T or a subclass.
|
||||
/// - slf must not be used after this call (as it will be freed).
|
||||
unsafe fn tp_dealloc(slf: *mut ffi::PyObject, py: Python);
|
||||
unsafe fn tp_dealloc(slf: *mut ffi::PyObject, py: Python<'_>);
|
||||
}
|
||||
|
||||
impl<T, U> PyCellLayout<T> for PyCellBase<U>
|
||||
|
@ -889,7 +889,7 @@ where
|
|||
fn set_borrow_flag(&self, flag: BorrowFlag) {
|
||||
self.borrow_flag.set(flag)
|
||||
}
|
||||
unsafe fn tp_dealloc(slf: *mut ffi::PyObject, py: Python) {
|
||||
unsafe fn tp_dealloc(slf: *mut ffi::PyObject, py: Python<'_>) {
|
||||
// For `#[pyclass]` types which inherit from PyAny, we can just call tp_free
|
||||
if T::type_object_raw(py) == &mut PyBaseObject_Type {
|
||||
return get_tp_free(ffi::Py_TYPE(slf))(slf as _);
|
||||
|
@ -921,7 +921,7 @@ where
|
|||
fn set_borrow_flag(&self, flag: BorrowFlag) {
|
||||
self.ob_base.set_borrow_flag(flag)
|
||||
}
|
||||
unsafe fn tp_dealloc(slf: *mut ffi::PyObject, py: Python) {
|
||||
unsafe fn tp_dealloc(slf: *mut ffi::PyObject, py: Python<'_>) {
|
||||
// Safety: Python only calls tp_dealloc when no references to the object remain.
|
||||
let cell = &mut *(slf as *mut PyCell<T>);
|
||||
ManuallyDrop::drop(&mut cell.contents.value);
|
||||
|
|
|
@ -38,7 +38,7 @@ fn into_raw<T>(vec: Vec<T>) -> *mut c_void {
|
|||
Box::into_raw(vec.into_boxed_slice()) as _
|
||||
}
|
||||
|
||||
pub(crate) fn create_type_object<T>(py: Python) -> *mut ffi::PyTypeObject
|
||||
pub(crate) fn create_type_object<T>(py: Python<'_>) -> *mut ffi::PyTypeObject
|
||||
where
|
||||
T: PyClass,
|
||||
{
|
||||
|
@ -64,7 +64,7 @@ where
|
|||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
unsafe fn create_type_object_impl(
|
||||
py: Python,
|
||||
py: Python<'_>,
|
||||
tp_doc: &str,
|
||||
module_name: Option<&str>,
|
||||
name: &str,
|
||||
|
@ -210,7 +210,7 @@ unsafe fn create_type_object_impl(
|
|||
}
|
||||
|
||||
#[cold]
|
||||
fn type_object_creation_failed(py: Python, e: PyErr, name: &'static str) -> ! {
|
||||
fn type_object_creation_failed(py: Python<'_>, e: PyErr, name: &'static str) -> ! {
|
||||
e.print(py);
|
||||
panic!("An error occurred while initializing class {}", name)
|
||||
}
|
||||
|
@ -479,7 +479,7 @@ pub enum IterNextOutput<T, U> {
|
|||
pub type PyIterNextOutput = IterNextOutput<PyObject, PyObject>;
|
||||
|
||||
impl IntoPyCallbackOutput<*mut ffi::PyObject> for PyIterNextOutput {
|
||||
fn convert(self, _py: Python) -> PyResult<*mut ffi::PyObject> {
|
||||
fn convert(self, _py: Python<'_>) -> PyResult<*mut ffi::PyObject> {
|
||||
match self {
|
||||
IterNextOutput::Yield(o) => Ok(o.into_ptr()),
|
||||
IterNextOutput::Return(opt) => Err(crate::exceptions::PyStopIteration::new_err((opt,))),
|
||||
|
@ -492,7 +492,7 @@ where
|
|||
T: IntoPy<PyObject>,
|
||||
U: IntoPy<PyObject>,
|
||||
{
|
||||
fn convert(self, py: Python) -> PyResult<PyIterNextOutput> {
|
||||
fn convert(self, py: Python<'_>) -> PyResult<PyIterNextOutput> {
|
||||
match self {
|
||||
IterNextOutput::Yield(o) => Ok(IterNextOutput::Yield(o.into_py(py))),
|
||||
IterNextOutput::Return(o) => Ok(IterNextOutput::Return(o.into_py(py))),
|
||||
|
@ -504,7 +504,7 @@ impl<T> IntoPyCallbackOutput<PyIterNextOutput> for Option<T>
|
|||
where
|
||||
T: IntoPy<PyObject>,
|
||||
{
|
||||
fn convert(self, py: Python) -> PyResult<PyIterNextOutput> {
|
||||
fn convert(self, py: Python<'_>) -> PyResult<PyIterNextOutput> {
|
||||
match self {
|
||||
Some(o) => Ok(PyIterNextOutput::Yield(o.into_py(py))),
|
||||
None => Ok(PyIterNextOutput::Return(py.None())),
|
||||
|
@ -526,7 +526,7 @@ pub enum IterANextOutput<T, U> {
|
|||
pub type PyIterANextOutput = IterANextOutput<PyObject, PyObject>;
|
||||
|
||||
impl IntoPyCallbackOutput<*mut ffi::PyObject> for PyIterANextOutput {
|
||||
fn convert(self, _py: Python) -> PyResult<*mut ffi::PyObject> {
|
||||
fn convert(self, _py: Python<'_>) -> PyResult<*mut ffi::PyObject> {
|
||||
match self {
|
||||
IterANextOutput::Yield(o) => Ok(o.into_ptr()),
|
||||
IterANextOutput::Return(opt) => {
|
||||
|
@ -541,7 +541,7 @@ where
|
|||
T: IntoPy<PyObject>,
|
||||
U: IntoPy<PyObject>,
|
||||
{
|
||||
fn convert(self, py: Python) -> PyResult<PyIterANextOutput> {
|
||||
fn convert(self, py: Python<'_>) -> PyResult<PyIterANextOutput> {
|
||||
match self {
|
||||
IterANextOutput::Yield(o) => Ok(IterANextOutput::Yield(o.into_py(py))),
|
||||
IterANextOutput::Return(o) => Ok(IterANextOutput::Return(o.into_py(py))),
|
||||
|
@ -553,7 +553,7 @@ impl<T> IntoPyCallbackOutput<PyIterANextOutput> for Option<T>
|
|||
where
|
||||
T: IntoPy<PyObject>,
|
||||
{
|
||||
fn convert(self, py: Python) -> PyResult<PyIterANextOutput> {
|
||||
fn convert(self, py: Python<'_>) -> PyResult<PyIterANextOutput> {
|
||||
match self {
|
||||
Some(o) => Ok(PyIterANextOutput::Yield(o.into_py(py))),
|
||||
None => Ok(PyIterANextOutput::Return(py.None())),
|
||||
|
|
|
@ -22,7 +22,7 @@ pub trait PyObjectInit<T>: Sized {
|
|||
/// - `subtype` must be a valid pointer to a type object of T or a subclass.
|
||||
unsafe fn into_new_object(
|
||||
self,
|
||||
py: Python,
|
||||
py: Python<'_>,
|
||||
subtype: *mut PyTypeObject,
|
||||
) -> PyResult<*mut ffi::PyObject>;
|
||||
private_decl! {}
|
||||
|
@ -34,7 +34,7 @@ pub struct PyNativeTypeInitializer<T: PyTypeInfo>(PhantomData<T>);
|
|||
impl<T: PyTypeInfo> PyObjectInit<T> for PyNativeTypeInitializer<T> {
|
||||
unsafe fn into_new_object(
|
||||
self,
|
||||
py: Python,
|
||||
py: Python<'_>,
|
||||
subtype: *mut PyTypeObject,
|
||||
) -> PyResult<*mut ffi::PyObject> {
|
||||
let type_object = T::type_object_raw(py);
|
||||
|
@ -197,7 +197,7 @@ impl<T: PyClass> PyClassInitializer<T> {
|
|||
|
||||
/// Creates a new PyCell and initializes it.
|
||||
#[doc(hidden)]
|
||||
pub fn create_cell(self, py: Python) -> PyResult<*mut PyCell<T>>
|
||||
pub fn create_cell(self, py: Python<'_>) -> PyResult<*mut PyCell<T>>
|
||||
where
|
||||
T: PyClass,
|
||||
{
|
||||
|
@ -212,7 +212,7 @@ impl<T: PyClass> PyClassInitializer<T> {
|
|||
#[doc(hidden)]
|
||||
pub unsafe fn create_cell_from_subtype(
|
||||
self,
|
||||
py: Python,
|
||||
py: Python<'_>,
|
||||
subtype: *mut crate::ffi::PyTypeObject,
|
||||
) -> PyResult<*mut PyCell<T>>
|
||||
where
|
||||
|
@ -225,7 +225,7 @@ impl<T: PyClass> PyClassInitializer<T> {
|
|||
impl<T: PyClass> PyObjectInit<T> for PyClassInitializer<T> {
|
||||
unsafe fn into_new_object(
|
||||
self,
|
||||
py: Python,
|
||||
py: Python<'_>,
|
||||
subtype: *mut PyTypeObject,
|
||||
) -> PyResult<*mut ffi::PyObject> {
|
||||
/// Layout of a PyCellBase after base new has been called, but the borrow flag has not
|
||||
|
@ -302,7 +302,7 @@ where
|
|||
U: Into<PyClassInitializer<T>>,
|
||||
{
|
||||
#[inline]
|
||||
fn convert(self, _py: Python) -> PyResult<PyClassInitializer<T>> {
|
||||
fn convert(self, _py: Python<'_>) -> PyResult<PyClassInitializer<T>> {
|
||||
Ok(self.into())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -114,7 +114,7 @@ impl Dummy {
|
|||
|
||||
fn __delitem__(&self, key: u32) {}
|
||||
|
||||
fn __iter__(_: crate::pycell::PyRef<Self>, py: crate::Python) -> crate::Py<DummyIter> {
|
||||
fn __iter__(_: crate::pycell::PyRef<'_, Self>, py: crate::Python<'_>) -> crate::Py<DummyIter> {
|
||||
crate::Py::new(py, DummyIter {}).unwrap()
|
||||
}
|
||||
|
||||
|
@ -122,7 +122,10 @@ impl Dummy {
|
|||
::std::option::Option::None
|
||||
}
|
||||
|
||||
fn __reversed__(slf: crate::pycell::PyRef<Self>, py: crate::Python) -> crate::Py<DummyIter> {
|
||||
fn __reversed__(
|
||||
slf: crate::pycell::PyRef<'_, Self>,
|
||||
py: crate::Python<'_>,
|
||||
) -> crate::Py<DummyIter> {
|
||||
crate::Py::new(py, DummyIter {}).unwrap()
|
||||
}
|
||||
|
||||
|
@ -262,19 +265,19 @@ impl Dummy {
|
|||
|
||||
fn __ior__(&mut self, other: &Self) {}
|
||||
|
||||
fn __neg__(slf: crate::pycell::PyRef<Self>) -> crate::pycell::PyRef<Self> {
|
||||
fn __neg__(slf: crate::pycell::PyRef<'_, Self>) -> crate::pycell::PyRef<'_, Self> {
|
||||
slf
|
||||
}
|
||||
|
||||
fn __pos__(slf: crate::pycell::PyRef<Self>) -> crate::pycell::PyRef<Self> {
|
||||
fn __pos__(slf: crate::pycell::PyRef<'_, Self>) -> crate::pycell::PyRef<'_, Self> {
|
||||
slf
|
||||
}
|
||||
|
||||
fn __abs__(slf: crate::pycell::PyRef<Self>) -> crate::pycell::PyRef<Self> {
|
||||
fn __abs__(slf: crate::pycell::PyRef<'_, Self>) -> crate::pycell::PyRef<'_, Self> {
|
||||
slf
|
||||
}
|
||||
|
||||
fn __invert__(slf: crate::pycell::PyRef<Self>) -> crate::pycell::PyRef<Self> {
|
||||
fn __invert__(slf: crate::pycell::PyRef<'_, Self>) -> crate::pycell::PyRef<'_, Self> {
|
||||
slf
|
||||
}
|
||||
|
||||
|
@ -328,7 +331,7 @@ impl Dummy {
|
|||
// Awaitable Objects
|
||||
//////////////////////
|
||||
|
||||
fn __await__(slf: crate::pycell::PyRef<Self>) -> crate::pycell::PyRef<Self> {
|
||||
fn __await__(slf: crate::pycell::PyRef<'_, Self>) -> crate::pycell::PyRef<'_, Self> {
|
||||
slf
|
||||
}
|
||||
|
||||
|
@ -337,7 +340,10 @@ impl Dummy {
|
|||
// Asynchronous Iterators
|
||||
//////////////////////
|
||||
|
||||
fn __aiter__(slf: crate::pycell::PyRef<Self>, py: crate::Python) -> crate::Py<DummyIter> {
|
||||
fn __aiter__(
|
||||
slf: crate::pycell::PyRef<'_, Self>,
|
||||
py: crate::Python<'_>,
|
||||
) -> crate::Py<DummyIter> {
|
||||
crate::Py::new(py, DummyIter {}).unwrap()
|
||||
}
|
||||
|
||||
|
@ -508,7 +514,7 @@ impl Dummy {
|
|||
|
||||
fn __delitem__(&self, key: u32) {}
|
||||
|
||||
fn __iter__(_: crate::pycell::PyRef<Self>, py: crate::Python) -> crate::Py<DummyIter> {
|
||||
fn __iter__(_: crate::pycell::PyRef<'_, Self>, py: crate::Python<'_>) -> crate::Py<DummyIter> {
|
||||
crate::Py::new(py, DummyIter {}).unwrap()
|
||||
}
|
||||
|
||||
|
@ -516,7 +522,10 @@ impl Dummy {
|
|||
::std::option::Option::None
|
||||
}
|
||||
|
||||
fn __reversed__(slf: crate::pycell::PyRef<Self>, py: crate::Python) -> crate::Py<DummyIter> {
|
||||
fn __reversed__(
|
||||
slf: crate::pycell::PyRef<'_, Self>,
|
||||
py: crate::Python<'_>,
|
||||
) -> crate::Py<DummyIter> {
|
||||
crate::Py::new(py, DummyIter {}).unwrap()
|
||||
}
|
||||
|
||||
|
@ -655,19 +664,19 @@ impl Dummy {
|
|||
|
||||
fn __ior__(&mut self, other: &Self) {}
|
||||
|
||||
fn __neg__(slf: crate::pycell::PyRef<Self>) -> crate::pycell::PyRef<Self> {
|
||||
fn __neg__(slf: crate::pycell::PyRef<'_, Self>) -> crate::pycell::PyRef<'_, Self> {
|
||||
slf
|
||||
}
|
||||
|
||||
fn __pos__(slf: crate::pycell::PyRef<Self>) -> crate::pycell::PyRef<Self> {
|
||||
fn __pos__(slf: crate::pycell::PyRef<'_, Self>) -> crate::pycell::PyRef<'_, Self> {
|
||||
slf
|
||||
}
|
||||
|
||||
fn __abs__(slf: crate::pycell::PyRef<Self>) -> crate::pycell::PyRef<Self> {
|
||||
fn __abs__(slf: crate::pycell::PyRef<'_, Self>) -> crate::pycell::PyRef<'_, Self> {
|
||||
slf
|
||||
}
|
||||
|
||||
fn __invert__(slf: crate::pycell::PyRef<Self>) -> crate::pycell::PyRef<Self> {
|
||||
fn __invert__(slf: crate::pycell::PyRef<'_, Self>) -> crate::pycell::PyRef<'_, Self> {
|
||||
slf
|
||||
}
|
||||
|
||||
|
@ -721,7 +730,7 @@ impl Dummy {
|
|||
// Awaitable Objects
|
||||
//////////////////////
|
||||
|
||||
fn __await__(slf: crate::pycell::PyRef<Self>) -> crate::pycell::PyRef<Self> {
|
||||
fn __await__(slf: crate::pycell::PyRef<'_, Self>) -> crate::pycell::PyRef<'_, Self> {
|
||||
slf
|
||||
}
|
||||
|
||||
|
@ -730,7 +739,10 @@ impl Dummy {
|
|||
// Asynchronous Iterators
|
||||
//////////////////////
|
||||
|
||||
fn __aiter__(slf: crate::pycell::PyRef<Self>, py: crate::Python) -> crate::Py<DummyIter> {
|
||||
fn __aiter__(
|
||||
slf: crate::pycell::PyRef<'_, Self>,
|
||||
py: crate::Python<'_>,
|
||||
) -> crate::Py<DummyIter> {
|
||||
crate::Py::new(py, DummyIter {}).unwrap()
|
||||
}
|
||||
|
||||
|
|
|
@ -9,13 +9,13 @@ fn do_something(x: i32) -> crate::PyResult<i32> {
|
|||
|
||||
#[crate::pymodule]
|
||||
#[pyo3(crate = "crate")]
|
||||
fn foo(_py: crate::Python, _m: &crate::types::PyModule) -> crate::PyResult<()> {
|
||||
fn foo(_py: crate::Python<'_>, _m: &crate::types::PyModule) -> crate::PyResult<()> {
|
||||
::std::result::Result::Ok(())
|
||||
}
|
||||
|
||||
#[crate::pymodule]
|
||||
#[pyo3(crate = "crate")]
|
||||
fn my_module(_py: crate::Python, m: &crate::types::PyModule) -> crate::PyResult<()> {
|
||||
fn my_module(_py: crate::Python<'_>, m: &crate::types::PyModule) -> crate::PyResult<()> {
|
||||
m.add_function(crate::wrap_pyfunction!(do_something, m)?)?;
|
||||
m.add_wrapped(crate::wrap_pymodule!(foo))?;
|
||||
|
||||
|
|
|
@ -52,7 +52,7 @@ pub unsafe trait PyTypeInfo: Sized {
|
|||
type AsRefTarget: PyNativeType;
|
||||
|
||||
/// PyTypeObject instance for this type.
|
||||
fn type_object_raw(py: Python) -> *mut ffi::PyTypeObject;
|
||||
fn type_object_raw(py: Python<'_>) -> *mut ffi::PyTypeObject;
|
||||
|
||||
/// Checks if `object` is an instance of this type or a subclass of this type.
|
||||
fn is_type_of(object: &PyAny) -> bool {
|
||||
|
@ -75,14 +75,14 @@ pub unsafe trait PyTypeInfo: Sized {
|
|||
/// See also [PyTypeInfo::type_object_raw](trait.PyTypeInfo.html#tymethod.type_object_raw).
|
||||
pub unsafe trait PyTypeObject {
|
||||
/// Returns the safe abstraction over the type object.
|
||||
fn type_object(py: Python) -> &PyType;
|
||||
fn type_object(py: Python<'_>) -> &PyType;
|
||||
}
|
||||
|
||||
unsafe impl<T> PyTypeObject for T
|
||||
where
|
||||
T: PyTypeInfo,
|
||||
{
|
||||
fn type_object(py: Python) -> &PyType {
|
||||
fn type_object(py: Python<'_>) -> &PyType {
|
||||
unsafe { py.from_borrowed_ptr(Self::type_object_raw(py) as _) }
|
||||
}
|
||||
}
|
||||
|
@ -107,7 +107,7 @@ impl LazyStaticType {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_or_init<T: PyClass>(&self, py: Python) -> *mut ffi::PyTypeObject {
|
||||
pub fn get_or_init<T: PyClass>(&self, py: Python<'_>) -> *mut ffi::PyTypeObject {
|
||||
let type_object = *self.value.get_or_init(py, || create_type_object::<T>(py));
|
||||
self.ensure_init(py, type_object, T::NAME, &T::for_all_items);
|
||||
type_object
|
||||
|
@ -115,7 +115,7 @@ impl LazyStaticType {
|
|||
|
||||
fn ensure_init(
|
||||
&self,
|
||||
py: Python,
|
||||
py: Python<'_>,
|
||||
type_object: *mut ffi::PyTypeObject,
|
||||
name: &str,
|
||||
for_all_items: &dyn Fn(&mut dyn FnMut(&PyClassItems)),
|
||||
|
@ -188,7 +188,7 @@ impl LazyStaticType {
|
|||
}
|
||||
|
||||
fn initialize_tp_dict(
|
||||
py: Python,
|
||||
py: Python<'_>,
|
||||
type_object: *mut ffi::PyObject,
|
||||
items: Vec<(&'static std::ffi::CStr, PyObject)>,
|
||||
) -> PyResult<()> {
|
||||
|
|
|
@ -80,7 +80,7 @@ impl PyAny {
|
|||
/// assert!(any.downcast::<PyList>().is_err());
|
||||
/// });
|
||||
/// ```
|
||||
pub fn downcast<T>(&self) -> Result<&T, PyDowncastError>
|
||||
pub fn downcast<T>(&self) -> Result<&T, PyDowncastError<'_>>
|
||||
where
|
||||
for<'py> T: PyTryFrom<'py>,
|
||||
{
|
||||
|
@ -658,11 +658,11 @@ impl PyAny {
|
|||
/// Casts the PyObject to a concrete Python object type.
|
||||
///
|
||||
/// This can cast only to native Python types, not types implemented in Rust.
|
||||
pub fn cast_as<'a, D>(&'a self) -> Result<&'a D, PyDowncastError>
|
||||
pub fn cast_as<'a, D>(&'a self) -> Result<&'a D, PyDowncastError<'_>>
|
||||
where
|
||||
D: PyTryFrom<'a>,
|
||||
{
|
||||
D::try_from(self)
|
||||
<D as PyTryFrom<'_>>::try_from(self)
|
||||
}
|
||||
|
||||
/// Extracts some type from the Python object.
|
||||
|
|
|
@ -13,7 +13,7 @@ pyobject_native_type!(PyBool, ffi::PyObject, ffi::PyBool_Type, #checkfunction=ff
|
|||
impl PyBool {
|
||||
/// Depending on `val`, returns `true` or `false`.
|
||||
#[inline]
|
||||
pub fn new(py: Python, val: bool) -> &PyBool {
|
||||
pub fn new(py: Python<'_>, val: bool) -> &PyBool {
|
||||
unsafe { py.from_borrowed_ptr(if val { ffi::Py_True() } else { ffi::Py_False() }) }
|
||||
}
|
||||
|
||||
|
@ -27,7 +27,7 @@ impl PyBool {
|
|||
/// Converts a Rust `bool` to a Python `bool`.
|
||||
impl ToPyObject for bool {
|
||||
#[inline]
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
unsafe {
|
||||
PyObject::from_borrowed_ptr(
|
||||
py,
|
||||
|
@ -43,7 +43,7 @@ impl ToPyObject for bool {
|
|||
|
||||
impl IntoPy<PyObject> for bool {
|
||||
#[inline]
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
PyBool::new(py, self).into()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,7 +44,7 @@ impl PyByteArray {
|
|||
/// })
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn new_with<F>(py: Python, len: usize, init: F) -> PyResult<&PyByteArray>
|
||||
pub fn new_with<F>(py: Python<'_>, len: usize, init: F) -> PyResult<&PyByteArray>
|
||||
where
|
||||
F: FnOnce(&mut [u8]) -> PyResult<()>,
|
||||
{
|
||||
|
|
|
@ -50,7 +50,7 @@ impl PyBytes {
|
|||
/// })
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn new_with<F>(py: Python, len: usize, init: F) -> PyResult<&PyBytes>
|
||||
pub fn new_with<F>(py: Python<'_>, len: usize, init: F) -> PyResult<&PyBytes>
|
||||
where
|
||||
F: FnOnce(&mut [u8]) -> PyResult<()>,
|
||||
{
|
||||
|
@ -125,7 +125,7 @@ impl<I: SliceIndex<[u8]>> Index<I> for PyBytes {
|
|||
}
|
||||
|
||||
impl<'a> IntoPy<PyObject> for &'a [u8] {
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
PyBytes::new(py, self).to_object(py)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -167,7 +167,7 @@ impl PyCapsule {
|
|||
/// assert_eq!(rx.recv(), Ok("Destructor called!".to_string()));
|
||||
/// ```
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
pub fn set_context(&self, py: Python, context: *mut c_void) -> PyResult<()> {
|
||||
pub fn set_context(&self, py: Python<'_>, context: *mut c_void) -> PyResult<()> {
|
||||
let result = unsafe { ffi::PyCapsule_SetContext(self.as_ptr(), context) as u8 };
|
||||
if result != 0 {
|
||||
Err(PyErr::fetch(py))
|
||||
|
@ -178,7 +178,7 @@ impl PyCapsule {
|
|||
|
||||
/// Gets the current context stored in the capsule. If there is no context, the pointer
|
||||
/// will be null.
|
||||
pub fn get_context(&self, py: Python) -> PyResult<*mut c_void> {
|
||||
pub fn get_context(&self, py: Python<'_>) -> PyResult<*mut c_void> {
|
||||
let ctx = unsafe { ffi::PyCapsule_GetContext(self.as_ptr()) };
|
||||
if ctx.is_null() && self.is_valid() && PyErr::occurred(py) {
|
||||
Err(PyErr::fetch(py))
|
||||
|
|
|
@ -0,0 +1,584 @@
|
|||
// Copyright (c) 2017-present PyO3 Project and Contributors
|
||||
|
||||
//! Defines conversions between Rust and Python types.
|
||||
use crate::err::{self, PyDowncastError, PyResult};
|
||||
use crate::type_object::PyTypeInfo;
|
||||
use crate::types::PyTuple;
|
||||
use crate::{
|
||||
ffi, gil, Py, PyAny, PyCell, PyClass, PyNativeType, PyObject, PyRef, PyRefMut, Python,
|
||||
};
|
||||
use std::ptr::NonNull;
|
||||
|
||||
/// This trait represents that **we can do zero-cost conversion from the object
|
||||
/// to a FFI pointer**.
|
||||
///
|
||||
/// This trait is implemented for types that internally wrap a pointer to a Python object.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use pyo3::{prelude::*, AsPyPointer};
|
||||
/// Python::with_gil(|py| {
|
||||
/// let dict = pyo3::types::PyDict::new(py);
|
||||
/// // All native object wrappers implement AsPyPointer!!!
|
||||
/// assert_ne!(dict.as_ptr(), std::ptr::null_mut());
|
||||
/// });
|
||||
/// ```
|
||||
pub trait AsPyPointer {
|
||||
/// Retrieves the underlying FFI pointer (as a borrowed pointer).
|
||||
fn as_ptr(&self) -> *mut ffi::PyObject;
|
||||
}
|
||||
|
||||
/// This trait allows retrieving the underlying FFI pointer from Python objects.
|
||||
pub trait IntoPyPointer {
|
||||
/// Retrieves the underlying FFI pointer. Whether pointer owned or borrowed
|
||||
/// depends on implementation.
|
||||
fn into_ptr(self) -> *mut ffi::PyObject;
|
||||
}
|
||||
|
||||
/// Convert `None` into a null pointer.
|
||||
impl<T> AsPyPointer for Option<T>
|
||||
where
|
||||
T: AsPyPointer,
|
||||
{
|
||||
#[inline]
|
||||
fn as_ptr(&self) -> *mut ffi::PyObject {
|
||||
self.as_ref()
|
||||
.map_or_else(std::ptr::null_mut, |t| t.as_ptr())
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert `None` into a null pointer.
|
||||
impl<T> IntoPyPointer for Option<T>
|
||||
where
|
||||
T: IntoPyPointer,
|
||||
{
|
||||
#[inline]
|
||||
fn into_ptr(self) -> *mut ffi::PyObject {
|
||||
self.map_or_else(std::ptr::null_mut, |t| t.into_ptr())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> IntoPyPointer for &'a T
|
||||
where
|
||||
T: AsPyPointer,
|
||||
{
|
||||
fn into_ptr(self) -> *mut ffi::PyObject {
|
||||
unsafe { ffi::_Py_XNewRef(self.as_ptr()) }
|
||||
}
|
||||
}
|
||||
|
||||
/// Conversion trait that allows various objects to be converted into `PyObject`.
|
||||
pub trait ToPyObject {
|
||||
/// Converts self into a Python object.
|
||||
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()]
|
||||
pub trait ToBorrowedObject: ToPyObject {
|
||||
/// Converts self into a Python object and calls the specified closure
|
||||
/// on the native FFI pointer underlying the Python object.
|
||||
///
|
||||
/// May be more efficient than `to_object` because it does not need
|
||||
/// to touch any reference counts when the input object already is a Python object.
|
||||
fn with_borrowed_ptr<F, R>(&self, py: Python<'_>, f: F) -> R
|
||||
where
|
||||
F: FnOnce(*mut ffi::PyObject) -> R,
|
||||
{
|
||||
let ptr = self.to_object(py).into_ptr();
|
||||
let result = f(ptr);
|
||||
unsafe {
|
||||
ffi::Py_XDECREF(ptr);
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ToBorrowedObject for T where T: ToPyObject {}
|
||||
|
||||
/// Defines a conversion from a Rust type to a Python object.
|
||||
///
|
||||
/// It functions similarly to std's [`Into`](std::convert::Into) trait,
|
||||
/// but requires a [GIL token](Python) as an argument.
|
||||
/// Many functions and traits internal to PyO3 require this trait as a bound,
|
||||
/// so a lack of this trait can manifest itself in different error messages.
|
||||
///
|
||||
/// # Examples
|
||||
/// ## With `#[pyclass]`
|
||||
/// The easiest way to implement `IntoPy` is by exposing a struct as a native Python object
|
||||
/// by annotating it with [`#[pyclass]`](crate::prelude::pyclass).
|
||||
///
|
||||
/// ```rust
|
||||
/// use pyo3::prelude::*;
|
||||
///
|
||||
/// #[pyclass]
|
||||
/// struct Number {
|
||||
/// #[pyo3(get, set)]
|
||||
/// value: i32,
|
||||
/// }
|
||||
/// ```
|
||||
/// Python code will see this as an instance of the `Number` class with a `value` attribute.
|
||||
///
|
||||
/// ## Conversion to a Python object
|
||||
///
|
||||
/// However, it may not be desirable to expose the existence of `Number` to Python code.
|
||||
/// `IntoPy` allows us to define a conversion to an appropriate Python object.
|
||||
/// ```rust
|
||||
/// use pyo3::prelude::*;
|
||||
///
|
||||
/// struct Number {
|
||||
/// value: i32,
|
||||
/// }
|
||||
///
|
||||
/// impl IntoPy<PyObject> for Number {
|
||||
/// fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
/// // delegates to i32's IntoPy implementation.
|
||||
/// self.value.into_py(py)
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
/// Python code will see this as an `int` object.
|
||||
///
|
||||
/// ## Dynamic conversion into Python objects.
|
||||
/// It is also possible to return a different Python object depending on some condition.
|
||||
/// This is useful for types like enums that can carry different types.
|
||||
///
|
||||
/// ```rust
|
||||
/// use pyo3::prelude::*;
|
||||
///
|
||||
/// enum Value {
|
||||
/// Integer(i32),
|
||||
/// String(String),
|
||||
/// None,
|
||||
/// }
|
||||
///
|
||||
/// impl IntoPy<PyObject> for Value {
|
||||
/// fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
/// match self {
|
||||
/// Self::Integer(val) => val.into_py(py),
|
||||
/// Self::String(val) => val.into_py(py),
|
||||
/// Self::None => py.None(),
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// # fn main() {
|
||||
/// # Python::with_gil(|py| {
|
||||
/// # let v = Value::Integer(73).into_py(py);
|
||||
/// # let v = v.extract::<i32>(py).unwrap();
|
||||
/// #
|
||||
/// # let v = Value::String("foo".into()).into_py(py);
|
||||
/// # let v = v.extract::<String>(py).unwrap();
|
||||
/// #
|
||||
/// # let v = Value::None.into_py(py);
|
||||
/// # let v = v.extract::<Option<Vec<i32>>>(py).unwrap();
|
||||
/// # });
|
||||
/// # }
|
||||
/// ```
|
||||
/// Python code will see this as any of the `int`, `string` or `None` objects.
|
||||
#[cfg_attr(docsrs, doc(alias = "IntoPyCallbackOutput"))]
|
||||
pub trait IntoPy<T>: Sized {
|
||||
/// Performs the conversion.
|
||||
fn into_py(self, py: Python<'_>) -> T;
|
||||
}
|
||||
|
||||
/// `FromPyObject` is implemented by various types that can be extracted from
|
||||
/// a Python object reference.
|
||||
///
|
||||
/// Normal usage is through the helper methods `Py::extract` or `PyAny::extract`:
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// let obj: Py<PyAny> = ...;
|
||||
/// let value: &TargetType = obj.extract(py)?;
|
||||
///
|
||||
/// let any: &PyAny = ...;
|
||||
/// let value: &TargetType = any.extract()?;
|
||||
/// ```
|
||||
///
|
||||
/// Note: depending on the implementation, the lifetime of the extracted result may
|
||||
/// depend on the lifetime of the `obj` or the `prepared` variable.
|
||||
///
|
||||
/// For example, when extracting `&str` from a Python byte string, the resulting string slice will
|
||||
/// point to the existing string data (lifetime: `'source`).
|
||||
/// On the other hand, when extracting `&str` from a Python Unicode string, the preparation step
|
||||
/// will convert the string to UTF-8, and the resulting string slice will have lifetime `'prepared`.
|
||||
/// Since which case applies depends on the runtime type of the Python object,
|
||||
/// both the `obj` and `prepared` variables must outlive the resulting string slice.
|
||||
///
|
||||
/// The trait's conversion method takes a `&PyAny` argument but is called
|
||||
/// `FromPyObject` for historical reasons.
|
||||
pub trait FromPyObject<'source>: Sized {
|
||||
/// Extracts `Self` from the source `PyObject`.
|
||||
fn extract(ob: &'source PyAny) -> PyResult<Self>;
|
||||
}
|
||||
|
||||
/// Identity conversion: allows using existing `PyObject` instances where
|
||||
/// `T: ToPyObject` is expected.
|
||||
impl<T: ?Sized + ToPyObject> ToPyObject for &'_ T {
|
||||
#[inline]
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
<T as ToPyObject>::to_object(*self, py)
|
||||
}
|
||||
}
|
||||
|
||||
/// `Option::Some<T>` is converted like `T`.
|
||||
/// `Option::None` is converted to Python `None`.
|
||||
impl<T> ToPyObject for Option<T>
|
||||
where
|
||||
T: ToPyObject,
|
||||
{
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
self.as_ref()
|
||||
.map_or_else(|| py.None(), |val| val.to_object(py))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> IntoPy<PyObject> for Option<T>
|
||||
where
|
||||
T: IntoPy<PyObject>,
|
||||
{
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
self.map_or_else(|| py.None(), |val| val.into_py(py))
|
||||
}
|
||||
}
|
||||
|
||||
/// `()` is converted to Python `None`.
|
||||
impl ToPyObject for () {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
py.None()
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPy<PyObject> for () {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
py.None()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> IntoPy<PyObject> for &'_ T
|
||||
where
|
||||
T: AsPyPointer,
|
||||
{
|
||||
#[inline]
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
unsafe { PyObject::from_borrowed_ptr(py, self.as_ptr()) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> FromPyObject<'a> for &'a PyCell<T>
|
||||
where
|
||||
T: PyClass,
|
||||
{
|
||||
fn extract(obj: &'a PyAny) -> PyResult<Self> {
|
||||
PyTryFrom::try_from(obj).map_err(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> FromPyObject<'a> for T
|
||||
where
|
||||
T: PyClass + Clone,
|
||||
{
|
||||
fn extract(obj: &'a PyAny) -> PyResult<Self> {
|
||||
let cell: &PyCell<Self> = PyTryFrom::try_from(obj)?;
|
||||
Ok(unsafe { cell.try_borrow_unguarded()?.clone() })
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> FromPyObject<'a> for PyRef<'a, T>
|
||||
where
|
||||
T: PyClass,
|
||||
{
|
||||
fn extract(obj: &'a PyAny) -> PyResult<Self> {
|
||||
let cell: &PyCell<T> = PyTryFrom::try_from(obj)?;
|
||||
cell.try_borrow().map_err(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> FromPyObject<'a> for PyRefMut<'a, T>
|
||||
where
|
||||
T: PyClass,
|
||||
{
|
||||
fn extract(obj: &'a PyAny) -> PyResult<Self> {
|
||||
let cell: &PyCell<T> = PyTryFrom::try_from(obj)?;
|
||||
cell.try_borrow_mut().map_err(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> FromPyObject<'a> for Option<T>
|
||||
where
|
||||
T: FromPyObject<'a>,
|
||||
{
|
||||
fn extract(obj: &'a PyAny) -> PyResult<Self> {
|
||||
if obj.as_ptr() == unsafe { ffi::Py_None() } {
|
||||
Ok(None)
|
||||
} else {
|
||||
T::extract(obj).map(Some)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait implemented by Python object types that allow a checked downcast.
|
||||
/// If `T` implements `PyTryFrom`, we can convert `&PyAny` to `&T`.
|
||||
///
|
||||
/// This trait is similar to `std::convert::TryFrom`
|
||||
pub trait PyTryFrom<'v>: Sized + PyNativeType {
|
||||
/// Cast from a concrete Python object type to PyObject.
|
||||
fn try_from<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, PyDowncastError<'v>>;
|
||||
|
||||
/// Cast from a concrete Python object type to PyObject. With exact type check.
|
||||
fn try_from_exact<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, PyDowncastError<'v>>;
|
||||
|
||||
/// Cast a PyAny to a specific type of PyObject. The caller must
|
||||
/// have already verified the reference is for this type.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Callers must ensure that the type is valid or risk type confusion.
|
||||
unsafe fn try_from_unchecked<V: Into<&'v PyAny>>(value: V) -> &'v Self;
|
||||
}
|
||||
|
||||
/// Trait implemented by Python object types that allow a checked downcast.
|
||||
/// This trait is similar to `std::convert::TryInto`
|
||||
pub trait PyTryInto<T>: Sized {
|
||||
/// Cast from PyObject to a concrete Python object type.
|
||||
fn try_into(&self) -> Result<&T, PyDowncastError<'_>>;
|
||||
|
||||
/// Cast from PyObject to a concrete Python object type. With exact type check.
|
||||
fn try_into_exact(&self) -> Result<&T, PyDowncastError<'_>>;
|
||||
}
|
||||
|
||||
// TryFrom implies TryInto
|
||||
impl<U> PyTryInto<U> for PyAny
|
||||
where
|
||||
U: for<'v> PyTryFrom<'v>,
|
||||
{
|
||||
fn try_into(&self) -> Result<&U, PyDowncastError<'_>> {
|
||||
<U as PyTryFrom<'_>>::try_from(self)
|
||||
}
|
||||
fn try_into_exact(&self) -> Result<&U, PyDowncastError<'_>> {
|
||||
U::try_from_exact(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'v, T> PyTryFrom<'v> for T
|
||||
where
|
||||
T: PyTypeInfo + PyNativeType,
|
||||
{
|
||||
fn try_from<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, PyDowncastError<'v>> {
|
||||
let value = value.into();
|
||||
unsafe {
|
||||
if T::is_type_of(value) {
|
||||
Ok(Self::try_from_unchecked(value))
|
||||
} else {
|
||||
Err(PyDowncastError::new(value, T::NAME))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn try_from_exact<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, PyDowncastError<'v>> {
|
||||
let value = value.into();
|
||||
unsafe {
|
||||
if T::is_exact_type_of(value) {
|
||||
Ok(Self::try_from_unchecked(value))
|
||||
} else {
|
||||
Err(PyDowncastError::new(value, T::NAME))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn try_from_unchecked<V: Into<&'v PyAny>>(value: V) -> &'v Self {
|
||||
Self::unchecked_downcast(value.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'v, T> PyTryFrom<'v> for PyCell<T>
|
||||
where
|
||||
T: 'v + PyClass,
|
||||
{
|
||||
fn try_from<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, PyDowncastError<'v>> {
|
||||
let value = value.into();
|
||||
unsafe {
|
||||
if T::is_type_of(value) {
|
||||
Ok(Self::try_from_unchecked(value))
|
||||
} else {
|
||||
Err(PyDowncastError::new(value, T::NAME))
|
||||
}
|
||||
}
|
||||
}
|
||||
fn try_from_exact<V: Into<&'v PyAny>>(value: V) -> Result<&'v Self, PyDowncastError<'v>> {
|
||||
let value = value.into();
|
||||
unsafe {
|
||||
if T::is_exact_type_of(value) {
|
||||
Ok(Self::try_from_unchecked(value))
|
||||
} else {
|
||||
Err(PyDowncastError::new(value, T::NAME))
|
||||
}
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
unsafe fn try_from_unchecked<V: Into<&'v PyAny>>(value: V) -> &'v Self {
|
||||
Self::unchecked_downcast(value.into())
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts `()` to an empty Python tuple.
|
||||
impl IntoPy<Py<PyTuple>> for () {
|
||||
fn into_py(self, py: Python<'_>) -> Py<PyTuple> {
|
||||
PyTuple::empty(py).into()
|
||||
}
|
||||
}
|
||||
|
||||
/// Raw level conversion between `*mut ffi::PyObject` and PyO3 types.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// See safety notes on individual functions.
|
||||
pub unsafe trait FromPyPointer<'p>: Sized {
|
||||
/// Convert from an arbitrary `PyObject`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Implementations must ensure the object does not get freed during `'p`
|
||||
/// and ensure that `ptr` is of the correct type.
|
||||
/// Note that it must be safe to decrement the reference count of `ptr`.
|
||||
unsafe fn from_owned_ptr_or_opt(py: Python<'p>, ptr: *mut ffi::PyObject) -> Option<&'p Self>;
|
||||
/// Convert from an arbitrary `PyObject` or panic.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Relies on [`from_owned_ptr_or_opt`](#method.from_owned_ptr_or_opt).
|
||||
unsafe fn from_owned_ptr_or_panic(py: Python<'p>, ptr: *mut ffi::PyObject) -> &'p Self {
|
||||
Self::from_owned_ptr_or_opt(py, ptr).unwrap_or_else(|| err::panic_after_error(py))
|
||||
}
|
||||
/// Convert from an arbitrary `PyObject` or panic.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Relies on [`from_owned_ptr_or_opt`](#method.from_owned_ptr_or_opt).
|
||||
unsafe fn from_owned_ptr(py: Python<'p>, ptr: *mut ffi::PyObject) -> &'p Self {
|
||||
Self::from_owned_ptr_or_panic(py, ptr)
|
||||
}
|
||||
/// Convert from an arbitrary `PyObject`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Relies on [`from_owned_ptr_or_opt`](#method.from_owned_ptr_or_opt).
|
||||
unsafe fn from_owned_ptr_or_err(py: Python<'p>, ptr: *mut ffi::PyObject) -> PyResult<&'p Self> {
|
||||
Self::from_owned_ptr_or_opt(py, ptr).ok_or_else(|| err::PyErr::fetch(py))
|
||||
}
|
||||
/// Convert from an arbitrary borrowed `PyObject`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Implementations must ensure the object does not get freed during `'p` and avoid type confusion.
|
||||
unsafe fn from_borrowed_ptr_or_opt(py: Python<'p>, ptr: *mut ffi::PyObject)
|
||||
-> Option<&'p Self>;
|
||||
/// Convert from an arbitrary borrowed `PyObject`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Relies on unsafe fn [`from_borrowed_ptr_or_opt`](#method.from_borrowed_ptr_or_opt).
|
||||
unsafe fn from_borrowed_ptr_or_panic(py: Python<'p>, ptr: *mut ffi::PyObject) -> &'p Self {
|
||||
Self::from_borrowed_ptr_or_opt(py, ptr).unwrap_or_else(|| err::panic_after_error(py))
|
||||
}
|
||||
/// Convert from an arbitrary borrowed `PyObject`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Relies on unsafe fn [`from_borrowed_ptr_or_opt`](#method.from_borrowed_ptr_or_opt).
|
||||
unsafe fn from_borrowed_ptr(py: Python<'p>, ptr: *mut ffi::PyObject) -> &'p Self {
|
||||
Self::from_borrowed_ptr_or_panic(py, ptr)
|
||||
}
|
||||
/// Convert from an arbitrary borrowed `PyObject`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Relies on unsafe fn [`from_borrowed_ptr_or_opt`](#method.from_borrowed_ptr_or_opt).
|
||||
unsafe fn from_borrowed_ptr_or_err(
|
||||
py: Python<'p>,
|
||||
ptr: *mut ffi::PyObject,
|
||||
) -> PyResult<&'p Self> {
|
||||
Self::from_borrowed_ptr_or_opt(py, ptr).ok_or_else(|| err::PyErr::fetch(py))
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl<'p, T> FromPyPointer<'p> for T
|
||||
where
|
||||
T: 'p + crate::PyNativeType,
|
||||
{
|
||||
unsafe fn from_owned_ptr_or_opt(py: Python<'p>, ptr: *mut ffi::PyObject) -> Option<&'p Self> {
|
||||
gil::register_owned(py, NonNull::new(ptr)?);
|
||||
Some(&*(ptr as *mut Self))
|
||||
}
|
||||
unsafe fn from_borrowed_ptr_or_opt(
|
||||
_py: Python<'p>,
|
||||
ptr: *mut ffi::PyObject,
|
||||
) -> Option<&'p Self> {
|
||||
NonNull::new(ptr as *mut Self).map(|p| &*p.as_ptr())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::types::{IntoPyDict, PyAny, PyDict, PyList};
|
||||
use crate::{AsPyPointer, PyObject, Python, ToPyObject};
|
||||
|
||||
use super::PyTryFrom;
|
||||
|
||||
#[test]
|
||||
fn test_try_from() {
|
||||
Python::with_gil(|py| {
|
||||
let list: &PyAny = vec![3, 6, 5, 4, 7].to_object(py).into_ref(py);
|
||||
let dict: &PyAny = vec![("reverse", true)].into_py_dict(py).as_ref();
|
||||
|
||||
assert!(<PyDict as PyTryFrom<'_>>::try_from(list).is_ok());
|
||||
assert!(<PyDict as PyTryFrom<'_>>::try_from(dict).is_ok());
|
||||
|
||||
assert!(<PyAny as PyTryFrom<'_>>::try_from(list).is_ok());
|
||||
assert!(<PyAny as PyTryFrom<'_>>::try_from(dict).is_ok());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_try_from_exact() {
|
||||
Python::with_gil(|py| {
|
||||
let list: &PyAny = vec![3, 6, 5, 4, 7].to_object(py).into_ref(py);
|
||||
let dict: &PyAny = vec![("reverse", true)].into_py_dict(py).as_ref();
|
||||
|
||||
assert!(PyList::try_from_exact(list).is_ok());
|
||||
assert!(PyDict::try_from_exact(dict).is_ok());
|
||||
|
||||
assert!(PyAny::try_from_exact(list).is_err());
|
||||
assert!(PyAny::try_from_exact(dict).is_err());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_try_from_unchecked() {
|
||||
Python::with_gil(|py| {
|
||||
let list = PyList::new(py, &[1, 2, 3]);
|
||||
let val = unsafe { <PyList as PyTryFrom>::try_from_unchecked(list.as_ref()) };
|
||||
assert!(list.is(val));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_option_as_ptr() {
|
||||
Python::with_gil(|py| {
|
||||
let mut option: Option<PyObject> = None;
|
||||
assert_eq!(option.as_ptr(), std::ptr::null_mut());
|
||||
|
||||
let none = py.None();
|
||||
option = Some(none.clone());
|
||||
|
||||
let ref_cnt = none.get_refcnt(py);
|
||||
assert_eq!(option.as_ptr(), none.as_ptr());
|
||||
|
||||
// Ensure ref count not changed by as_ptr call
|
||||
assert_eq!(none.get_refcnt(py), ref_cnt);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -20,7 +20,7 @@ pyobject_native_type!(
|
|||
|
||||
impl PyComplex {
|
||||
/// Creates a new `PyComplex` from the given real and imaginary values.
|
||||
pub fn from_doubles(py: Python, real: c_double, imag: c_double) -> &PyComplex {
|
||||
pub fn from_doubles(py: Python<'_>, real: c_double, imag: c_double) -> &PyComplex {
|
||||
unsafe {
|
||||
let ptr = ffi::PyComplex_FromDoubles(real, imag);
|
||||
py.from_owned_ptr(ptr)
|
||||
|
|
|
@ -26,7 +26,7 @@ use crate::types::PyTuple;
|
|||
use crate::{AsPyPointer, PyAny, PyObject, Python, ToPyObject};
|
||||
use std::os::raw::c_int;
|
||||
|
||||
fn ensure_datetime_api(_py: Python) -> &'static PyDateTime_CAPI {
|
||||
fn ensure_datetime_api(_py: Python<'_>) -> &'static PyDateTime_CAPI {
|
||||
unsafe {
|
||||
if pyo3_ffi::PyDateTimeAPI().is_null() {
|
||||
PyDateTime_IMPORT()
|
||||
|
@ -173,7 +173,7 @@ pyobject_native_type!(
|
|||
|
||||
impl PyDate {
|
||||
/// Creates a new `datetime.date`.
|
||||
pub fn new(py: Python, year: i32, month: u8, day: u8) -> PyResult<&PyDate> {
|
||||
pub fn new(py: Python<'_>, year: i32, month: u8, day: u8) -> PyResult<&PyDate> {
|
||||
unsafe {
|
||||
let ptr = (ensure_datetime_api(py).Date_FromDate)(
|
||||
year,
|
||||
|
@ -188,7 +188,7 @@ impl PyDate {
|
|||
/// Construct a `datetime.date` from a POSIX timestamp
|
||||
///
|
||||
/// This is equivalent to `datetime.date.fromtimestamp`
|
||||
pub fn from_timestamp(py: Python, timestamp: i64) -> PyResult<&PyDate> {
|
||||
pub fn from_timestamp(py: Python<'_>, timestamp: i64) -> PyResult<&PyDate> {
|
||||
let time_tuple = PyTuple::new(py, &[timestamp]);
|
||||
|
||||
// safety ensure that the API is loaded
|
||||
|
@ -466,7 +466,7 @@ pyobject_native_type!(
|
|||
impl PyDelta {
|
||||
/// Creates a new `timedelta`.
|
||||
pub fn new(
|
||||
py: Python,
|
||||
py: Python<'_>,
|
||||
days: i32,
|
||||
seconds: i32,
|
||||
microseconds: i32,
|
||||
|
@ -501,7 +501,7 @@ impl PyDeltaAccess for PyDelta {
|
|||
}
|
||||
|
||||
// Utility function
|
||||
fn opt_to_pyobj(py: Python, opt: Option<&PyObject>) -> *mut ffi::PyObject {
|
||||
fn opt_to_pyobj(py: Python<'_>, opt: Option<&PyObject>) -> *mut ffi::PyObject {
|
||||
// Convenience function for unpacking Options to either an Object or None
|
||||
match opt {
|
||||
Some(tzi) => tzi.as_ptr(),
|
||||
|
|
|
@ -27,7 +27,7 @@ pyobject_native_type!(
|
|||
|
||||
impl PyDict {
|
||||
/// Creates a new empty dictionary.
|
||||
pub fn new(py: Python) -> &PyDict {
|
||||
pub fn new(py: Python<'_>) -> &PyDict {
|
||||
unsafe { py.from_owned_ptr::<PyDict>(ffi::PyDict_New()) }
|
||||
}
|
||||
|
||||
|
@ -39,7 +39,7 @@ impl PyDict {
|
|||
/// Returns an error on invalid input. In the case of key collisions,
|
||||
/// this keeps the last entry seen.
|
||||
#[cfg(not(PyPy))]
|
||||
pub fn from_sequence(py: Python, seq: PyObject) -> PyResult<&PyDict> {
|
||||
pub fn from_sequence(py: Python<'_>, seq: PyObject) -> PyResult<&PyDict> {
|
||||
unsafe {
|
||||
let dict = py.from_owned_ptr::<PyDict>(ffi::PyDict_New());
|
||||
err::error_on_minusone(
|
||||
|
@ -172,7 +172,7 @@ impl PyDict {
|
|||
///
|
||||
/// Note that it's unsafe to use when the dictionary might be changed by
|
||||
/// other code.
|
||||
pub fn iter(&self) -> PyDictIterator {
|
||||
pub fn iter(&self) -> PyDictIterator<'_> {
|
||||
PyDictIterator {
|
||||
dict: self.as_ref(),
|
||||
pos: 0,
|
||||
|
@ -236,7 +236,7 @@ where
|
|||
V: ToPyObject,
|
||||
H: hash::BuildHasher,
|
||||
{
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
IntoPyDict::into_py_dict(self, py).into()
|
||||
}
|
||||
}
|
||||
|
@ -246,7 +246,7 @@ where
|
|||
K: cmp::Eq + ToPyObject,
|
||||
V: ToPyObject,
|
||||
{
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
IntoPyDict::into_py_dict(self, py).into()
|
||||
}
|
||||
}
|
||||
|
@ -257,7 +257,7 @@ where
|
|||
V: IntoPy<PyObject>,
|
||||
H: hash::BuildHasher,
|
||||
{
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
let iter = self
|
||||
.into_iter()
|
||||
.map(|(k, v)| (k.into_py(py), v.into_py(py)));
|
||||
|
@ -270,7 +270,7 @@ where
|
|||
K: cmp::Eq + IntoPy<PyObject>,
|
||||
V: IntoPy<PyObject>,
|
||||
{
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
let iter = self
|
||||
.into_iter()
|
||||
.map(|(k, v)| (k.into_py(py), v.into_py(py)));
|
||||
|
@ -283,7 +283,7 @@ where
|
|||
pub trait IntoPyDict {
|
||||
/// Converts self into a `PyDict` object pointer. Whether pointer owned or borrowed
|
||||
/// depends on implementation.
|
||||
fn into_py_dict(self, py: Python) -> &PyDict;
|
||||
fn into_py_dict(self, py: Python<'_>) -> &PyDict;
|
||||
}
|
||||
|
||||
impl<T, I> IntoPyDict for I
|
||||
|
@ -291,7 +291,7 @@ where
|
|||
T: PyDictItem,
|
||||
I: IntoIterator<Item = T>,
|
||||
{
|
||||
fn into_py_dict(self, py: Python) -> &PyDict {
|
||||
fn into_py_dict(self, py: Python<'_>) -> &PyDict {
|
||||
let dict = PyDict::new(py);
|
||||
for item in self {
|
||||
dict.set_item(item.key(), item.value())
|
||||
|
|
|
@ -35,13 +35,13 @@ impl PyFloat {
|
|||
}
|
||||
|
||||
impl ToPyObject for f64 {
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
PyFloat::new(py, *self).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPy<PyObject> for f64 {
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
PyFloat::new(py, self).into()
|
||||
}
|
||||
}
|
||||
|
@ -63,13 +63,13 @@ impl<'source> FromPyObject<'source> for f64 {
|
|||
}
|
||||
|
||||
impl ToPyObject for f32 {
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
PyFloat::new(py, f64::from(*self)).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPy<PyObject> for f32 {
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
PyFloat::new(py, f64::from(self)).into()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -104,7 +104,7 @@ impl PyCFunction {
|
|||
/// py_run!(py, add_one, "assert add_one(42) == 43");
|
||||
/// });
|
||||
/// ```
|
||||
pub fn new_closure<F, R>(f: F, py: Python) -> PyResult<&PyCFunction>
|
||||
pub fn new_closure<F, R>(f: F, py: Python<'_>) -> PyResult<&PyCFunction>
|
||||
where
|
||||
F: Fn(&types::PyTuple, Option<&types::PyDict>) -> R + Send + 'static,
|
||||
R: crate::callback::IntoPyCallbackOutput<*mut ffi::PyObject>,
|
||||
|
@ -131,7 +131,7 @@ impl PyCFunction {
|
|||
#[doc(hidden)]
|
||||
fn internal_new_from_pointers(
|
||||
method_def: PyMethodDef,
|
||||
py: Python,
|
||||
py: Python<'_>,
|
||||
mod_ptr: *mut ffi::PyObject,
|
||||
module_name: *mut ffi::PyObject,
|
||||
) -> PyResult<&Self> {
|
||||
|
@ -150,7 +150,7 @@ impl PyCFunction {
|
|||
#[doc(hidden)]
|
||||
pub fn internal_new(
|
||||
method_def: PyMethodDef,
|
||||
py_or_module: PyFunctionArguments,
|
||||
py_or_module: PyFunctionArguments<'_>,
|
||||
) -> PyResult<&Self> {
|
||||
let (py, module) = py_or_module.into_py_and_maybe_module();
|
||||
let (mod_ptr, module_name) = if let Some(m) = module {
|
||||
|
|
|
@ -99,7 +99,7 @@ impl Py<PyIterator> {
|
|||
/// Similar to [`as_ref`](#method.as_ref), and also consumes this `Py` and registers the
|
||||
/// Python object reference in PyO3's object storage. The reference count for the Python
|
||||
/// object will not be decreased until the GIL lifetime ends.
|
||||
pub fn into_ref(self, py: Python) -> &PyIterator {
|
||||
pub fn into_ref(self, py: Python<'_>) -> &PyIterator {
|
||||
unsafe { py.from_owned_ptr(self.into_ptr()) }
|
||||
}
|
||||
}
|
||||
|
@ -212,7 +212,8 @@ def fibonacci(target):
|
|||
fn iterator_try_from() {
|
||||
Python::with_gil(|py| {
|
||||
let obj: Py<PyAny> = vec![10, 20].to_object(py).as_ref(py).iter().unwrap().into();
|
||||
let iter: &PyIterator = PyIterator::try_from(obj.as_ref(py)).unwrap();
|
||||
let iter: &PyIterator =
|
||||
<PyIterator as PyTryFrom<'_>>::try_from(obj.as_ref(py)).unwrap();
|
||||
assert!(obj.is(iter));
|
||||
});
|
||||
}
|
||||
|
|
|
@ -21,7 +21,10 @@ pyobject_native_type_core!(PyList, ffi::PyList_Type, #checkfunction=ffi::PyList_
|
|||
|
||||
#[inline]
|
||||
#[track_caller]
|
||||
fn new_from_iter(py: Python, elements: &mut dyn ExactSizeIterator<Item = PyObject>) -> Py<PyList> {
|
||||
fn new_from_iter(
|
||||
py: Python<'_>,
|
||||
elements: &mut dyn ExactSizeIterator<Item = PyObject>,
|
||||
) -> Py<PyList> {
|
||||
unsafe {
|
||||
// PyList_New checks for overflow but has a bad error message, so we check ourselves
|
||||
let len: Py_ssize_t = elements
|
||||
|
@ -90,7 +93,7 @@ impl PyList {
|
|||
}
|
||||
|
||||
/// Constructs a new empty list.
|
||||
pub fn empty(py: Python) -> &PyList {
|
||||
pub fn empty(py: Python<'_>) -> &PyList {
|
||||
unsafe { py.from_owned_ptr::<PyList>(ffi::PyList_New(0)) }
|
||||
}
|
||||
|
||||
|
@ -264,7 +267,7 @@ impl PyList {
|
|||
}
|
||||
|
||||
/// Returns an iterator over this list's items.
|
||||
pub fn iter(&self) -> PyListIterator {
|
||||
pub fn iter(&self) -> PyListIterator<'_> {
|
||||
PyListIterator {
|
||||
list: self,
|
||||
index: 0,
|
||||
|
@ -351,7 +354,7 @@ impl<T> IntoPy<PyObject> for Vec<T>
|
|||
where
|
||||
T: IntoPy<PyObject>,
|
||||
{
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
let mut iter = self.into_iter().map(|e| e.into_py(py));
|
||||
let list = new_from_iter(py, &mut iter);
|
||||
list.into()
|
||||
|
@ -853,7 +856,7 @@ mod tests {
|
|||
}
|
||||
|
||||
impl ToPyObject for Bad {
|
||||
fn to_object(&self, py: Python) -> Py<PyAny> {
|
||||
fn to_object(&self, py: Python<'_>) -> Py<PyAny> {
|
||||
self.to_owned().into_py(py)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -142,7 +142,7 @@ impl Py<PyMapping> {
|
|||
/// Similar to [`as_ref`](#method.as_ref), and also consumes this `Py` and registers the
|
||||
/// Python object reference in PyO3's object storage. The reference count for the Python
|
||||
/// object will not be decreased until the GIL lifetime ends.
|
||||
pub fn into_ref(self, py: Python) -> &PyMapping {
|
||||
pub fn into_ref(self, py: Python<'_>) -> &PyMapping {
|
||||
unsafe { py.from_owned_ptr(self.into_ptr()) }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@ macro_rules! pyobject_native_type_base(
|
|||
unsafe impl<$($generics,)*> $crate::PyNativeType for $name {}
|
||||
|
||||
impl<$($generics,)*> ::std::fmt::Debug for $name {
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter)
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>)
|
||||
-> ::std::result::Result<(), ::std::fmt::Error>
|
||||
{
|
||||
let s = self.repr().or(::std::result::Result::Err(::std::fmt::Error))?;
|
||||
|
@ -48,7 +48,7 @@ macro_rules! pyobject_native_type_base(
|
|||
}
|
||||
|
||||
impl<$($generics,)*> ::std::fmt::Display for $name {
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter)
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>)
|
||||
-> ::std::result::Result<(), ::std::fmt::Error>
|
||||
{
|
||||
let s = self.str().or(::std::result::Result::Err(::std::fmt::Error))?;
|
||||
|
@ -59,7 +59,7 @@ macro_rules! pyobject_native_type_base(
|
|||
impl<$($generics,)*> $crate::ToPyObject for $name
|
||||
{
|
||||
#[inline]
|
||||
fn to_object(&self, py: $crate::Python) -> $crate::PyObject {
|
||||
fn to_object(&self, py: $crate::Python<'_>) -> $crate::PyObject {
|
||||
use $crate::AsPyPointer;
|
||||
unsafe { $crate::PyObject::from_borrowed_ptr(py, self.as_ptr()) }
|
||||
}
|
||||
|
@ -101,7 +101,7 @@ macro_rules! pyobject_native_type_named (
|
|||
|
||||
impl<$($generics,)*> $crate::IntoPy<$crate::Py<$name>> for &'_ $name {
|
||||
#[inline]
|
||||
fn into_py(self, py: $crate::Python) -> $crate::Py<$name> {
|
||||
fn into_py(self, py: $crate::Python<'_>) -> $crate::Py<$name> {
|
||||
use $crate::AsPyPointer;
|
||||
unsafe { $crate::Py::from_borrowed_ptr(py, self.as_ptr()) }
|
||||
}
|
||||
|
@ -135,7 +135,7 @@ macro_rules! pyobject_native_type_info(
|
|||
const MODULE: ::std::option::Option<&'static str> = $module;
|
||||
|
||||
#[inline]
|
||||
fn type_object_raw(_py: $crate::Python) -> *mut $crate::ffi::PyTypeObject {
|
||||
fn type_object_raw(_py: $crate::Python<'_>) -> *mut $crate::ffi::PyTypeObject {
|
||||
// Create a very short lived mutable reference and directly
|
||||
// cast it to a pointer: no mutable references can be aliasing
|
||||
// because we hold the GIL.
|
||||
|
@ -191,7 +191,7 @@ macro_rules! pyobject_native_type_sized {
|
|||
($name:ty, $layout:path $(;$generics:ident)*) => {
|
||||
unsafe impl $crate::type_object::PyLayout<$name> for $layout {}
|
||||
impl $crate::type_object::PySizedLayout<$name> for $layout {}
|
||||
impl<'a, $($generics,)*> $crate::impl_::pyclass::PyClassBaseType for $name {
|
||||
impl<$($generics,)*> $crate::impl_::pyclass::PyClassBaseType for $name {
|
||||
type LayoutAsBase = $crate::pycell::PyCellBase<$layout>;
|
||||
type BaseNativeType = $name;
|
||||
type ThreadChecker = $crate::impl_::pyclass::ThreadCheckerStub<$crate::PyObject>;
|
||||
|
|
|
@ -221,7 +221,7 @@ impl PyModule {
|
|||
/// use pyo3::prelude::*;
|
||||
///
|
||||
/// #[pymodule]
|
||||
/// fn my_module(_py: Python, module: &PyModule) -> PyResult<()> {
|
||||
/// fn my_module(_py: Python<'_>, module: &PyModule) -> PyResult<()> {
|
||||
/// module.add("c", 299_792_458)?;
|
||||
/// Ok(())
|
||||
/// }
|
||||
|
@ -265,7 +265,7 @@ impl PyModule {
|
|||
/// struct Foo { /* fields omitted */ }
|
||||
///
|
||||
/// #[pymodule]
|
||||
/// fn my_module(_py: Python, module: &PyModule) -> PyResult<()> {
|
||||
/// fn my_module(_py: Python<'_>, module: &PyModule) -> PyResult<()> {
|
||||
/// module.add_class::<Foo>()?;
|
||||
/// Ok(())
|
||||
/// }
|
||||
|
@ -324,7 +324,7 @@ impl PyModule {
|
|||
/// use pyo3::prelude::*;
|
||||
///
|
||||
/// #[pymodule]
|
||||
/// fn my_module(py: Python, module: &PyModule) -> PyResult<()> {
|
||||
/// fn my_module(py: Python<'_>, module: &PyModule) -> PyResult<()> {
|
||||
/// let submodule = PyModule::new(py, "submodule")?;
|
||||
/// submodule.add("super_useful_constant", "important")?;
|
||||
///
|
||||
|
@ -367,7 +367,7 @@ impl PyModule {
|
|||
/// println!("Hello world!")
|
||||
/// }
|
||||
/// #[pymodule]
|
||||
/// fn my_module(_py: Python, module: &PyModule) -> PyResult<()> {
|
||||
/// fn my_module(_py: Python<'_>, module: &PyModule) -> PyResult<()> {
|
||||
/// module.add_function(wrap_pyfunction!(say_hello, module)?)
|
||||
/// }
|
||||
/// ```
|
||||
|
|
|
@ -14,12 +14,12 @@ macro_rules! int_fits_larger_int {
|
|||
($rust_type:ty, $larger_type:ty) => {
|
||||
impl ToPyObject for $rust_type {
|
||||
#[inline]
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
(*self as $larger_type).into_py(py)
|
||||
}
|
||||
}
|
||||
impl IntoPy<PyObject> for $rust_type {
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
(self as $larger_type).into_py(py)
|
||||
}
|
||||
}
|
||||
|
@ -48,12 +48,12 @@ pyobject_native_type_core!(PyLong, ffi::PyLong_Type, #checkfunction=ffi::PyLong_
|
|||
macro_rules! int_fits_c_long {
|
||||
($rust_type:ty) => {
|
||||
impl ToPyObject for $rust_type {
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
unsafe { PyObject::from_owned_ptr(py, ffi::PyLong_FromLong(*self as c_long)) }
|
||||
}
|
||||
}
|
||||
impl IntoPy<PyObject> for $rust_type {
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
unsafe { PyObject::from_owned_ptr(py, ffi::PyLong_FromLong(self as c_long)) }
|
||||
}
|
||||
}
|
||||
|
@ -82,13 +82,13 @@ macro_rules! int_convert_u64_or_i64 {
|
|||
($rust_type:ty, $pylong_from_ll_or_ull:expr, $pylong_as_ll_or_ull:expr) => {
|
||||
impl ToPyObject for $rust_type {
|
||||
#[inline]
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
unsafe { PyObject::from_owned_ptr(py, $pylong_from_ll_or_ull(*self)) }
|
||||
}
|
||||
}
|
||||
impl IntoPy<PyObject> for $rust_type {
|
||||
#[inline]
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
unsafe { PyObject::from_owned_ptr(py, $pylong_from_ll_or_ull(self)) }
|
||||
}
|
||||
}
|
||||
|
@ -152,12 +152,12 @@ mod fast_128bit_int_conversion {
|
|||
($rust_type: ty, $is_signed: expr) => {
|
||||
impl ToPyObject for $rust_type {
|
||||
#[inline]
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
(*self).into_py(py)
|
||||
}
|
||||
}
|
||||
impl IntoPy<PyObject> for $rust_type {
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
unsafe {
|
||||
// Always use little endian
|
||||
let bytes = self.to_le_bytes();
|
||||
|
@ -211,13 +211,13 @@ mod slow_128bit_int_conversion {
|
|||
($rust_type: ty, $half_type: ty) => {
|
||||
impl ToPyObject for $rust_type {
|
||||
#[inline]
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
(*self).into_py(py)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoPy<PyObject> for $rust_type {
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
let lower = self as u64;
|
||||
let upper = (self >> SHIFT) as $half_type;
|
||||
unsafe {
|
||||
|
@ -262,7 +262,7 @@ mod slow_128bit_int_conversion {
|
|||
}
|
||||
|
||||
fn err_if_invalid_value<T: PartialEq>(
|
||||
py: Python,
|
||||
py: Python<'_>,
|
||||
invalid_value: T,
|
||||
actual_value: T,
|
||||
) -> PyResult<T> {
|
||||
|
|
|
@ -330,7 +330,7 @@ impl Py<PySequence> {
|
|||
/// Similar to [`as_ref`](#method.as_ref), and also consumes this `Py` and registers the
|
||||
/// Python object reference in PyO3's object storage. The reference count for the Python
|
||||
/// object will not be decreased until the GIL lifetime ends.
|
||||
pub fn into_ref(self, py: Python) -> &PySequence {
|
||||
pub fn into_ref(self, py: Python<'_>) -> &PySequence {
|
||||
unsafe { py.from_owned_ptr(self.into_ptr()) }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ impl PySet {
|
|||
}
|
||||
|
||||
/// Creates a new empty set.
|
||||
pub fn empty(py: Python) -> PyResult<&PySet> {
|
||||
pub fn empty(py: Python<'_>) -> PyResult<&PySet> {
|
||||
unsafe { py.from_owned_ptr_or_err(ffi::PySet_New(ptr::null_mut())) }
|
||||
}
|
||||
|
||||
|
@ -111,7 +111,7 @@ impl PySet {
|
|||
/// Returns an iterator of values in this set.
|
||||
///
|
||||
/// Note that it can be unsafe to use when the set might be changed by other code.
|
||||
pub fn iter(&self) -> PySetIterator {
|
||||
pub fn iter(&self) -> PySetIterator<'_> {
|
||||
PySetIterator::new(self)
|
||||
}
|
||||
}
|
||||
|
@ -123,7 +123,7 @@ pub struct PySetIterator<'p> {
|
|||
|
||||
#[cfg(Py_LIMITED_API)]
|
||||
impl PySetIterator<'_> {
|
||||
fn new(set: &PyAny) -> PySetIterator {
|
||||
fn new(set: &PyAny) -> PySetIterator<'_> {
|
||||
PySetIterator {
|
||||
it: PyIterator::from_object(set.py(), set).unwrap(),
|
||||
}
|
||||
|
@ -148,7 +148,7 @@ pub struct PySetIterator<'py> {
|
|||
|
||||
#[cfg(not(Py_LIMITED_API))]
|
||||
impl PySetIterator<'_> {
|
||||
fn new(set: &PyAny) -> PySetIterator {
|
||||
fn new(set: &PyAny) -> PySetIterator<'_> {
|
||||
PySetIterator { set, pos: 0 }
|
||||
}
|
||||
}
|
||||
|
@ -195,7 +195,7 @@ where
|
|||
T: hash::Hash + Eq + ToPyObject,
|
||||
S: hash::BuildHasher + Default,
|
||||
{
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
let set = PySet::new::<T>(py, &[]).expect("Failed to construct empty set");
|
||||
{
|
||||
for val in self {
|
||||
|
@ -210,7 +210,7 @@ impl<T> ToPyObject for collections::BTreeSet<T>
|
|||
where
|
||||
T: hash::Hash + Eq + ToPyObject,
|
||||
{
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
let set = PySet::new::<T>(py, &[]).expect("Failed to construct empty set");
|
||||
{
|
||||
for val in self {
|
||||
|
@ -226,7 +226,7 @@ where
|
|||
K: IntoPy<PyObject> + Eq + hash::Hash,
|
||||
S: hash::BuildHasher + Default,
|
||||
{
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
let set = PySet::empty(py).expect("Failed to construct empty set");
|
||||
{
|
||||
for val in self {
|
||||
|
@ -252,7 +252,7 @@ impl<K> IntoPy<PyObject> for BTreeSet<K>
|
|||
where
|
||||
K: IntoPy<PyObject> + cmp::Ord,
|
||||
{
|
||||
fn into_py(self, py: Python) -> PyObject {
|
||||
fn into_py(self, py: Python<'_>) -> PyObject {
|
||||
let set = PySet::empty(py).expect("Failed to construct empty set");
|
||||
{
|
||||
for val in self {
|
||||
|
@ -283,7 +283,7 @@ impl PyFrozenSet {
|
|||
}
|
||||
|
||||
/// Creates a new empty frozen set
|
||||
pub fn empty(py: Python) -> PyResult<&PyFrozenSet> {
|
||||
pub fn empty(py: Python<'_>) -> PyResult<&PyFrozenSet> {
|
||||
unsafe { py.from_owned_ptr_or_err(ffi::PyFrozenSet_New(ptr::null_mut())) }
|
||||
}
|
||||
|
||||
|
@ -317,7 +317,7 @@ impl PyFrozenSet {
|
|||
/// Returns an iterator of values in this frozen set.
|
||||
///
|
||||
/// Note that it can be unsafe to use when the set might be changed by other code.
|
||||
pub fn iter(&self) -> PySetIterator {
|
||||
pub fn iter(&self) -> PySetIterator<'_> {
|
||||
PySetIterator::new(self.as_ref())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@ impl PySliceIndices {
|
|||
|
||||
impl PySlice {
|
||||
/// Constructs a new slice with the given elements.
|
||||
pub fn new(py: Python, start: isize, stop: isize, step: isize) -> &PySlice {
|
||||
pub fn new(py: Python<'_>, start: isize, stop: isize, step: isize) -> &PySlice {
|
||||
unsafe {
|
||||
let ptr = ffi::PySlice_New(
|
||||
ffi::PyLong_FromLong(start as c_long),
|
||||
|
@ -84,7 +84,7 @@ impl PySlice {
|
|||
}
|
||||
|
||||
impl ToPyObject for PySliceIndices {
|
||||
fn to_object(&self, py: Python) -> PyObject {
|
||||
fn to_object(&self, py: Python<'_>) -> PyObject {
|
||||
PySlice::new(py, self.start, self.stop, self.step).into()
|
||||
}
|
||||
}
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue