Mercurial > public > mercurial-scm > hg
view rust/hg-cpython/src/exceptions.rs @ 43422:b9f791090211
rust-cpython: rename PyLeakedRef to PyLeaked
This series will make PyLeaked* behave more like a Python iterator, which
means mutation of the owner object will be allowed and the leaked reference
(i.e. the iterator) will be invalidated instead.
I'll add PyLeakedRef/PyLeakedRefMut structs which will represent a "borrowed"
state, and prevent the underlying value from being mutably borrowed while the
leaked reference is in use:
let shared = self.inner_shared(py);
let leaked = shared.leak_immutable();
{
let leaked_ref: PyLeakedRef<_> = leaked.borrow(py);
shared.borrow_mut(); // panics since the underlying value is borrowed
}
shared.borrow_mut(); // allowed
The relation between PyLeaked* structs is quite similar to RefCell/Ref/RefMut,
but the implementation can't be reused because the borrowing state will have
to be shared across objects having no lifetime relation.
PyLeaked isn't named as PyLeakedCell since it isn't actually a cell in that
leaked.borrow_mut() will require &mut self.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sat, 12 Oct 2019 19:10:51 +0900 |
parents | 7a01778bc7b7 |
children | 3bd77c64bc74 |
line wrap: on
line source
// ancestors.rs // // Copyright 2018 Georges Racinet <gracinet@anybox.fr> // // This software may be used and distributed according to the terms of the // GNU General Public License version 2 or any later version. //! Bindings for Rust errors //! //! [`GraphError`] exposes `hg::GraphError` as a subclass of `ValueError` //! but some variants of `hg::GraphError` can be converted directly to other //! existing Python exceptions if appropriate. //! //! [`GraphError`]: struct.GraphError.html use cpython::{ exc::{IOError, RuntimeError, ValueError}, py_exception, PyErr, Python, }; use hg; py_exception!(rustext, GraphError, ValueError); impl GraphError { pub fn pynew(py: Python, inner: hg::GraphError) -> PyErr { match inner { hg::GraphError::ParentOutOfRange(r) => { GraphError::new(py, ("ParentOutOfRange", r)) } hg::GraphError::WorkingDirectoryUnsupported => { match py .import("mercurial.error") .and_then(|m| m.get(py, "WdirUnsupported")) { Err(e) => e, Ok(cls) => PyErr::from_instance(py, cls), } } } } } py_exception!(rustext, PatternError, RuntimeError); py_exception!(rustext, PatternFileError, RuntimeError); py_exception!(rustext, HgPathPyError, RuntimeError); impl PatternError { pub fn pynew(py: Python, inner: hg::PatternError) -> PyErr { match inner { hg::PatternError::UnsupportedSyntax(m) => { PatternError::new(py, ("PatternError", m)) } } } } impl PatternFileError { pub fn pynew(py: Python, inner: hg::PatternFileError) -> PyErr { match inner { hg::PatternFileError::IO(e) => { let value = (e.raw_os_error().unwrap_or(2), e.to_string()); PyErr::new::<IOError, _>(py, value) } hg::PatternFileError::Pattern(e, l) => match e { hg::PatternError::UnsupportedSyntax(m) => { PatternFileError::new(py, ("PatternFileError", m, l)) } }, } } } py_exception!(shared_ref, AlreadyBorrowed, RuntimeError);