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

login
register
mail settings
Submitter Gregory Szorc
Date Jan. 3, 2016, 12:45 a.m.
Message ID <3481c6e7a3ee0816aa6f.1451781930@ubuntu-vm-main>
Download mbox | patch
Permalink /patch/12496/
State Accepted
Delegated to: Matt Mackall
Headers show

Comments

Gregory Szorc - Jan. 3, 2016, 12:45 a.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@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.

\ No newline at end of file

Patch

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)