# HG changeset patch # User Dan Villiom Podlaski Christiansen # Date 2011-04-30 17:41:53 # Node ID ca3376f044f8b35d048b19c7bdfd4925ce532d88 # Parent dea93484cf9f949ef73cb698b73af16097f612d0 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. diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py --- a/mercurial/scmutil.py +++ b/mercurial/scmutil.py @@ -136,6 +136,20 @@ class abstractopener(object): '''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