Permalink
Browse files

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

--HG--
extra : rebase_source : 47f7f8b38d3f741f6af334172c90c4d518e4fca7
  • Loading branch information...
1 parent 04c6f30 commit d2244a2192d1afd518777f4cf0015123ab9aa6bf Matt Mackall committed Apr 17, 2012
Showing with 9 additions and 0 deletions.
  1. +9 −0 mercurial/scmutil.py
View
@@ -159,6 +159,15 @@ def __init__(self, *args, **kwargs):
'''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:

0 comments on commit d2244a2

Please sign in to comment.