comparison mercurial/cmdutil.py @ 14638:1bdbca0b6604

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).
author Idan Kamara <idankk86@gmail.com>
date Wed, 15 Jun 2011 23:50:33 +0300
parents 5e9d691229d5
children 35c2cc322ba8
comparison
equal deleted inserted replaced
14637:5e9d691229d5 14638:1bdbca0b6604
159 159
160 writable = mode not in ('r', 'rb') 160 writable = mode not in ('r', 'rb')
161 161
162 if not pat or pat == '-': 162 if not pat or pat == '-':
163 fp = writable and repo.ui.fout or repo.ui.fin 163 fp = writable and repo.ui.fout or repo.ui.fin
164 return os.fdopen(os.dup(fp.fileno()), mode) 164 if hasattr(fp, 'fileno'):
165 return os.fdopen(os.dup(fp.fileno()), mode)
166 else:
167 # if this fp can't be duped properly, return
168 # a dummy object that can be closed
169 class wrappedfileobj(object):
170 noop = lambda x: None
171 def __init__(self, f):
172 self.f = f
173 def __getattr__(self, attr):
174 if attr == 'close':
175 return self.noop
176 else:
177 return getattr(self.f, attr)
178
179 return wrappedfileobj(fp)
165 if hasattr(pat, 'write') and writable: 180 if hasattr(pat, 'write') and writable:
166 return pat 181 return pat
167 if hasattr(pat, 'read') and 'r' in mode: 182 if hasattr(pat, 'read') and 'r' in mode:
168 return pat 183 return pat
169 return open(makefilename(repo, pat, node, total, seqno, revwidth, 184 return open(makefilename(repo, pat, node, total, seqno, revwidth,