##// END OF EJS Templates
chg: populate CHGHG if not set...
chg: populate CHGHG if not set Normally, chg determines which `hg` executable to use by first consulting the `$CHGHG` and `$HG` environment variables, and if neither are present defaults to the `hg` found in the user's `$PATH`. If built with the `HGPATHREL` compiler flag, chg will instead assume that there exists an `hg` executable in the same directory as the `chg` binary and attempt to use that. This can cause problems in situations where there are multiple actively-used Mercurial installations on the same system. When a `chg` client connects to a running command server, the server process performs some basic validation to determine whether a new command server needs to be spawned. These checks include things like checking certain "sensitive" environment variables and config sections, as well as checking whether the mtime of the extensions, hg's `__version__.py` module, and the Python interpreter have changed. Crucially, the command server doesn't explicitly check whether the executable it is running from matches the executable that the `chg` client would have otherwise invoked had there been no existing command server process. Without `HGPATHREL`, this still gets implicitly checked during the validation step, because the only way to specify an alternate hg executable (apart from `$PATH`) is via the `$CHGHG` and `$HG` environment variables, both of which are checked. With `HGPATHREL`, however, the command server has no way of knowing which hg executable the client would have run. This means that a client located at `/version_B/bin/chg` will happily connect to a command server running `/version_A/bin/hg` instead of `/version_B/bin/hg` as expected. A simple solution is to have the client set `$CHGHG` itself, which then allows the command server's environment validation to work as intended. I have tested this manually using two locally built hg installations and it seems to work with no ill effects. That said, I'm not sure how to write an automated test for this since the `chg` available to the tests isn't even built with the `HGPATHREL` compiler flag to begin with.

File last commit:

r44704:6b7aef44 default
r51279:cf4d2f31 stable
Show More
ref_sharing.rs
121 lines | 4.0 KiB | application/rls-services+xml | RustLexer
Yuya Nishihara
rust-cpython: change license of ref_sharing.rs to MIT...
r43352 // ref_sharing.rs
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 //
// Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
//
Yuya Nishihara
rust-cpython: change license of ref_sharing.rs to MIT...
r43352 // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997
//! Macros for use in the `hg-cpython` bridge library.
/// Defines a `py_class!` that acts as a Python iterator over a Rust iterator.
Yuya Nishihara
rust-cpython: replace dyn Iterator<..> of mapping with concrete type...
r43158 ///
/// TODO: this is a bit awkward to use, and a better (more complicated)
/// procedural macro would simplify the interface a lot.
///
/// # Parameters
///
/// * `$name` is the identifier to give to the resulting Rust struct.
Yuya Nishihara
rust-cpython: remove PySharedRefCell and its companion structs...
r44704 /// * `$leaked` corresponds to `UnsafePyLeaked` in the matching `@shared data`
/// declaration.
Yuya Nishihara
rust-cpython: replace dyn Iterator<..> of mapping with concrete type...
r43158 /// * `$iterator_type` is the type of the Rust iterator.
/// * `$success_func` is a function for processing the Rust `(key, value)`
/// tuple on iteration success, turning it into something Python understands.
/// * `$success_func` is the return type of `$success_func`
///
Yuya Nishihara
rust-cpython: mark all PyLeaked methods as unsafe...
r44689 /// # Safety
///
/// `$success_func` may take a reference, but it's lifetime may be cheated.
/// Do not copy it out of the function call.
///
Yuya Nishihara
rust-cpython: replace dyn Iterator<..> of mapping with concrete type...
r43158 /// # Example
///
/// ```
/// struct MyStruct {
/// inner: HashMap<Vec<u8>, Vec<u8>>;
/// }
///
/// py_class!(pub class MyType |py| {
Yuya Nishihara
rust-cpython: remove PySharedRefCell and its companion structs...
r44704 /// @shared data inner: MyStruct;
Yuya Nishihara
rust-cpython: replace dyn Iterator<..> of mapping with concrete type...
r43158 ///
/// def __iter__(&self) -> PyResult<MyTypeItemsIterator> {
Yuya Nishihara
rust-cpython: remove useless PyResult<> from leak_immutable()...
r43611 /// let leaked_ref = self.inner_shared(py).leak_immutable();
Yuya Nishihara
rust-cpython: leverage py_shared_iterator::from_inner() where appropriate
r43161 /// MyTypeItemsIterator::from_inner(
Yuya Nishihara
rust-cpython: replace dyn Iterator<..> of mapping with concrete type...
r43158 /// py,
Yuya Nishihara
rust-cpython: make PyLeakedRef operations relatively safe...
r43579 /// unsafe { leaked_ref.map(py, |o| o.iter()) },
Yuya Nishihara
rust-cpython: replace dyn Iterator<..> of mapping with concrete type...
r43158 /// )
/// }
/// });
///
/// impl MyType {
/// fn translate_key_value(
/// py: Python,
/// res: (&Vec<u8>, &Vec<u8>),
/// ) -> PyResult<Option<(PyBytes, PyBytes)>> {
/// let (f, entry) = res;
/// Ok(Some((
/// PyBytes::new(py, f),
/// PyBytes::new(py, entry),
/// )))
/// }
/// }
///
Yuya Nishihara
rust-cpython: rename py_shared_iterator_impl to py_shared_iterator...
r43159 /// py_shared_iterator!(
Yuya Nishihara
rust-cpython: replace dyn Iterator<..> of mapping with concrete type...
r43158 /// MyTypeItemsIterator,
Yuya Nishihara
rust-cpython: remove PySharedRefCell and its companion structs...
r44704 /// UnsafePyLeaked<HashMap<'static, Vec<u8>, Vec<u8>>>,
Yuya Nishihara
rust-cpython: replace dyn Iterator<..> of mapping with concrete type...
r43158 /// MyType::translate_key_value,
/// Option<(PyBytes, PyBytes)>
/// );
/// ```
Yuya Nishihara
rust-cpython: rename py_shared_iterator_impl to py_shared_iterator...
r43159 macro_rules! py_shared_iterator {
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 (
$name: ident,
Yuya Nishihara
rust-cpython: move $leaked struct out of macro...
r43447 $leaked: ty,
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 $success_func: expr,
$success_type: ty
) => {
py_class!(pub class $name |py| {
Yuya Nishihara
rust-cpython: remove useless Option<$leaked> from py_shared_iterator...
r43607 data inner: RefCell<$leaked>;
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997
def __next__(&self) -> PyResult<$success_type> {
Yuya Nishihara
rust-cpython: remove useless Option<$leaked> from py_shared_iterator...
r43607 let mut leaked = self.inner(py).borrow_mut();
Yuya Nishihara
rust-cpython: mark all PyLeaked methods as unsafe...
r44689 let mut iter = unsafe { leaked.try_borrow_mut(py)? };
Yuya Nishihara
rust-cpython: remove useless Option<$leaked> from py_shared_iterator...
r43607 match iter.next() {
None => Ok(None),
Yuya Nishihara
rust-cpython: mark all PyLeaked methods as unsafe...
r44689 // res may be a reference of cheated 'static lifetime
Yuya Nishihara
rust-cpython: remove useless Option<$leaked> from py_shared_iterator...
r43607 Some(res) => $success_func(py, res),
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 }
}
def __iter__(&self) -> PyResult<Self> {
Ok(self.clone_ref(py))
}
});
impl $name {
pub fn from_inner(
py: Python,
Yuya Nishihara
rust-cpython: remove Option<_> from interface of py_shared_iterator...
r43160 leaked: $leaked,
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 ) -> PyResult<Self> {
Self::create_instance(
py,
Yuya Nishihara
rust-cpython: remove useless Option<$leaked> from py_shared_iterator...
r43607 RefCell::new(leaked),
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 )
}
}
};
}