changeset 27778:4d10600c3f08

util: simplify file I/O functions using context managers
author Bryan O'Sullivan <bryano@fb.com>
date Tue, 12 Jan 2016 14:49:35 -0800
parents 47ac135113ec
children b2479d305c10
files mercurial/util.py
diffstat 1 files changed, 3 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/util.py	Tue Jan 12 14:49:10 2016 -0800
+++ b/mercurial/util.py	Tue Jan 12 14:49:35 2016 -0800
@@ -1445,25 +1445,16 @@
         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