[PATCH 10 of 10] util: simplify file I/O functions using context managers

Augie Fackler raf at durin42.com
Thu Jan 14 12:36:23 CST 2016


On Tue, Jan 12, 2016 at 02:50:13PM -0800, Bryan O'Sullivan wrote:
> # HG changeset patch
> # User Bryan O'Sullivan <bryano at fb.com>
> # Date 1452638975 28800
> #      Tue Jan 12 14:49:35 2016 -0800
> # Node ID 69a304f8a51acd7c833a2be0d3aadc2c7935d42f
> # Parent  e9a844767dfabda6625b15d1f2e6231aefa13e2c
> util: simplify file I/O functions using context managers

Queued these, thanks.

>
> diff --git a/mercurial/util.py b/mercurial/util.py
> --- a/mercurial/util.py
> +++ b/mercurial/util.py
> @@ -1444,25 +1444,16 @@ def ensuredirs(name, mode=None, notindex
>          os.chmod(name, mode)
>
>  def readfile(path):
> -    fp = open(path, 'rb')
> -    try:
> +    with open(path, 'rb') as fp:
>          return fp.read()
> -    finally:
> -        fp.close()
>
>  def writefile(path, text):
> -    fp = open(path, 'wb')
> -    try:
> +    with open(path, 'wb') as fp:
>          fp.write(text)
> -    finally:
> -        fp.close()
>
>  def appendfile(path, text):
> -    fp = open(path, 'ab')
> -    try:
> +    with open(path, 'ab') as fp:
>          fp.write(text)
> -    finally:
> -        fp.close()
>
>  class chunkbuffer(object):
>      """Allow arbitrary sized chunks of data to be efficiently read from an
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at selenic.com
> https://selenic.com/mailman/listinfo/mercurial-devel


More information about the Mercurial-devel mailing list