[PATCH 6 of 9] util: add readfile() & writefile() helper functions
Dan Villiom Podlaski Christiansen
danchr at gmail.com
Sun Dec 26 05:51:22 CST 2010
# 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)
+ 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