[PATCH 2 of 5 V2] osutil: implement __enter__ and __exit__ on posixfile

Gregory Szorc gregory.szorc at gmail.com
Sat Jan 2 18:45:30 CST 2016


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1451781717 28800
#      Sat Jan 02 16:41:57 2016 -0800
# Node ID 3481c6e7a3ee0816aa6fc78a8a0c9c33d3ce4a59
# Parent  8a33bbee1ed445f61c825a789d7c5b9d93295eef
osutil: implement __enter__ and __exit__ on posixfile

So they can be used as context managers.

diff --git a/mercurial/pure/osutil.py b/mercurial/pure/osutil.py
--- a/mercurial/pure/osutil.py
+++ b/mercurial/pure/osutil.py
@@ -249,8 +249,14 @@ else:
             return getattr(self._file, name)
 
         def __setattr__(self, name, value):
             '''mimics the read-only attributes of Python file objects
             by raising 'TypeError: readonly attribute' if someone tries:
               f = posixfile('foo.txt')
               f.name = 'bla'  '''
             return self._file.__setattr__(name, value)
+
+        def __enter__(self):
+            return self._file.__enter__()
+
+        def __exit__(self, exc_type, exc_value, exc_tb):
+            return self._file.__exit__(exc_type, exc_value, exc_tb)
\ No newline at end of file


More information about the Mercurial-devel mailing list