Skip to content

Commit

Permalink
attempt to fix issues with test_getter_setter
Browse files Browse the repository at this point in the history
  • Loading branch information
ngoldbaum committed Oct 30, 2024
1 parent 862b882 commit 8316e1f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
39 changes: 39 additions & 0 deletions src/conversions/std/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::num::{
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
};
use std::os::raw::c_long;
use std::sync::atomic::{AtomicI32, Ordering};

macro_rules! int_fits_larger_int {
($rust_type:ty, $larger_type:ty) => {
Expand Down Expand Up @@ -690,6 +691,44 @@ nonzero_int_impl!(NonZeroU64, u64);
nonzero_int_impl!(NonZeroU128, u128);
nonzero_int_impl!(NonZeroUsize, usize);

impl<'py> IntoPyObject<'py> for &AtomicI32 {
type Target = PyInt;
type Output = Bound<'py, Self::Target>;
type Error = Infallible;

fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
unsafe {
Ok(ffi::PyLong_FromLong(self.load(Ordering::Relaxed) as c_long)
.assume_owned(py)
.downcast_into_unchecked())
}
}

#[cfg(feature = "experimental-inspect")]
fn type_output() -> TypeInfo {
TypeInfo::builtin("int")
}
}

impl<'py> IntoPyObject<'py> for AtomicI32 {
type Target = PyInt;
type Output = Bound<'py, Self::Target>;
type Error = Infallible;

fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
unsafe {
Ok(ffi::PyLong_FromLong(self.load(Ordering::Relaxed) as c_long)
.assume_owned(py)
.downcast_into_unchecked())
}
}

#[cfg(feature = "experimental-inspect")]
fn type_output() -> TypeInfo {
TypeInfo::builtin("int")
}
}

#[cfg(test)]
mod test_128bit_integers {
use super::*;
Expand Down
12 changes: 6 additions & 6 deletions tests/test_getter_setter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![cfg(feature = "macros")]

use std::cell::Cell;
use std::sync::atomic::AtomicI32;

use pyo3::prelude::*;
use pyo3::py_run;
Expand Down Expand Up @@ -218,19 +218,19 @@ fn get_all_and_set() {
}

#[pyclass]
struct CellGetterSetter {
struct AtomicGetterSetter {
#[pyo3(get, set)]
cell_inner: Cell<i32>,
inner: AtomicI32,
}

#[test]
fn cell_getter_setter() {
let c = CellGetterSetter {
cell_inner: Cell::new(10),
let c = AtomicGetterSetter {
inner: AtomicI32::new(10),
};
Python::with_gil(|py| {
let inst = Py::new(py, c).unwrap();
let cell = Cell::new(20i32).into_pyobject(py).unwrap();
let cell = AtomicI32::new(20i32).into_pyobject(py).unwrap();

py_run!(py, cell, "assert cell == 20");
py_run!(py, inst, "assert inst.cell_inner == 10");
Expand Down

0 comments on commit 8316e1f

Please sign in to comment.