##// END OF EJS Templates
posix: always seek to EOF when opening a file in append mode...
posix: always seek to EOF when opening a file in append mode Python 3 already does this, so skip it there. Consider the program: #include <stdio.h> int main() { FILE *f = fopen("narf", "w"); fprintf(f, "narf\n"); fclose(f); f = fopen("narf", "a"); printf("%ld\n", ftell(f)); fprintf(f, "troz\n"); printf("%ld\n", ftell(f)); return 0; } on macOS, FreeBSD, and Linux with glibc, this program prints 5 10 but on musl libc (Alpine Linux and probably others) this prints 0 10 By my reading of https://pubs.opengroup.org/onlinepubs/009695399/functions/fopen.html this is technically correct, specifically: > Opening a file with append mode (a as the first character in the > mode argument) shall cause all subsequent writes to the file to be > forced to the then current end-of-file, regardless of intervening > calls to fseek(). in other words, the file position doesn't really matter in append-mode files, and we can't depend on it being at all meaningful unless we perform a seek() before tell() after open(..., 'a'). Experimentally after a .write() we can do a .tell() and it'll always be reasonable, but I'm unclear from reading the specification if that's a smart thing to rely on. This matches what we do on Windows and what Python 3 does for free, so let's just be consistent. Thanks to Yuya for the idea.

File last commit:

r41842:060c030c default
r43163:97ada9b8 5.0.2 stable
Show More
conversion.rs
50 lines | 1.7 KiB | application/rls-services+xml | RustLexer
// conversion.rs
//
// Copyright 2019 Georges Racinet <georges.racinet@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 the hg::ancestors module provided by the
//! `hg-core` crate. From Python, this will be seen as `rustext.ancestor`
use cpython::{
ObjectProtocol, PyDict, PyObject, PyResult, PyTuple, Python, PythonObject,
ToPyObject,
};
use hg::Revision;
use std::collections::HashSet;
use std::iter::FromIterator;
/// Utility function to convert a Python iterable into various collections
///
/// We need this in particular to feed to various methods of inner objects
/// with `impl IntoIterator<Item=Revision>` arguments, because
/// a `PyErr` can arise at each step of iteration, whereas these methods
/// expect iterables over `Revision`, not over some `Result<Revision, PyErr>`
pub fn rev_pyiter_collect<C>(py: Python, revs: &PyObject) -> PyResult<C>
where
C: FromIterator<Revision>,
{
revs.iter(py)?
.map(|r| r.and_then(|o| o.extract::<Revision>(py)))
.collect()
}
/// Copy and convert an `HashSet<Revision>` in a Python set
///
/// This will probably turn useless once `PySet` support lands in
/// `rust-cpython`.
///
/// This builds a Python tuple, then calls Python's "set()" on it
pub fn py_set(py: Python, set: &HashSet<Revision>) -> PyResult<PyObject> {
let as_vec: Vec<PyObject> = set
.iter()
.map(|rev| rev.to_py_object(py).into_object())
.collect();
let as_pytuple = PyTuple::new(py, as_vec.as_slice());
let locals = PyDict::new(py);
locals.set_item(py, "obj", as_pytuple.to_py_object(py))?;
py.eval("set(obj)", None, Some(&locals))
}