##// END OF EJS Templates
hgweb: encode WSGI environment using the ISO-8859-1 codec...
hgweb: encode WSGI environment using the ISO-8859-1 codec The WSGI specification (PEP 3333) specifies that on Python 3 all strings passed by the server must be of type str with code points encodable using the ISO 8859-1 codec. For some reason, I introduced a bug in 2632c1ed8f34 by applying the reverse change. Maybe I got confused because PEP 3333 says that arbitrary operating system environment variables may be contained in the WSGI environment and therefore we need to handle the WSGI environment variables like we would handle operating system environment variables. The bug mentioned in the previous paragraph and fixed by this changeset manifested e.g. in the path of the URL being encoded in the wrong way. Browsers encode non-ASCII bytes with the percent-encoding. WSGI servers will decode the percent-encoded bytes and pass them to the application as strings where each byte is mapped to the corresponding code point with the same ordinal (i.e. it is decoded using the ISO-8859-1 codec). Mercurial uses the bytes type for these strings (which makes much more sense), so we need to encode it again using the ISO-8859-1 codec. If we use another codec, it can result in nonsense.

File last commit:

r48882:bf8837e3 default
r51713:9ed281bb stable
Show More
copymap.rs
122 lines | 3.3 KiB | application/rls-services+xml | RustLexer
// copymap.rs
//
// Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
//
// 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 `hg::dirstate::dirstate_map::CopyMap` provided by the
//! `hg-core` package.
use cpython::{
PyBytes, PyClone, PyDict, PyObject, PyResult, Python, UnsafePyLeaked,
};
use std::cell::RefCell;
use crate::dirstate::dirstate_map::v2_error;
use crate::dirstate::dirstate_map::DirstateMap;
use hg::dirstate::CopyMapIter;
use hg::dirstate_tree::on_disk::DirstateV2ParseError;
use hg::utils::hg_path::HgPath;
py_class!(pub class CopyMap |py| {
data dirstate_map: DirstateMap;
def __getitem__(&self, key: PyObject) -> PyResult<PyBytes> {
(*self.dirstate_map(py)).copymapgetitem(py, key)
}
def __len__(&self) -> PyResult<usize> {
self.dirstate_map(py).copymaplen(py)
}
def __contains__(&self, key: PyObject) -> PyResult<bool> {
self.dirstate_map(py).copymapcontains(py, key)
}
def get(
&self,
key: PyObject,
default: Option<PyObject> = None
) -> PyResult<Option<PyObject>> {
self.dirstate_map(py).copymapget(py, key, default)
}
def pop(
&self,
key: PyObject,
default: Option<PyObject> = None
) -> PyResult<Option<PyObject>> {
self.dirstate_map(py).copymappop(py, key, default)
}
def __iter__(&self) -> PyResult<CopyMapKeysIterator> {
self.dirstate_map(py).copymapiter(py)
}
// Python's `dict()` builtin works with either a subclass of dict
// or an abstract mapping. Said mapping needs to implement `__getitem__`
// and `keys`.
def keys(&self) -> PyResult<CopyMapKeysIterator> {
self.dirstate_map(py).copymapiter(py)
}
def items(&self) -> PyResult<CopyMapItemsIterator> {
self.dirstate_map(py).copymapitemsiter(py)
}
def iteritems(&self) -> PyResult<CopyMapItemsIterator> {
self.dirstate_map(py).copymapitemsiter(py)
}
def __setitem__(
&self,
key: PyObject,
item: PyObject
) -> PyResult<()> {
self.dirstate_map(py).copymapsetitem(py, key, item)?;
Ok(())
}
def copy(&self) -> PyResult<PyDict> {
self.dirstate_map(py).copymapcopy(py)
}
});
impl CopyMap {
pub fn from_inner(py: Python, dm: DirstateMap) -> PyResult<Self> {
Self::create_instance(py, dm)
}
fn translate_key(
py: Python,
res: Result<(&HgPath, &HgPath), DirstateV2ParseError>,
) -> PyResult<Option<PyBytes>> {
let (k, _v) = res.map_err(|e| v2_error(py, e))?;
Ok(Some(PyBytes::new(py, k.as_bytes())))
}
fn translate_key_value(
py: Python,
res: Result<(&HgPath, &HgPath), DirstateV2ParseError>,
) -> PyResult<Option<(PyBytes, PyBytes)>> {
let (k, v) = res.map_err(|e| v2_error(py, e))?;
Ok(Some((
PyBytes::new(py, k.as_bytes()),
PyBytes::new(py, v.as_bytes()),
)))
}
}
py_shared_iterator!(
CopyMapKeysIterator,
UnsafePyLeaked<CopyMapIter<'static>>,
CopyMap::translate_key,
Option<PyBytes>
);
py_shared_iterator!(
CopyMapItemsIterator,
UnsafePyLeaked<CopyMapIter<'static>>,
CopyMap::translate_key_value,
Option<(PyBytes, PyBytes)>
);