# HG changeset patch # User Matt Mackall # Date 2012-04-17 16:11:59 # Node ID 154219f3a6a40b9596e4f3e36d5f1a89180559f2 # Parent 92c7e917b647768b23ba6554af44ed0e74b28925 opener: introduce tryread helper This makes it easier to follow the common pattern "read a file or give an empty string if it's missing". diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py --- a/mercurial/scmutil.py +++ b/mercurial/scmutil.py @@ -159,6 +159,15 @@ class abstractopener(object): '''Prevent instantiation; don't call this from subclasses.''' raise NotImplementedError('attempted instantiating ' + str(type(self))) + def tryread(self, path): + 'gracefully return an empty string for missing files' + try: + return self.read(path) + except IOError, inst: + if inst.errno != errno.ENOENT: + raise + return "" + def read(self, path): fp = self(path, 'rb') try: