[PATCH 6 of 9] util: add readfile() & writefile() helper functions

Adrian Buehlmann adrian at cadifra.com
Mon Dec 27 14:40:34 CST 2010


On 2010-12-26 12:51, Dan Villiom Podlaski Christiansen wrote:
> # HG changeset patch
> # User Dan Villiom Podlaski Christiansen <danchr at gmail.com>
> # Date 1293364215 -3600
> # Node ID 164e179694827a67f5fab098b4ce1a1c001518c5
> # Parent  3b29e00516d7b79cbac6fdd5a9c0e71746bf6df6
> util: add readfile() & writefile() helper functions
> 
> These two functions allow quickly reading or writing a file, without
> relying on reference counting to close the file handle afterwards.
> 
> diff --git a/mercurial/util.py b/mercurial/util.py
> --- a/mercurial/util.py
> +++ b/mercurial/util.py
> @@ -973,6 +973,20 @@ class filteropener(object):
>      def write(self, data, path, *args, **kwargs):
>          return self._orig.write(self._filter(path), *args, **kwargs)
>  
> +def readfile(path, size=-1):
> +    fp = open(path)

Can we please have posixfile here instead of open, while we're at it?

(Yes, this is indeed for the stupid rest of us who is still using
Windows. And yes I know that the original code places used Python's open).

> +    try:
> +        return fp.read(size)
> +    finally:
> +        fp.close()
> +
> +def writefile(path, mode, text):
> +    fp = open(path, mode)

same here

> +    try:
> +        fp.write(text)
> +    finally:
> +        fp.close()
> +
>  class chunkbuffer(object):
>      """Allow arbitrary sized chunks of data to be efficiently read from an
>      iterator over chunks of arbitrary size."""

Thanks.


More information about the Mercurial-devel mailing list