Skip to content

Commit

Permalink
Tidy some usage of py.from_borrowed_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
LilyFoote committed Feb 20, 2024
1 parent 61bc02d commit c602d8d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
5 changes: 2 additions & 3 deletions pyo3-macros-backend/src/pymethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,12 +572,11 @@ pub fn impl_py_setter_def(
_slf: *mut _pyo3::ffi::PyObject,
_value: *mut _pyo3::ffi::PyObject,
) -> _pyo3::PyResult<::std::os::raw::c_int> {
let _value = py
.from_borrowed_ptr_or_opt(_value)
let _value = _pyo3::Bound::from_borrowed_ptr_or_opt(py, _value)
.ok_or_else(|| {
_pyo3::exceptions::PyAttributeError::new_err("can't delete attribute")
})?;
let _val = _pyo3::FromPyObject::extract(_value)?;
let _val = _pyo3::FromPyObject::extract_bound(&_value)?;
#( #holders )*
_pyo3::callback::convert(py, #setter_impl)
}
Expand Down
16 changes: 7 additions & 9 deletions src/ffi/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::Python;
#[cfg(not(Py_LIMITED_API))]
use crate::{
types::{PyDict, PyString},
IntoPy, Py, PyAny,
Bound, IntoPy, Py, PyAny,
};
#[cfg(not(any(Py_3_12, Py_LIMITED_API)))]
use libc::wchar_t;
Expand Down Expand Up @@ -57,9 +57,9 @@ fn test_date_fromtimestamp() {
#[test]
fn test_utc_timezone() {
Python::with_gil(|py| {
let utc_timezone: &PyAny = unsafe {
let utc_timezone: Bound<'_, PyAny> = unsafe {
PyDateTime_IMPORT();
py.from_borrowed_ptr(PyDateTime_TimeZone_UTC())
Bound::from_borrowed_ptr(py, PyDateTime_TimeZone_UTC())
};
let locals = PyDict::new_bound(py);
locals.set_item("utc_timezone", utc_timezone).unwrap();
Expand Down Expand Up @@ -254,35 +254,33 @@ fn test_get_tzinfo() {

crate::Python::with_gil(|py| {
use crate::types::{PyDateTime, PyTime};
use crate::PyAny;

let utc = &timezone_utc_bound(py);

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

assert!(
unsafe { py.from_borrowed_ptr::<PyAny>(PyDateTime_DATE_GET_TZINFO(dt.as_ptr())) }
unsafe { Bound::from_borrowed_ptr(py, PyDateTime_DATE_GET_TZINFO(dt.as_ptr())) }
.is(utc)
);

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

assert!(
unsafe { py.from_borrowed_ptr::<PyAny>(PyDateTime_DATE_GET_TZINFO(dt.as_ptr())) }
unsafe { Bound::from_borrowed_ptr(py, PyDateTime_DATE_GET_TZINFO(dt.as_ptr())) }
.is_none()
);

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

assert!(
unsafe { py.from_borrowed_ptr::<PyAny>(PyDateTime_TIME_GET_TZINFO(t.as_ptr())) }
.is(utc)
unsafe { Bound::from_borrowed_ptr(py, PyDateTime_TIME_GET_TZINFO(t.as_ptr())) }.is(utc)
);

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

assert!(
unsafe { py.from_borrowed_ptr::<PyAny>(PyDateTime_TIME_GET_TZINFO(t.as_ptr())) }
unsafe { Bound::from_borrowed_ptr(py, PyDateTime_TIME_GET_TZINFO(t.as_ptr())) }
.is_none()
);
})
Expand Down
8 changes: 5 additions & 3 deletions src/impl_/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ use crate::{
internal_tricks::extract_c_string,
pycell::PyCellLayout,
pyclass_init::PyObjectInit,
types::any::PyAnyMethods,
types::PyBool,
Py, PyAny, PyCell, PyClass, PyErr, PyMethodDefType, PyNativeType, PyResult, PyTypeInfo, Python,
Borrowed, Py, PyAny, PyCell, PyClass, PyErr, PyMethodDefType, PyNativeType, PyResult,
PyTypeInfo, Python,
};
use std::{
borrow::Cow,
Expand Down Expand Up @@ -811,8 +813,8 @@ slot_fragment_trait! {
other: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> {
// By default `__ne__` will try `__eq__` and invert the result
let slf: &PyAny = py.from_borrowed_ptr(slf);
let other: &PyAny = py.from_borrowed_ptr(other);
let slf = Borrowed::from_ptr(py, slf);
let other = Borrowed::from_ptr(py, other);
slf.eq(other).map(|is_eq| PyBool::new_bound(py, !is_eq).to_owned().into_ptr())
}
}
Expand Down

0 comments on commit c602d8d

Please sign in to comment.