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

Dan Villiom Podlaski Christiansen danchr at gmail.com
Sun May 1 04:47:00 CDT 2011


# HG changeset patch
# User Dan Villiom Podlaski Christiansen <danchr at gmail.com>
# Date 1304243209 -7200
# Node ID 34ac4d2c9e287ba10e77631df9c5f68d70dd671c
# Parent  4510dc0c677f84a73b212acb4c41c78dee2ebffa
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
@@ -771,6 +771,20 @@ def makedirs(name, mode=None):
     makedirs(parent, mode)
     makedirs(name, mode)
 
+def readfile(path):
+    fp = open(path)
+    try:
+        return fp.read(size)
+    finally:
+        fp.close()
+
+def writefile(path, mode, text):
+    fp = open(path, mode)
+    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."""


More information about the Mercurial-devel mailing list