1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
use pyo3::prelude::*;
use pyo3_sharedref::*;
#[pyclass]
struct Owner {
string: PyShareable<String>,
}
#[pymethods]
impl Owner {
#[new]
fn new(s: String) -> Self {
Self { string: s.into() }
}
}
fn with_setup(
test: impl FnOnce(Python<'_>, &Bound<'_, Owner>) -> PyResult<()>,
) -> PyResult<()> {
Python::initialize();
Python::attach(|py| {
let owner = Bound::new(py, Owner::new("new".to_owned()))?;
test(py, &owner)
})
}
/// "leak" in the sense of `SharedByPyObject` the `string` data field,
/// taking care of all the boilerplate
fn share_string(owner: &Bound<'_, Owner>) -> SharedByPyObject<&'static String> {
let shareable = &owner.borrow().string;
unsafe { shareable.share(owner) }
}
fn try_share_string(
owner: &Bound<'_, Owner>,
) -> Result<SharedByPyObject<&'static String>, TryShareError> {
let shareable = &owner.borrow().string;
unsafe { shareable.try_share(owner) }
}
/// Mutate the `string` field of `owner` as would be done from Python code
///
/// This is to simulate normal mutation of the owner object from
/// the Python interpreter. This could be replaced by methods of [`Owner`]
/// (wih closure replaced by a small fixed operations)
/// and perhaps will, once we are done converting the original tests
/// from rust-cpython
fn mutate_string<'py>(
owner: &'py Bound<'py, Owner>,
f: impl FnOnce(&mut String),
) {
let shareable = &owner.borrow_mut().string;
let shared_ref = unsafe { shareable.borrow_with_owner(owner) };
f(&mut shared_ref.write());
}
#[test]
fn test_shared_borrow() -> PyResult<()> {
with_setup(|py, owner| {
let shared = share_string(owner);
let shared_ref = unsafe { shared.try_borrow(py) }.unwrap();
assert_eq!(*shared_ref, "new");
Ok(())
})
}
#[test]
fn test_shared_borrow_mut() -> PyResult<()> {
with_setup(|py, owner| {
let shared = share_string(owner);
let mut shared_iter = unsafe { shared.map(py, |s| s.chars()) };
let mut shared_ref = unsafe { shared_iter.try_borrow_mut(py) }.unwrap();
assert_eq!(shared_ref.next(), Some('n'));
assert_eq!(shared_ref.next(), Some('e'));
assert_eq!(shared_ref.next(), Some('w'));
assert_eq!(shared_ref.next(), None);
Ok(())
})
}
#[test]
fn test_shared_borrow_after_mut() -> PyResult<()> {
with_setup(|py, owner| {
let shared = share_string(owner);
mutate_string(owner, String::clear);
assert!(unsafe { shared.try_borrow(py) }.is_err());
Ok(())
})
}
#[test]
fn test_shared_borrow_mut_after_mut() -> PyResult<()> {
with_setup(|py, owner| {
let shared = share_string(owner);
let mut shared_iter = unsafe { shared.map(py, |s| s.chars()) };
mutate_string(owner, String::clear);
assert!(unsafe { shared_iter.try_borrow_mut(py) }.is_err());
Ok(())
})
}
#[test]
#[should_panic(expected = "map() over invalidated shared reference")]
fn test_shared_map_after_mut() {
with_setup(|py, owner| {
let shared = share_string(owner);
mutate_string(owner, String::clear);
let _shared_iter = unsafe { shared.map(py, |s| s.chars()) };
Ok(())
})
.expect("should already have panicked")
}
/// run `try_borrow_mut` on the `string` field and assert it is not an error
///
/// Simply returning the `Result` is not possible, because that is
/// returning a reference to data owned by the function
fn assert_try_write_string_ok(owner: &Bound<'_, Owner>) {
let shareable = &owner.borrow().string;
let shared_ref = unsafe { shareable.borrow_with_owner(owner) };
assert!(shared_ref.try_write().is_ok());
}
fn assert_try_write_string_err(owner: &Bound<'_, Owner>) {
let shareable = &owner.borrow().string;
let shared_ref = unsafe { shareable.borrow_with_owner(owner) };
assert!(shared_ref.try_write().is_err());
}
fn assert_try_read_string_err(owner: &Bound<'_, Owner>) {
let shareable = &owner.borrow().string;
let shared_ref = unsafe { shareable.borrow_with_owner(owner) };
assert!(shared_ref.try_read().is_err());
}
#[test]
fn test_try_write_while_shared_ref() -> PyResult<()> {
with_setup(|py, owner| {
assert_try_write_string_ok(owner);
let shared = share_string(owner);
{
let _shared_ref = unsafe { shared.try_borrow(py) }.unwrap();
assert_try_write_string_err(owner);
{
let _shared_ref2 = unsafe { shared.try_borrow(py) }.unwrap();
assert_try_write_string_err(owner);
}
assert_try_write_string_err(owner);
}
assert_try_write_string_ok(owner);
Ok(())
})
}
#[test]
fn test_try_write_while_shared_ref_mut() -> PyResult<()> {
with_setup(|py, owner| {
assert_try_write_string_ok(owner);
let shared = share_string(owner);
let mut shared_iter = unsafe { shared.map(py, |s| s.chars()) };
{
let _shared_ref =
unsafe { shared_iter.try_borrow_mut(py) }.unwrap();
assert_try_write_string_err(owner);
}
assert_try_write_string_ok(owner);
Ok(())
})
}
#[test]
fn test_try_share_while_write() -> PyResult<()> {
with_setup(|_py, owner| {
let shareable = &owner.borrow().string;
let shared_ref = unsafe { shareable.borrow_with_owner(owner) };
let _mut_ref = shared_ref.write();
assert!(try_share_string(owner).is_err());
Ok(())
})
}
#[test]
#[should_panic(expected = "already mutably borrowed")]
fn test_share_while_write() {
with_setup(|_py, owner| {
let shareable = &owner.borrow().string;
let shared_ref = unsafe { shareable.borrow_with_owner(owner) };
let _mut_ref = shared_ref.write();
share_string(owner);
Ok(())
})
.expect("should already have panicked")
}
#[test]
fn test_try_write_while_borrow() -> PyResult<()> {
with_setup(|_py, owner| {
let shareable = &owner.borrow().string;
let shared_ref = unsafe { shareable.borrow_with_owner(owner) };
let _ref = shared_ref.read();
assert_try_write_string_err(owner);
Ok(())
})
}
#[test]
#[should_panic(expected = "already borrowed")]
fn test_write_while_borrow() {
with_setup(|_py, owner| {
let shareable = &owner.borrow().string;
let shared_ref = unsafe { shareable.borrow_with_owner(owner) };
let _ref = shared_ref.read();
let shared_ref2 = unsafe { shareable.borrow_with_owner(owner) };
let _mut_ref = shared_ref2.write();
Ok(())
})
.expect("should already have panicked")
}
#[test]
fn test_try_borrow_while_write() -> PyResult<()> {
with_setup(|_py, owner| {
let shareable = &owner.borrow().string;
let shared_ref = unsafe { shareable.borrow_with_owner(owner) };
let _mut_ref = shared_ref.write();
assert_try_read_string_err(owner);
Ok(())
})
}
#[test]
#[should_panic(expected = "already mutably borrowed")]
fn test_borrow_while_write() {
with_setup(|_py, owner| {
let shareable = &owner.borrow().string;
let shared_ref = unsafe { shareable.borrow_with_owner(owner) };
let _mut_ref = shared_ref.write();
let shared_ref2 = unsafe { shareable.borrow_with_owner(owner) };
let _ref = shared_ref2.read();
Ok(())
})
.expect("should already have panicked")
}
|