[PATCH 4 of 5] cmdutil: reimplement file wrapper that disables close()

Yuya Nishihara yuya at tcha.org
Thu Dec 17 08:09:24 CST 2015


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1450004471 -32400
#      Sun Dec 13 20:01:11 2015 +0900
# Node ID 41f488a682b0c9be578244fd4e95ba7ae09f796f
# Parent  14d8c7b6d03751e3fa7899c3d45785e40bbf399b
cmdutil: reimplement file wrapper that disables close()

There's no need to dynamically create wrappedfileobj class and define
close() as lambda. Also, __iter__() was missing.

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -437,6 +437,19 @@ def makefilename(repo, pat, node, desc=N
         raise error.Abort(_("invalid format spec '%%%s' in output filename") %
                          inst.args[0])
 
+class _unclosablefile(object):
+    def __init__(self, fp):
+        self._fp = fp
+
+    def close(self):
+        pass
+
+    def __iter__(self):
+        return iter(self._fp)
+
+    def __getattr__(self, attr):
+        return getattr(self._fp, attr)
+
 def makefileobj(repo, pat, node=None, desc=None, total=None,
                 seqno=None, revwidth=None, mode='wb', modemap=None,
                 pathname=None):
@@ -453,17 +466,7 @@ def makefileobj(repo, pat, node=None, de
         else:
             # if this fp can't be duped properly, return
             # a dummy object that can be closed
-            class wrappedfileobj(object):
-                noop = lambda x: None
-                def __init__(self, f):
-                    self.f = f
-                def __getattr__(self, attr):
-                    if attr == 'close':
-                        return self.noop
-                    else:
-                        return getattr(self.f, attr)
-
-            return wrappedfileobj(fp)
+            return _unclosablefile(fp)
     if util.safehasattr(pat, 'write') and writable:
         return pat
     if util.safehasattr(pat, 'read') and 'r' in mode:


More information about the Mercurial-devel mailing list