changeset 16455:154219f3a6a4

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".
author Matt Mackall <mpm@selenic.com>
date Tue, 17 Apr 2012 11:11:59 -0500
parents 92c7e917b647
children 42862d64aa8f
files mercurial/scmutil.py
diffstat 1 files changed, 9 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/scmutil.py	Tue Apr 17 07:22:44 2012 +0200
+++ b/mercurial/scmutil.py	Tue Apr 17 11:11:59 2012 -0500
@@ -159,6 +159,15 @@
         '''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: