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