comparison mercurial/cmdutil.py @ 13121:bf763946f8b0

make_file: always return a fresh file handle that can be closed Currently, cmdutil.make_file() will return a freshly made file handle, except when given a pattern of '-'. If callers would want to close the handle, they would have to make sure that it's neither sys.stdin or sys.stdout. Instead, returning a duplicate of either of the two ensures that make_file() lives up to its name and creates a new file handle regardless of the input.
author Dan Villiom Podlaski Christiansen <danchr@gmail.com>
date Tue, 07 Dec 2010 16:08:16 +0100
parents 79184986658c
children f78bc5ddbe4f
comparison
equal deleted inserted replaced
13120:8568bbdfbafe 13121:bf763946f8b0
231 total=None, seqno=None, revwidth=None, mode='wb', pathname=None): 231 total=None, seqno=None, revwidth=None, mode='wb', pathname=None):
232 232
233 writable = 'w' in mode or 'a' in mode 233 writable = 'w' in mode or 'a' in mode
234 234
235 if not pat or pat == '-': 235 if not pat or pat == '-':
236 return writable and sys.stdout or sys.stdin 236 fp = writable and sys.stdout or sys.stdin
237 return os.fdopen(os.dup(fp.fileno()), mode)
237 if hasattr(pat, 'write') and writable: 238 if hasattr(pat, 'write') and writable:
238 return pat 239 return pat
239 if hasattr(pat, 'read') and 'r' in mode: 240 if hasattr(pat, 'read') and 'r' in mode:
240 return pat 241 return pat
241 return open(make_filename(repo, pat, node, total, seqno, revwidth, 242 return open(make_filename(repo, pat, node, total, seqno, revwidth,