Skip to content

Commit c602d8d

Browse files
committed
Tidy some usage of py.from_borrowed_ptr
1 parent 61bc02d commit c602d8d

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

pyo3-macros-backend/src/pymethod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,12 +572,11 @@ pub fn impl_py_setter_def(
572572
_slf: *mut _pyo3::ffi::PyObject,
573573
_value: *mut _pyo3::ffi::PyObject,
574574
) -> _pyo3::PyResult<::std::os::raw::c_int> {
575-
let _value = py
576-
.from_borrowed_ptr_or_opt(_value)
575+
let _value = _pyo3::Bound::from_borrowed_ptr_or_opt(py, _value)
577576
.ok_or_else(|| {
578577
_pyo3::exceptions::PyAttributeError::new_err("can't delete attribute")
579578
})?;
580-
let _val = _pyo3::FromPyObject::extract(_value)?;
579+
let _val = _pyo3::FromPyObject::extract_bound(&_value)?;
581580
#( #holders )*
582581
_pyo3::callback::convert(py, #setter_impl)
583582
}

src/ffi/tests.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::Python;
55
#[cfg(not(Py_LIMITED_API))]
66
use crate::{
77
types::{PyDict, PyString},
8-
IntoPy, Py, PyAny,
8+
Bound, IntoPy, Py, PyAny,
99
};
1010
#[cfg(not(any(Py_3_12, Py_LIMITED_API)))]
1111
use libc::wchar_t;
@@ -57,9 +57,9 @@ fn test_date_fromtimestamp() {
5757
#[test]
5858
fn test_utc_timezone() {
5959
Python::with_gil(|py| {
60-
let utc_timezone: &PyAny = unsafe {
60+
let utc_timezone: Bound<'_, PyAny> = unsafe {
6161
PyDateTime_IMPORT();
62-
py.from_borrowed_ptr(PyDateTime_TimeZone_UTC())
62+
Bound::from_borrowed_ptr(py, PyDateTime_TimeZone_UTC())
6363
};
6464
let locals = PyDict::new_bound(py);
6565
locals.set_item("utc_timezone", utc_timezone).unwrap();
@@ -254,35 +254,33 @@ fn test_get_tzinfo() {
254254

255255
crate::Python::with_gil(|py| {
256256
use crate::types::{PyDateTime, PyTime};
257-
use crate::PyAny;
258257

259258
let utc = &timezone_utc_bound(py);
260259

261260
let dt = PyDateTime::new_bound(py, 2018, 1, 1, 0, 0, 0, 0, Some(utc)).unwrap();
262261

263262
assert!(
264-
unsafe { py.from_borrowed_ptr::<PyAny>(PyDateTime_DATE_GET_TZINFO(dt.as_ptr())) }
263+
unsafe { Bound::from_borrowed_ptr(py, PyDateTime_DATE_GET_TZINFO(dt.as_ptr())) }
265264
.is(utc)
266265
);
267266

268267
let dt = PyDateTime::new_bound(py, 2018, 1, 1, 0, 0, 0, 0, None).unwrap();
269268

270269
assert!(
271-
unsafe { py.from_borrowed_ptr::<PyAny>(PyDateTime_DATE_GET_TZINFO(dt.as_ptr())) }
270+
unsafe { Bound::from_borrowed_ptr(py, PyDateTime_DATE_GET_TZINFO(dt.as_ptr())) }
272271
.is_none()
273272
);
274273

275274
let t = PyTime::new_bound(py, 0, 0, 0, 0, Some(utc)).unwrap();
276275

277276
assert!(
278-
unsafe { py.from_borrowed_ptr::<PyAny>(PyDateTime_TIME_GET_TZINFO(t.as_ptr())) }
279-
.is(utc)
277+
unsafe { Bound::from_borrowed_ptr(py, PyDateTime_TIME_GET_TZINFO(t.as_ptr())) }.is(utc)
280278
);
281279

282280
let t = PyTime::new_bound(py, 0, 0, 0, 0, None).unwrap();
283281

284282
assert!(
285-
unsafe { py.from_borrowed_ptr::<PyAny>(PyDateTime_TIME_GET_TZINFO(t.as_ptr())) }
283+
unsafe { Bound::from_borrowed_ptr(py, PyDateTime_TIME_GET_TZINFO(t.as_ptr())) }
286284
.is_none()
287285
);
288286
})

src/impl_/pyclass.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ use crate::{
66
internal_tricks::extract_c_string,
77
pycell::PyCellLayout,
88
pyclass_init::PyObjectInit,
9+
types::any::PyAnyMethods,
910
types::PyBool,
10-
Py, PyAny, PyCell, PyClass, PyErr, PyMethodDefType, PyNativeType, PyResult, PyTypeInfo, Python,
11+
Borrowed, Py, PyAny, PyCell, PyClass, PyErr, PyMethodDefType, PyNativeType, PyResult,
12+
PyTypeInfo, Python,
1113
};
1214
use std::{
1315
borrow::Cow,
@@ -811,8 +813,8 @@ slot_fragment_trait! {
811813
other: *mut ffi::PyObject,
812814
) -> PyResult<*mut ffi::PyObject> {
813815
// By default `__ne__` will try `__eq__` and invert the result
814-
let slf: &PyAny = py.from_borrowed_ptr(slf);
815-
let other: &PyAny = py.from_borrowed_ptr(other);
816+
let slf = Borrowed::from_ptr(py, slf);
817+
let other = Borrowed::from_ptr(py, other);
816818
slf.eq(other).map(|is_eq| PyBool::new_bound(py, !is_eq).to_owned().into_ptr())
817819
}
818820
}

0 commit comments

Comments
 (0)