[PATCH 4 of 9] rust-cpython: allow mutation unless leaked reference is borrowed

Raphaël Gomès raphael.gomes at octobus.net
Sat Oct 19 16:14:06 EDT 2019


On 10/19/19 12:07 PM, Yuya Nishihara wrote:
> -            # since the iterator is potentially not deleted,
> -            # delete the iterator to release the reference for the Rust
> -            # implementation.
> -            # TODO make the Rust implementation behave like Python
> -            # since this would not work with a non ref-counting GC.
> -            del items
>   
Nice.
>   ///
> -/// # Warning
> -///
> -/// TODO allow Python container types: for now, integration with the garbage
> -///     collector does not extend to Rust structs holding references to Python
> -///     objects. Should the need surface, `__traverse__` and `__clear__` will
> -///     need to be written as per the `rust-cpython` docs on GC integration.
> -///
Nice.
>       #[test]
> -    fn test_borrow_mut_while_leaked() {
> +    fn test_borrow_mut_while_leaked_ref() {
>           let (gil, owner) = prepare_env();
>           let py = gil.python();
>           assert!(owner.string_shared(py).borrow_mut().is_ok());
> -        let _leaked = owner.string_shared(py).leak_immutable().unwrap();
> -        // TODO: will be allowed
> -        assert!(owner.string_shared(py).borrow_mut().is_err());
> +        let leaked = owner.string_shared(py).leak_immutable().unwrap();
> +        {
> +            let _leaked_ref = leaked.try_borrow(py).unwrap();
> +            assert!(owner.string_shared(py).borrow_mut().is_err());
> +            {
> +                let _leaked_ref2 = leaked.try_borrow(py).unwrap();
> +                assert!(owner.string_shared(py).borrow_mut().is_err());
> +            }
> +            assert!(owner.string_shared(py).borrow_mut().is_err());
> +        }
> +        assert!(owner.string_shared(py).borrow_mut().is_ok());
> +    }
> +
> +    #[test]
> +    fn test_borrow_mut_while_leaked_ref_mut() {
> +        let (gil, owner) = prepare_env();
> +        let py = gil.python();
> +        assert!(owner.string_shared(py).borrow_mut().is_ok());
> +        let leaked = owner.string_shared(py).leak_immutable().unwrap();
> +        let mut leaked_iter = unsafe { leaked.map(py, |s| s.chars()) };
> +        {
> +            let _leaked_ref = leaked_iter.try_borrow_mut(py).unwrap();
> +            assert!(owner.string_shared(py).borrow_mut().is_err());
> +        }
> +        assert!(owner.string_shared(py).borrow_mut().is_ok());
>       }
>   }
Thanks for writing tests. Other than the obvious merit of ...testing the 
code, they offer a nice overview of the interface.


More information about the Mercurial-devel mailing list