# HG changeset patch # User Bryan O'Sullivan # Date 2016-01-12 22:49:35 # Node ID 4d10600c3f08880902707eaea5ca961a6d770aae # Parent 47ac135113ec944826db6e76b7b8af76e4a166e1 util: simplify file I/O functions using context managers diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -1445,25 +1445,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