changeset 14097:ca3376f044f8

opener: add read & write utility methods The two new methods are useful for quickly opening a file for reading or writing. Unlike 'opener(...).read()', they ensure they the file is immediately closed without relying on CPython reference counting.
author Dan Villiom Podlaski Christiansen <danchr@gmail.com>
date Sat, 30 Apr 2011 19:41:53 +0200
parents dea93484cf9f
children 9f5a0acb0056
files mercurial/scmutil.py
diffstat 1 files changed, 14 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/scmutil.py	Sun May 01 11:01:57 2011 +0200
+++ b/mercurial/scmutil.py	Sat Apr 30 19:41:53 2011 +0200
@@ -136,6 +136,20 @@
         '''Prevent instantiation; don't call this from subclasses.'''
         raise NotImplementedError('attempted instantiating ' + str(type(self)))
 
+    def read(self, *args, **kwargs):
+        fp = self(*args, **kwargs)
+        try:
+            return fp.read()
+        finally:
+            fp.close()
+
+    def write(self, data, *args, **kwargs):
+        fp = self(*args, **kwargs)
+        try:
+            return fp.write(data)
+        finally:
+            fp.close()
+
 class opener(abstractopener):
     '''Open files relative to a base directory