rust/pyo3-sharedref/tests/test_sharedref.rs
author Georges Racinet <georges.racinet@octobus.net>
Sat, 14 Dec 2024 18:21:56 +0100
changeset 52607 a7d2529ed6dd
child 52608 d85514a88706
permissions -rw-r--r--
rust-pyo3-sharedref: converted integration tests from rust-cpython This should bring full confidence on the conversion to PyO3. It highlights also the difference between the two bindings systems: in PyO3, the struct defined by the user is the inner Rust one. In rust-cpython, it is the wrapped one exposed to CPythob We enclose some of the boilerplate in helper functions. Perhaps we should first import the rust-cpython integration test, rework it with the same helpers, then only change the helpers.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52607
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
     1
use pyo3::prelude::*;
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
     2
use pyo3_sharedref::*;
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
     3
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
     4
#[pyclass]
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
     5
struct Owner {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
     6
    string: PySharedRefCell<String>,
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
     7
}
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
     8
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
     9
#[pymethods]
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    10
impl Owner {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    11
    #[new]
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    12
    fn new(s: String) -> Self {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    13
        Self {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    14
            string: PySharedRefCell::new(s),
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    15
        }
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    16
    }
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    17
}
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    18
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    19
fn with_setup(
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    20
    test: impl FnOnce(Python<'_>, &Bound<'_, Owner>) -> PyResult<()>,
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    21
) -> PyResult<()> {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    22
    pyo3::prepare_freethreaded_python();
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    23
    Python::with_gil(|py| {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    24
        let owner = Bound::new(py, Owner::new("new".to_owned()))?;
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    25
        test(py, &owner)
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    26
    })
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    27
}
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    28
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    29
/// "leak" in the sense of `UnsafePyLeaked` the `string` data field,
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    30
/// taking care of all the boilerplate
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    31
fn leak_string(owner: &Bound<'_, Owner>) -> UnsafePyLeaked<&'static String> {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    32
    let cell = &owner.borrow().string;
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    33
    let shared_ref = unsafe { cell.borrow(owner) };
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    34
    shared_ref.leak_immutable()
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    35
}
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    36
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    37
fn try_leak_string(
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    38
    owner: &Bound<'_, Owner>,
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    39
) -> Result<UnsafePyLeaked<&'static String>, TryLeakError> {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    40
    let cell = &owner.borrow().string;
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    41
    let shared_ref = unsafe { cell.borrow(owner) };
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    42
    shared_ref.try_leak_immutable()
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    43
}
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    44
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    45
/// Mutate the `string` field of `owner` as would be done from Python code
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    46
///
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    47
/// This is to simulate normal mutation of the owner object from
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    48
/// the Python interpreter. This could be replaced by methods of [`Owner`]
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    49
/// (wih closure replaced by a small fixed operations)
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    50
/// and perhaps will, once we are done converting the original tests
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    51
/// from rust-cpython
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    52
fn mutate_string<'py>(
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    53
    owner: &'py Bound<'py, Owner>,
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    54
    f: impl FnOnce(&mut String),
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    55
) -> () {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    56
    let cell = &owner.borrow_mut().string;
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    57
    let shared_ref = unsafe { cell.borrow(owner) };
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    58
    f(&mut shared_ref.borrow_mut());
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    59
}
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    60
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    61
#[test]
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    62
fn test_leaked_borrow() -> PyResult<()> {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    63
    with_setup(|py, owner| {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    64
        let leaked = leak_string(owner);
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    65
        let leaked_ref = unsafe { leaked.try_borrow(py) }.unwrap();
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    66
        assert_eq!(*leaked_ref, "new");
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    67
        Ok(())
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    68
    })
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    69
}
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    70
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    71
#[test]
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    72
fn test_leaked_borrow_mut() -> PyResult<()> {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    73
    with_setup(|py, owner| {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    74
        let leaked = leak_string(owner);
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    75
        let mut leaked_iter = unsafe { leaked.map(py, |s| s.chars()) };
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    76
        let mut leaked_ref =
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    77
            unsafe { leaked_iter.try_borrow_mut(py) }.unwrap();
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    78
        assert_eq!(leaked_ref.next(), Some('n'));
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    79
        assert_eq!(leaked_ref.next(), Some('e'));
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    80
        assert_eq!(leaked_ref.next(), Some('w'));
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    81
        assert_eq!(leaked_ref.next(), None);
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    82
        Ok(())
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    83
    })
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    84
}
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    85
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    86
#[test]
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    87
fn test_leaked_borrow_after_mut() -> PyResult<()> {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    88
    with_setup(|py, owner| {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    89
        let leaked = leak_string(owner);
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    90
        mutate_string(owner, String::clear);
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    91
        assert!(unsafe { leaked.try_borrow(py) }.is_err());
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    92
        Ok(())
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    93
    })
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    94
}
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    95
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    96
#[test]
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    97
fn test_leaked_borrow_mut_after_mut() -> PyResult<()> {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    98
    with_setup(|py, owner| {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
    99
        let leaked = leak_string(owner);
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   100
        let mut leaked_iter = unsafe { leaked.map(py, |s| s.chars()) };
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   101
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   102
        mutate_string(owner, String::clear);
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   103
        assert!(unsafe { leaked_iter.try_borrow_mut(py) }.is_err());
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   104
        Ok(())
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   105
    })
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   106
}
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   107
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   108
#[test]
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   109
#[should_panic(expected = "map() over invalidated leaked reference")]
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   110
fn test_leaked_map_after_mut() {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   111
    with_setup(|py, owner| {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   112
        let leaked = leak_string(owner);
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   113
        mutate_string(owner, String::clear);
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   114
        let _leaked_iter = unsafe { leaked.map(py, |s| s.chars()) };
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   115
        Ok(())
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   116
    })
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   117
    .expect("should already have panicked")
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   118
}
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   119
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   120
/// run `try_borrow_mut` on the `string` field and assert it is not an error
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   121
///
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   122
/// Simply returning the `Result` is not possible, because that is
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   123
/// returning a reference to data owned by the function
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   124
fn assert_try_borrow_string_mut_ok(owner: &Bound<'_, Owner>) {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   125
    let cell = &owner.borrow().string;
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   126
    let shared_ref = unsafe { cell.borrow(owner) };
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   127
    assert!(shared_ref.try_borrow_mut().is_ok());
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   128
}
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   129
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   130
fn assert_try_borrow_string_mut_err(owner: &Bound<'_, Owner>) {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   131
    let cell = &owner.borrow().string;
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   132
    let shared_ref = unsafe { cell.borrow(owner) };
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   133
    assert!(shared_ref.try_borrow_mut().is_err());
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   134
}
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   135
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   136
fn assert_try_borrow_string_err(owner: &Bound<'_, Owner>) {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   137
    let cell = &owner.borrow().string;
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   138
    let shared_ref = unsafe { cell.borrow(owner) };
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   139
    assert!(shared_ref.try_borrow().is_err());
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   140
}
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   141
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   142
#[test]
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   143
fn test_try_borrow_mut_while_leaked_ref() -> PyResult<()> {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   144
    with_setup(|py, owner| {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   145
        assert_try_borrow_string_mut_ok(owner);
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   146
        let leaked = leak_string(owner);
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   147
        {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   148
            let _leaked_ref = unsafe { leaked.try_borrow(py) }.unwrap();
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   149
            assert_try_borrow_string_mut_err(owner);
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   150
            {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   151
                let _leaked_ref2 = unsafe { leaked.try_borrow(py) }.unwrap();
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   152
                assert_try_borrow_string_mut_err(owner);
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   153
            }
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   154
            assert_try_borrow_string_mut_err(owner);
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   155
        }
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   156
        assert_try_borrow_string_mut_ok(owner);
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   157
        Ok(())
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   158
    })
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   159
}
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   160
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   161
#[test]
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   162
fn test_try_borrow_mut_while_leaked_ref_mut() -> PyResult<()> {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   163
    with_setup(|py, owner| {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   164
        assert_try_borrow_string_mut_ok(owner);
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   165
        let leaked = leak_string(owner);
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   166
        let mut leaked_iter = unsafe { leaked.map(py, |s| s.chars()) };
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   167
        {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   168
            let _leaked_ref =
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   169
                unsafe { leaked_iter.try_borrow_mut(py) }.unwrap();
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   170
            assert_try_borrow_string_mut_err(owner);
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   171
        }
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   172
        assert_try_borrow_string_mut_ok(owner);
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   173
        Ok(())
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   174
    })
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   175
}
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   176
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   177
#[test]
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   178
fn test_try_leak_while_borrow_mut() -> PyResult<()> {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   179
    with_setup(|_py, owner| {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   180
        let cell = &owner.borrow().string;
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   181
        let shared_ref = unsafe { cell.borrow(owner) };
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   182
        let _mut_ref = shared_ref.borrow_mut();
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   183
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   184
        assert!(try_leak_string(owner).is_err());
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   185
        Ok(())
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   186
    })
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   187
}
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   188
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   189
#[test]
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   190
#[should_panic(expected = "already mutably borrowed")]
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   191
fn test_leak_while_borrow_mut() {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   192
    with_setup(|_py, owner| {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   193
        let cell = &owner.borrow().string;
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   194
        let shared_ref = unsafe { cell.borrow(owner) };
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   195
        let _mut_ref = shared_ref.borrow_mut();
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   196
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   197
        leak_string(owner);
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   198
        Ok(())
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   199
    })
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   200
    .expect("should already have panicked")
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   201
}
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   202
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   203
#[test]
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   204
fn test_try_borrow_mut_while_borrow() -> PyResult<()> {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   205
    with_setup(|_py, owner| {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   206
        let cell = &owner.borrow().string;
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   207
        let shared_ref = unsafe { cell.borrow(owner) };
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   208
        let _ref = shared_ref.borrow();
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   209
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   210
        assert_try_borrow_string_mut_err(owner);
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   211
        Ok(())
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   212
    })
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   213
}
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   214
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   215
#[test]
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   216
#[should_panic(expected = "already borrowed")]
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   217
fn test_borrow_mut_while_borrow() {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   218
    with_setup(|_py, owner| {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   219
        let cell = &owner.borrow().string;
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   220
        let shared_ref = unsafe { cell.borrow(owner) };
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   221
        let _ref = shared_ref.borrow();
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   222
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   223
        let shared_ref2 = unsafe { cell.borrow(owner) };
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   224
        let _mut_ref = shared_ref2.borrow_mut();
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   225
        Ok(())
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   226
    })
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   227
    .expect("should already have panicked")
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   228
}
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   229
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   230
#[test]
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   231
fn test_try_borrow_while_borrow_mut() -> PyResult<()> {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   232
    with_setup(|_py, owner| {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   233
        let cell = &owner.borrow().string;
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   234
        let shared_ref = unsafe { cell.borrow(owner) };
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   235
        let _mut_ref = shared_ref.borrow_mut();
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   236
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   237
        assert_try_borrow_string_err(owner);
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   238
        Ok(())
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   239
    })
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   240
}
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   241
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   242
#[test]
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   243
#[should_panic(expected = "already mutably borrowed")]
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   244
fn test_borrow_while_borrow_mut() {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   245
    with_setup(|_py, owner| {
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   246
        let cell = &owner.borrow().string;
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   247
        let shared_ref = unsafe { cell.borrow(owner) };
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   248
        let _mut_ref = shared_ref.borrow_mut();
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   249
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   250
        let shared_ref2 = unsafe { cell.borrow(owner) };
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   251
        let _ref = shared_ref2.borrow();
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   252
        Ok(())
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   253
    })
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   254
    .expect("should already have panicked")
a7d2529ed6dd rust-pyo3-sharedref: converted integration tests from rust-cpython
Georges Racinet <georges.racinet@octobus.net>
parents:
diff changeset
   255
}