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.
--- 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