# HG changeset patch # User Idan Kamara # Date 2011-06-15 20:50:33 # Node ID 1bdbca0b66041e97a9c49d4e3a5ca1bc67082137 # Parent 5e9d691229d5d08046e8008aad864f714e982a3a cmdutil: return a dummy, closable file object if it cannot be duped If the ui I/O descriptors aren't real descriptors, they cannot be duped. Instead, we return a wrapper object that behaves the same, and can be closed (by overriding close and doing nothing). diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py --- a/mercurial/cmdutil.py +++ b/mercurial/cmdutil.py @@ -161,7 +161,22 @@ def makefileobj(repo, pat, node=None, to if not pat or pat == '-': fp = writable and repo.ui.fout or repo.ui.fin - return os.fdopen(os.dup(fp.fileno()), mode) + if hasattr(fp, 'fileno'): + return os.fdopen(os.dup(fp.fileno()), mode) + 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) if hasattr(pat, 'write') and writable: return pat if hasattr(pat, 'read') and 'r' in mode: