diff -r 50269a4dce61 -r 6d96658a22b0 mercurial/util.py --- a/mercurial/util.py Thu Jun 23 18:20:58 2016 +0100 +++ b/mercurial/util.py Thu Jun 23 18:21:25 2016 +0100 @@ -1516,6 +1516,15 @@ if safehasattr(self, '_fp'): # constructor actually did something self.discard() + def __enter__(self): + return self + + def __exit__(self, exctype, excvalue, traceback): + if exctype is not None: + self.discard() + else: + self.close() + def makedirs(name, mode=None, notindexed=False): """recursive directory creation with parent mode inheritance