[PATCH 3 of 3] util: add readfile() & writefile() helper functions
Dan Villiom Podlaski Christiansen
danchr at gmail.com
Sun May 1 04:20:09 CDT 2011
# HG changeset patch
# User Dan Villiom Podlaski Christiansen <danchr at gmail.com>
# Date 1304185315 -7200
# Node ID 9eda7d15fdf520c5408963fd70f356b10b01a78b
# Parent 8b489f0fbc996eb5ddf5f0b3b05c5b4b4c2f5665
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, 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