[PATCH 5 of 8] util: add readfile() & writefile() helper functions
Dan Villiom Podlaski Christiansen
danchr at gmail.com
Fri Dec 24 18:35:34 CST 2010
# HG changeset patch
# User Dan Villiom Podlaski Christiansen <danchr at gmail.com>
# Date 1293200581 -3600
# Node ID a2eaf9582e86fc4177b5dd5f88e994710028caf8
# Parent 12f7ef8071592cb1e071ea1c8d2dfa801540a578
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
@@ -957,6 +957,20 @@ class opener(object):
f.close()
self._fixfilemode(dst)
+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